1*433d6423SLionel Sambuc #include <minix/mthread.h>
2*433d6423SLionel Sambuc #include "global.h"
3*433d6423SLionel Sambuc
4*433d6423SLionel Sambuc /*===========================================================================*
5*433d6423SLionel Sambuc * mthread_event_init *
6*433d6423SLionel Sambuc *===========================================================================*/
mthread_event_init(event)7*433d6423SLionel Sambuc int mthread_event_init(event)
8*433d6423SLionel Sambuc mthread_event_t *event; /* The event to be initialized */
9*433d6423SLionel Sambuc {
10*433d6423SLionel Sambuc /* Initialize an event object.
11*433d6423SLionel Sambuc */
12*433d6423SLionel Sambuc int r;
13*433d6423SLionel Sambuc
14*433d6423SLionel Sambuc if (!event)
15*433d6423SLionel Sambuc return EINVAL;
16*433d6423SLionel Sambuc
17*433d6423SLionel Sambuc r = mthread_mutex_init(&event->mutex, NULL);
18*433d6423SLionel Sambuc if (r != 0)
19*433d6423SLionel Sambuc return r;
20*433d6423SLionel Sambuc
21*433d6423SLionel Sambuc r = mthread_cond_init(&event->cond, NULL);
22*433d6423SLionel Sambuc if (r != 0)
23*433d6423SLionel Sambuc mthread_mutex_destroy(&event->mutex);
24*433d6423SLionel Sambuc
25*433d6423SLionel Sambuc return r;
26*433d6423SLionel Sambuc }
27*433d6423SLionel Sambuc
28*433d6423SLionel Sambuc
29*433d6423SLionel Sambuc /*===========================================================================*
30*433d6423SLionel Sambuc * mthread_event_destroy *
31*433d6423SLionel Sambuc *===========================================================================*/
mthread_event_destroy(event)32*433d6423SLionel Sambuc int mthread_event_destroy(event)
33*433d6423SLionel Sambuc mthread_event_t *event; /* The event to be destroyed */
34*433d6423SLionel Sambuc {
35*433d6423SLionel Sambuc /* Destroy an event object.
36*433d6423SLionel Sambuc */
37*433d6423SLionel Sambuc int r;
38*433d6423SLionel Sambuc
39*433d6423SLionel Sambuc if (!event)
40*433d6423SLionel Sambuc return EINVAL;
41*433d6423SLionel Sambuc
42*433d6423SLionel Sambuc r = mthread_cond_destroy(&event->cond);
43*433d6423SLionel Sambuc if (r != 0)
44*433d6423SLionel Sambuc return r;
45*433d6423SLionel Sambuc
46*433d6423SLionel Sambuc return mthread_mutex_destroy(&event->mutex);
47*433d6423SLionel Sambuc }
48*433d6423SLionel Sambuc
49*433d6423SLionel Sambuc /*===========================================================================*
50*433d6423SLionel Sambuc * mthread_event_wait *
51*433d6423SLionel Sambuc *===========================================================================*/
mthread_event_wait(event)52*433d6423SLionel Sambuc int mthread_event_wait(event)
53*433d6423SLionel Sambuc mthread_event_t *event; /* The event to be waited on */
54*433d6423SLionel Sambuc {
55*433d6423SLionel Sambuc /* Wait for an event, blocking the current thread in the process.
56*433d6423SLionel Sambuc */
57*433d6423SLionel Sambuc int r;
58*433d6423SLionel Sambuc
59*433d6423SLionel Sambuc if (!event)
60*433d6423SLionel Sambuc return EINVAL;
61*433d6423SLionel Sambuc
62*433d6423SLionel Sambuc r = mthread_mutex_lock(&event->mutex);
63*433d6423SLionel Sambuc if (r != 0)
64*433d6423SLionel Sambuc return r;
65*433d6423SLionel Sambuc
66*433d6423SLionel Sambuc r = mthread_cond_wait(&event->cond, &event->mutex);
67*433d6423SLionel Sambuc if (r != 0) {
68*433d6423SLionel Sambuc mthread_mutex_unlock(&event->mutex);
69*433d6423SLionel Sambuc return r;
70*433d6423SLionel Sambuc }
71*433d6423SLionel Sambuc
72*433d6423SLionel Sambuc return mthread_mutex_unlock(&event->mutex);
73*433d6423SLionel Sambuc }
74*433d6423SLionel Sambuc
75*433d6423SLionel Sambuc /*===========================================================================*
76*433d6423SLionel Sambuc * mthread_event_fire *
77*433d6423SLionel Sambuc *===========================================================================*/
mthread_event_fire(event)78*433d6423SLionel Sambuc int mthread_event_fire(event)
79*433d6423SLionel Sambuc mthread_event_t *event; /* The event to be fired */
80*433d6423SLionel Sambuc {
81*433d6423SLionel Sambuc /* Fire an event, waking up any thread blocked on it.
82*433d6423SLionel Sambuc */
83*433d6423SLionel Sambuc int r;
84*433d6423SLionel Sambuc
85*433d6423SLionel Sambuc if (!event)
86*433d6423SLionel Sambuc return EINVAL;
87*433d6423SLionel Sambuc
88*433d6423SLionel Sambuc r = mthread_mutex_lock(&event->mutex);
89*433d6423SLionel Sambuc if (r != 0)
90*433d6423SLionel Sambuc return r;
91*433d6423SLionel Sambuc
92*433d6423SLionel Sambuc r = mthread_cond_signal(&event->cond);
93*433d6423SLionel Sambuc if (r != 0) {
94*433d6423SLionel Sambuc mthread_mutex_unlock(&event->mutex);
95*433d6423SLionel Sambuc return r;
96*433d6423SLionel Sambuc }
97*433d6423SLionel Sambuc
98*433d6423SLionel Sambuc return mthread_mutex_unlock(&event->mutex);
99*433d6423SLionel Sambuc }
100*433d6423SLionel Sambuc
101*433d6423SLionel Sambuc
102*433d6423SLionel Sambuc /*===========================================================================*
103*433d6423SLionel Sambuc * mthread_event_fire_all *
104*433d6423SLionel Sambuc *===========================================================================*/
mthread_event_fire_all(event)105*433d6423SLionel Sambuc int mthread_event_fire_all(event)
106*433d6423SLionel Sambuc mthread_event_t *event; /* The event to be fired */
107*433d6423SLionel Sambuc {
108*433d6423SLionel Sambuc /* Fire an event, waking up any thread blocked on it.
109*433d6423SLionel Sambuc */
110*433d6423SLionel Sambuc int r;
111*433d6423SLionel Sambuc
112*433d6423SLionel Sambuc if (!event)
113*433d6423SLionel Sambuc return EINVAL;
114*433d6423SLionel Sambuc
115*433d6423SLionel Sambuc r = mthread_mutex_lock(&event->mutex);
116*433d6423SLionel Sambuc if (r != 0)
117*433d6423SLionel Sambuc return r;
118*433d6423SLionel Sambuc
119*433d6423SLionel Sambuc r = mthread_cond_broadcast(&event->cond);
120*433d6423SLionel Sambuc if (r != 0) {
121*433d6423SLionel Sambuc mthread_mutex_unlock(&event->mutex);
122*433d6423SLionel Sambuc return r;
123*433d6423SLionel Sambuc }
124*433d6423SLionel Sambuc
125*433d6423SLionel Sambuc return mthread_mutex_unlock(&event->mutex);
126*433d6423SLionel Sambuc }
127*433d6423SLionel Sambuc
128*433d6423SLionel Sambuc /* pthread compatibility layer. */
129*433d6423SLionel Sambuc __weak_alias(pthread_event_destroy, mthread_event_destroy)
130*433d6423SLionel Sambuc __weak_alias(pthread_event_init, mthread_event_init)
131*433d6423SLionel Sambuc __weak_alias(pthread_event_wait, mthread_event_wait)
132*433d6423SLionel Sambuc __weak_alias(pthread_event_fire, mthread_event_fire)
133*433d6423SLionel Sambuc __weak_alias(pthread_event_fire_all, mthread_event_fire_all)
134*433d6423SLionel Sambuc
135