master
c 269 lines 5.79 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 lxtevent.c
8
9 Abstract:
10
11 This file contains synchronization event primitive support. It enables
12 simple synchronization across threads and forked processes.
13
14 --*/
15
16 #include "lxtevent.h"
17 #include "lxtlog.h"
18 #include <sys/mman.h>
19
20 int LxtSynchronizationEventClear(PLXT_SYNCHRONIZATION_EVENT Event)
21
22 /*++
23
24 Routine Description:
25
26 This routine clears the synchronization event.
27
28 Arguments:
29
30 Event - Supplies a pointer to synchronization event.
31
32 Return Value:
33
34 0 on success, -1 on failure.
35
36 --*/
37
38 {
39 int Result = LXT_RESULT_FAILURE;
40
41 LxtCheckResult(pthread_mutex_lock(&Event->Lock));
42 if (Event->Fail != 0)
43 {
44 LxtCheckResult(pthread_mutex_unlock(&Event->Lock));
45 Result = LXT_RESULT_FAILURE;
46 goto ErrorExit;
47 }
48
49 Event->Ready = 0;
50 LxtCheckResult(pthread_mutex_unlock(&Event->Lock));
51
52 ErrorExit:
53 return Result;
54 }
55
56 int LxtSynchronizationEventDestroy(PLXT_SYNCHRONIZATION_EVENT* Event)
57
58 /*++
59
60 Routine Description:
61
62 This routine frees all resources allocated for the event.
63
64 Arguments:
65
66 Event - Supplies a pointer to synchronization event.
67
68 Return Value:
69
70 0 on success, -1 on failure.
71
72 --*/
73
74 {
75 int Result = LXT_RESULT_FAILURE;
76
77 LxtCheckResult(pthread_mutex_destroy(&(*Event)->Lock));
78 LxtCheckResult(pthread_mutexattr_destroy(&(*Event)->LockAttribute));
79 LxtCheckResult(pthread_cond_destroy(&(*Event)->WaitConditionalVariable));
80 LxtCheckResult(pthread_condattr_destroy(&(*Event)->ConditionVariableAttribute));
81 LxtCheckResult(munmap(*Event, sizeof(*Event)));
82
83 ErrorExit:
84 return Result;
85 }
86
87 int LxtSynchronizationEventFail(PLXT_SYNCHRONIZATION_EVENT Event)
88
89 /*++
90
91 Routine Description:
92
93 This routine sets the fail flag and causes blocked event to be woken up and
94 return with error and also any further calls to set/wait on the event to
95 fail.
96
97 Arguments:
98
99 Event - Supplies a pointer to synchronization event.
100
101 Return Value:
102
103 0 on success, -1 on failure.
104
105 --*/
106
107 {
108 int Result = LXT_RESULT_FAILURE;
109
110 LxtCheckResult(pthread_mutex_lock(&Event->Lock));
111 Event->Fail = 1;
112 LxtCheckResult(pthread_mutex_unlock(&Event->Lock));
113 LxtCheckResult(pthread_cond_signal(&Event->WaitConditionalVariable));
114
115 ErrorExit:
116 return Result;
117 }
118
119 int LxtSynchronizationEventInit(PLXT_SYNCHRONIZATION_EVENT* Event)
120
121 /*++
122
123 Routine Description:
124
125 This routine initializes synchronization event.
126
127 Arguments:
128
129 Event - Supplies a pointer to where synchronization event will be allocated.
130
131 Return Value:
132
133 0 on success, -1 on failure.
134
135 --*/
136
137 {
138 void* MapResult;
139 int Result = LXT_RESULT_FAILURE;
140 PLXT_SYNCHRONIZATION_EVENT LocalEvent = NULL;
141
142 LxtCheckMapErrno(LocalEvent = mmap(NULL, sizeof(*Event), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0));
143
144 LxtCheckResult(pthread_mutexattr_init(&LocalEvent->LockAttribute));
145 LxtCheckResult(pthread_mutexattr_setpshared(&LocalEvent->LockAttribute, PTHREAD_PROCESS_SHARED));
146
147 LxtCheckResult(pthread_mutex_init(&LocalEvent->Lock, &LocalEvent->LockAttribute));
148 LxtCheckResult(pthread_condattr_init(&LocalEvent->ConditionVariableAttribute));
149 LxtCheckResult(pthread_condattr_setpshared(&LocalEvent->ConditionVariableAttribute, PTHREAD_PROCESS_SHARED));
150
151 LxtCheckResult(pthread_cond_init(&LocalEvent->WaitConditionalVariable, &LocalEvent->ConditionVariableAttribute));
152
153 LocalEvent->Ready = 0;
154 LocalEvent->Fail = 0;
155 *Event = LocalEvent;
156 Result = 0;
157
158 ErrorExit:
159 return Result;
160 }
161
162 int LxtSynchronizationEventReset(PLXT_SYNCHRONIZATION_EVENT Event)
163
164 /*++
165
166 Routine Description:
167
168 This routine resets event to the initialized state.
169
170 Arguments:
171
172 Event - Supplies a pointer to synchronization event.
173
174 Return Value:
175
176 0 on success, -1 on failure.
177
178 --*/
179
180 {
181 int Result = LXT_RESULT_FAILURE;
182
183 LxtCheckResult(pthread_mutex_lock(&Event->Lock));
184 LxtCheckResult(pthread_cond_init(&Event->WaitConditionalVariable, &Event->ConditionVariableAttribute));
185
186 Event->Fail == 0;
187 Event->Ready = 0;
188 LxtCheckResult(pthread_mutex_unlock(&Event->Lock));
189
190 ErrorExit:
191 return Result;
192 }
193
194 int LxtSynchronizationEventSet(PLXT_SYNCHRONIZATION_EVENT Event)
195
196 /*++
197
198 Routine Description:
199
200 This routine sets the synchronization event.
201
202 Arguments:
203
204 Event - Supplies a pointer to synchronization event.
205
206 Return Value:
207
208 0 on success, -1 on failure.
209
210 --*/
211
212 {
213 int Result = LXT_RESULT_FAILURE;
214
215 LxtCheckResult(pthread_mutex_lock(&Event->Lock));
216 if (Event->Fail != 0)
217 {
218 LxtCheckResult(pthread_mutex_unlock(&Event->Lock));
219 Result = LXT_RESULT_FAILURE;
220 goto ErrorExit;
221 }
222
223 Event->Ready = 1;
224 LxtCheckResult(pthread_mutex_unlock(&Event->Lock));
225 LxtCheckResult(pthread_cond_signal(&Event->WaitConditionalVariable));
226
227 ErrorExit:
228 return Result;
229 }
230
231 int LxtSynchronizationEventWait(PLXT_SYNCHRONIZATION_EVENT Event)
232
233 /*++
234
235 Routine Description:
236
237 This routine blocks on event until it is signalled.
238
239 Arguments:
240
241 Event - Supplies a pointer to synchronization event.
242
243 Return Value:
244
245 0 on success, -1 on failure.
246
247 --*/
248
249 {
250 int Result = LXT_RESULT_FAILURE;
251
252 LxtCheckResult(pthread_mutex_lock(&Event->Lock));
253 while ((Event->Ready == 0) && (Event->Fail == 0))
254 {
255 LxtCheckResult(pthread_cond_wait(&Event->WaitConditionalVariable, &Event->Lock));
256 }
257
258 if (Event->Fail != 0)
259 {
260 LxtCheckResult(pthread_mutex_unlock(&Event->Lock));
261 Result = LXT_RESULT_FAILURE;
262 goto ErrorExit;
263 }
264
265 LxtCheckResult(pthread_mutex_unlock(&Event->Lock));
266
267 ErrorExit:
268 return Result;
269 }