coverity appeasement
[minix.git] / lib / libmthread / event.c
blob1dea4e87eb4fb3b0f1e5107eda68966c1955e229
1 #include <minix/mthread.h>
2 #include "global.h"
4 /*===========================================================================*
5 * mthread_event_init *
6 *===========================================================================*/
7 int mthread_event_init(event)
8 mthread_event_t *event; /* The event to be initialized */
10 /* Initialize an event object.
12 int r;
14 if (!event)
15 return EINVAL;
17 r = mthread_mutex_init(&event->mutex, NULL);
18 if (r != 0)
19 return r;
21 r = mthread_cond_init(&event->cond, NULL);
22 if (r != 0)
23 mthread_mutex_destroy(&event->mutex);
25 return r;
29 /*===========================================================================*
30 * mthread_event_destroy *
31 *===========================================================================*/
32 int mthread_event_destroy(event)
33 mthread_event_t *event; /* The event to be destroyed */
35 /* Destroy an event object.
37 int r;
39 if (!event)
40 return EINVAL;
42 r = mthread_cond_destroy(&event->cond);
43 if (r != 0)
44 return r;
46 return mthread_mutex_destroy(&event->mutex);
49 /*===========================================================================*
50 * mthread_event_wait *
51 *===========================================================================*/
52 int mthread_event_wait(event)
53 mthread_event_t *event; /* The event to be waited on */
55 /* Wait for an event, blocking the current thread in the process.
57 int r;
59 if (!event)
60 return EINVAL;
62 r = mthread_mutex_lock(&event->mutex);
63 if (r != 0)
64 return r;
66 r = mthread_cond_wait(&event->cond, &event->mutex);
67 if (r != 0) {
68 mthread_mutex_unlock(&event->mutex);
69 return r;
72 return mthread_mutex_unlock(&event->mutex);
75 /*===========================================================================*
76 * mthread_event_fire *
77 *===========================================================================*/
78 int mthread_event_fire(event)
79 mthread_event_t *event; /* The event to be fired */
81 /* Fire an event, waking up any thread blocked on it.
83 int r;
85 if (!event)
86 return EINVAL;
88 r = mthread_mutex_lock(&event->mutex);
89 if (r != 0)
90 return r;
92 r = mthread_cond_signal(&event->cond);
93 if (r != 0) {
94 mthread_mutex_unlock(&event->mutex);
95 return r;
98 return mthread_mutex_unlock(&event->mutex);
102 /*===========================================================================*
103 * mthread_event_fire_all *
104 *===========================================================================*/
105 int mthread_event_fire_all(event)
106 mthread_event_t *event; /* The event to be fired */
108 /* Fire an event, waking up any thread blocked on it.
110 int r;
112 if (!event)
113 return EINVAL;
115 r = mthread_mutex_lock(&event->mutex);
116 if (r != 0)
117 return r;
119 r = mthread_cond_broadcast(&event->cond);
120 if (r != 0) {
121 mthread_mutex_unlock(&event->mutex);
122 return r;
125 return mthread_mutex_unlock(&event->mutex);