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