forked from neosmart/pevents
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pevents.cpp
361 lines (307 loc) · 7.7 KB
/
pevents.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* WIN32 Events for POSIX
* Author: Mahmoud Al-Qudsi <[email protected]>
* Copyright (C) 2011 - 2012 by NeoSmart Technologies
* This code is released under the terms of the MIT License
*/
#include "pevents.h"
#include <assert.h>
#include <sys/time.h>
#ifdef WFMO
#include <vector>
#endif
namespace neosmart
{
#ifdef WFMO
struct neosmart_wfmo_t_
{
pthread_mutex_t Mutex;
pthread_cond_t CVariable;
std::vector<bool> EventStatus;
bool StillWaiting;
int RefCount;
bool WaitAll;
void Destroy()
{
pthread_mutex_destroy(&Mutex);
pthread_cond_destroy(&CVariable);
}
};
typedef neosmart_wfmo_t_ *neosmart_wfmo_t;
struct neosmart_wfmo_info_t_
{
neosmart_wfmo_t Waiter;
int WaitIndex;
};
typedef neosmart_wfmo_info_t_ *neosmart_wfmo_info_t;
#endif
struct neosmart_event_t_
{
bool AutoReset;
pthread_cond_t CVariable;
pthread_mutex_t Mutex;
bool State;
#ifdef WFMO
std::vector<neosmart_wfmo_info_t_> RegisteredWaits;
#endif
};
neosmart_event_t CreateEvent(bool manualReset, bool initialState)
{
neosmart_event_t event = new neosmart_event_t_;
int result = pthread_cond_init(&event->CVariable, 0);
if(result != 0)
return NULL;
result = pthread_mutex_init(&event->Mutex, 0);
if(result != 0)
return NULL;
event->State = false;
event->AutoReset = !manualReset;
if(initialState && SetEvent(event) != 0)
return NULL; //Shouldn't ever happen
return event;
}
int WaitForEvent(neosmart_event_t event, uint64_t milliseconds)
{
int result = pthread_mutex_lock(&event->Mutex);
if(result != 0)
return result;
if(!event->State)
{
timespec ts;
if(milliseconds != -1)
{
timeval tv;
gettimeofday(&tv, NULL);
uint64_t nanoseconds = ((uint64_t) tv.tv_sec) * 1000 * 1000 * 1000 + milliseconds * 1000 * 1000 + ((uint64_t) tv.tv_usec) * 1000;
ts.tv_sec = nanoseconds / 1000 / 1000 / 1000;
ts.tv_nsec = (nanoseconds - ((uint64_t) ts.tv_sec) * 1000 * 1000 * 1000);
}
do
{
//Regardless of whether it's an auto-reset or manual-reset event:
//wait to obtain the event, then lock anyone else out
if(milliseconds != -1)
{
result = pthread_cond_timedwait(&event->CVariable, &event->Mutex, &ts);
}
else
{
result = pthread_cond_wait(&event->CVariable, &event->Mutex);
}
} while(result == 0 && !event->State);
if(result == 0 && event->AutoReset)
{
//We've only accquired the event if the wait succeeded
event->State = false;
}
}
else if(event->AutoReset)
{
//It's an auto-reset event that's currently available;
//we need to stop anyone else from using it
result = 0;
event->State = false;
}
//Else we're trying to obtain a manual reset event with a signalled state;
//don't do anything
pthread_mutex_unlock(&event->Mutex);
return result;
}
#ifdef WFMO
int WaitForMultipleEvents(neosmart_event_t *events, int count, bool waitAll, uint64_t milliseconds, int &waitIndex)
{
neosmart_wfmo_t wfmo = new neosmart_wfmo_t_;
int result = pthread_mutex_init(&wfmo->Mutex, 0);
if(result != 0)
return result;
result = pthread_cond_init(&wfmo->CVariable, 0);
if(result != 0)
return result;
neosmart_wfmo_info_t_ waitInfo;
waitInfo.Waiter = wfmo;
waitInfo.WaitIndex = -1;
wfmo->WaitAll = waitAll;
wfmo->StillWaiting = true;
wfmo->RefCount = 1;
wfmo->EventStatus.resize(count, false);
pthread_mutex_lock(&wfmo->Mutex);
bool done = false;
waitIndex = -1;
for(int i = 0; i < count; ++i)
{
waitInfo.WaitIndex = i;
if(WaitForEvent(events[i], 0) == 0)
{
wfmo->EventStatus[i] = true;
if(!waitAll)
{
waitIndex = i;
done = true;
break;
}
}
else
{
int result = pthread_mutex_lock(&events[i]->Mutex);
if(result != 0)
return result;
events[i]->RegisteredWaits.push_back(waitInfo);
++wfmo->RefCount;
pthread_mutex_unlock(&events[i]->Mutex);
}
}
timespec ts;
if(!done && milliseconds != -1)
{
timeval tv;
gettimeofday(&tv, NULL);
uint64_t nanoseconds = ((uint64_t) tv.tv_sec) * 1000 * 1000 * 1000 + milliseconds * 1000 * 1000 + ((uint64_t) tv.tv_usec) * 1000;
ts.tv_sec = nanoseconds / 1000 / 1000 / 1000;
ts.tv_nsec = (nanoseconds - ((uint64_t) ts.tv_sec) * 1000 * 1000 * 1000);
}
while(!done)
{
//One (or more) of the events we're monitoring has been triggered?
//If we're waiting for all events, assume we're done and check if there's an event that hasn't fired
//But if we're waiting for just one event, assume we're not done until we find a fired event
done = waitAll;
for(int i = 0; i < count; ++i)
{
if(!waitAll && wfmo->EventStatus[i])
{
done = true;
waitIndex = i;
break;
}
if(waitAll && !wfmo->EventStatus[i])
{
done = false;
break;
}
}
if(!done)
{
if(milliseconds != -1)
{
result = pthread_cond_timedwait(&wfmo->CVariable, &wfmo->Mutex, &ts);
}
else
{
result = pthread_cond_wait(&wfmo->CVariable, &wfmo->Mutex);
}
if(result != 0)
break;
}
}
wfmo->StillWaiting = false;
--wfmo->RefCount;
if(wfmo->RefCount == 0)
{
wfmo->Destroy();
delete wfmo;
}
else
{
pthread_mutex_unlock(&wfmo->Mutex);
}
return result;
}
#endif
int DestroyEvent(neosmart_event_t event)
{
int result = pthread_cond_destroy(&event->CVariable);
if(result != 0)
return result;
result = pthread_mutex_destroy(&event->Mutex);
if(result != 0)
return result;
delete event;
return 0;
}
int SetEvent(neosmart_event_t event)
{
int result = pthread_mutex_lock(&event->Mutex);
if(result != 0)
return result;
event->State = true;
//Depending on the event type, we either trigger everyone or only one
if(event->AutoReset)
{
#ifdef WFMO
while(!event->RegisteredWaits.empty())
{
neosmart_wfmo_info_t i = &event->RegisteredWaits.back();
pthread_mutex_lock(&i->Waiter->Mutex);
--i->Waiter->RefCount;
if(!i->Waiter->StillWaiting)
{
if(i->Waiter->RefCount == 0)
{
i->Waiter->Destroy();
delete i->Waiter;
}
else
{
pthread_mutex_unlock(&i->Waiter->Mutex);
}
event->RegisteredWaits.pop_back();
continue;
}
event->State = false;
i->Waiter->EventStatus[i->WaitIndex] = true;
if(!i->Waiter->WaitAll)
i->Waiter->StillWaiting = false;
result = pthread_cond_signal(&i->Waiter->CVariable);
pthread_mutex_unlock(&i->Waiter->Mutex);
event->RegisteredWaits.pop_back();
break;
}
#endif
//event->State can be false if compiled with WFMO support
if(event->State)
{
result = pthread_cond_signal(&event->CVariable);
}
}
else
{
#ifdef WFMO
for(int i = 0; i < event->RegisteredWaits.size(); ++i)
{
neosmart_wfmo_info_t info = &event->RegisteredWaits[i];
pthread_mutex_lock(&info->Waiter->Mutex);
--info->Waiter->RefCount;
if(!info->Waiter->StillWaiting)
{
if(info->Waiter->RefCount == 0)
{
info->Waiter->Destroy();
delete info->Waiter;
}
else
{
pthread_mutex_unlock(&info->Waiter->Mutex);
}
continue;
}
info->Waiter->EventStatus[info->WaitIndex] = true;
pthread_cond_signal(&info->Waiter->CVariable);
pthread_mutex_unlock(&info->Waiter->Mutex);
}
event->RegisteredWaits.clear();
#endif
result = pthread_cond_broadcast(&event->CVariable);
}
pthread_mutex_unlock(&event->Mutex);
return result;
}
int ResetEvent(neosmart_event_t event)
{
int result = pthread_mutex_lock(&event->Mutex);
if(result != 0)
return result;
event->State = false;
pthread_mutex_unlock(&event->Mutex);
return result;
}
}