forked from neosmart/pevents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpevents.cpp
545 lines (453 loc) · 12.5 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
* WIN32 Events for POSIX
* Author: Mahmoud Al-Qudsi <[email protected]>
* Copyright (C) 2011 - 2015 by NeoSmart Technologies
* This code is released under the terms of the MIT License
*/
#ifndef _WIN32
#include "pevents.h"
#include <assert.h>
#include <errno.h>
#include <sys/time.h>
#include <pthread.h>
#ifdef WFMO
#include <algorithm>
#include <deque>
#endif
namespace neosmart
{
#ifdef WFMO
struct neosmart_wfmo_t_
{
pthread_mutex_t Mutex;
pthread_cond_t CVariable;
int RefCount;
union
{
int FiredEvent; //WFSO
int EventsLeft; //WFMO
} Status;
bool WaitAll;
bool StillWaiting;
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_
{
pthread_cond_t CVariable;
pthread_mutex_t Mutex;
bool AutoReset;
bool State;
#ifdef WFMO
std::deque<neosmart_wfmo_info_t_> RegisteredWaits;
#endif
};
#ifdef WFMO
bool RemoveExpiredWaitHelper(neosmart_wfmo_info_t_ wait)
{
int result = pthread_mutex_trylock(&wait.Waiter->Mutex);
if (result == EBUSY)
{
return false;
}
assert(result == 0);
if (wait.Waiter->StillWaiting == false)
{
--wait.Waiter->RefCount;
assert(wait.Waiter->RefCount >= 0);
if (wait.Waiter->RefCount == 0)
{
wait.Waiter->Destroy();
delete wait.Waiter;
}
else
{
result = pthread_mutex_unlock(&wait.Waiter->Mutex);
assert(result == 0);
}
return true;
}
result = pthread_mutex_unlock(&wait.Waiter->Mutex);
assert(result == 0);
return false;
}
#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);
assert(result == 0);
result = pthread_mutex_init(&event->Mutex, 0);
assert(result == 0);
event->State = false;
event->AutoReset = !manualReset;
if (initialState)
{
result = SetEvent(event);
assert(result == 0);
}
return event;
}
int UnlockedWaitForEvent(neosmart_event_t event, uint64_t milliseconds)
{
int result = 0;
if (!event->State)
{
//Zero-timeout event state check optimization
if (milliseconds == 0)
{
return WAIT_TIMEOUT;
}
timespec ts;
if (milliseconds != (uint64_t) -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 != (uint64_t) -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 signaled state;
//don't do anything
return result;
}
int WaitForEvent(neosmart_event_t event, uint64_t milliseconds)
{
int tempResult;
if (milliseconds == 0)
{
tempResult = pthread_mutex_trylock(&event->Mutex);
if (tempResult == EBUSY)
{
return WAIT_TIMEOUT;
}
}
else
{
tempResult = pthread_mutex_lock(&event->Mutex);
}
assert(tempResult == 0);
int result = UnlockedWaitForEvent(event, milliseconds);
tempResult = pthread_mutex_unlock(&event->Mutex);
assert(tempResult == 0);
return result;
}
#ifdef WFMO
int WaitForMultipleEvents(neosmart_event_t *events, int count, bool waitAll, uint64_t milliseconds)
{
int unused;
return WaitForMultipleEvents(events, count, waitAll, milliseconds, unused);
}
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 = 0;
int tempResult = pthread_mutex_init(&wfmo->Mutex, 0);
assert(tempResult == 0);
tempResult = pthread_cond_init(&wfmo->CVariable, 0);
assert(tempResult == 0);
neosmart_wfmo_info_t_ waitInfo;
waitInfo.Waiter = wfmo;
waitInfo.WaitIndex = -1;
wfmo->WaitAll = waitAll;
wfmo->StillWaiting = true;
wfmo->RefCount = 1;
if (waitAll)
{
wfmo->Status.EventsLeft = count;
}
else
{
wfmo->Status.FiredEvent = -1;
}
tempResult = pthread_mutex_lock(&wfmo->Mutex);
assert(tempResult == 0);
bool done = false;
waitIndex = -1;
for (int i = 0; i < count; ++i)
{
waitInfo.WaitIndex = i;
//Must not release lock until RegisteredWait is potentially added
tempResult = pthread_mutex_lock(&events[i]->Mutex);
assert(tempResult == 0);
//Before adding this wait to the list of registered waits, let's clean up old, expired waits while we have the event lock anyway
events[i]->RegisteredWaits.erase(std::remove_if (events[i]->RegisteredWaits.begin(), events[i]->RegisteredWaits.end(), RemoveExpiredWaitHelper), events[i]->RegisteredWaits.end());
if (UnlockedWaitForEvent(events[i], 0) == 0)
{
tempResult = pthread_mutex_unlock(&events[i]->Mutex);
assert(tempResult == 0);
if (waitAll)
{
--wfmo->Status.EventsLeft;
assert(wfmo->Status.EventsLeft >= 0);
}
else
{
wfmo->Status.FiredEvent = i;
waitIndex = i;
done = true;
break;
}
}
else
{
events[i]->RegisteredWaits.push_back(waitInfo);
++wfmo->RefCount;
tempResult = pthread_mutex_unlock(&events[i]->Mutex);
assert(tempResult == 0);
}
}
timespec ts;
if (!done)
{
if (milliseconds == 0)
{
result = WAIT_TIMEOUT;
done = true;
}
else if (milliseconds != (uint64_t) -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 && wfmo->Status.EventsLeft == 0) || (!waitAll && wfmo->Status.FiredEvent != -1);
if (!done)
{
if (milliseconds != (uint64_t) -1)
{
result = pthread_cond_timedwait(&wfmo->CVariable, &wfmo->Mutex, &ts);
}
else
{
result = pthread_cond_wait(&wfmo->CVariable, &wfmo->Mutex);
}
if (result != 0)
{
break;
}
}
}
waitIndex = wfmo->Status.FiredEvent;
wfmo->StillWaiting = false;
--wfmo->RefCount;
assert(wfmo->RefCount >= 0);
if (wfmo->RefCount == 0)
{
wfmo->Destroy();
delete wfmo;
}
else
{
tempResult = pthread_mutex_unlock(&wfmo->Mutex);
assert(tempResult == 0);
}
return result;
}
#endif
int DestroyEvent(neosmart_event_t event)
{
int result = 0;
#ifdef WFMO
result = pthread_mutex_lock(&event->Mutex);
assert(result == 0);
event->RegisteredWaits.erase(std::remove_if (event->RegisteredWaits.begin(), event->RegisteredWaits.end(), RemoveExpiredWaitHelper), event->RegisteredWaits.end());
result = pthread_mutex_unlock(&event->Mutex);
assert(result == 0);
#endif
result = pthread_cond_destroy(&event->CVariable);
assert(result == 0);
result = pthread_mutex_destroy(&event->Mutex);
assert(result == 0);
delete event;
return 0;
}
int SetEvent(neosmart_event_t event)
{
int result = pthread_mutex_lock(&event->Mutex);
assert(result == 0);
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.front();
result = pthread_mutex_lock(&i->Waiter->Mutex);
assert(result == 0);
--i->Waiter->RefCount;
assert(i->Waiter->RefCount >= 0);
if (!i->Waiter->StillWaiting)
{
if (i->Waiter->RefCount == 0)
{
i->Waiter->Destroy();
delete i->Waiter;
}
else
{
result = pthread_mutex_unlock(&i->Waiter->Mutex);
assert(result == 0);
}
event->RegisteredWaits.pop_front();
continue;
}
event->State = false;
if (i->Waiter->WaitAll)
{
--i->Waiter->Status.EventsLeft;
assert(i->Waiter->Status.EventsLeft >= 0);
//We technically should do i->Waiter->StillWaiting = Waiter->Status.EventsLeft != 0
//but the only time it'll be equal to zero is if we're the last event, so no one
//else will be checking the StillWaiting flag. We're good to go without it.
}
else
{
i->Waiter->Status.FiredEvent = i->WaitIndex;
i->Waiter->StillWaiting = false;
}
result = pthread_mutex_unlock(&i->Waiter->Mutex);
assert(result == 0);
result = pthread_cond_signal(&i->Waiter->CVariable);
assert(result == 0);
event->RegisteredWaits.pop_front();
result = pthread_mutex_unlock(&event->Mutex);
assert(result == 0);
return 0;
}
#endif
//event->State can be false if compiled with WFMO support
if (event->State)
{
result = pthread_mutex_unlock(&event->Mutex);
assert(result == 0);
result = pthread_cond_signal(&event->CVariable);
assert(result == 0);
return 0;
}
}
else
{
#ifdef WFMO
for (size_t i = 0; i < event->RegisteredWaits.size(); ++i)
{
neosmart_wfmo_info_t info = &event->RegisteredWaits[i];
result = pthread_mutex_lock(&info->Waiter->Mutex);
assert(result == 0);
--info->Waiter->RefCount;
assert(info->Waiter->RefCount >= 0);
if (!info->Waiter->StillWaiting)
{
if (info->Waiter->RefCount == 0)
{
info->Waiter->Destroy();
delete info->Waiter;
}
else
{
result = pthread_mutex_unlock(&info->Waiter->Mutex);
assert(result == 0);
}
continue;
}
if (info->Waiter->WaitAll)
{
--info->Waiter->Status.EventsLeft;
assert(info->Waiter->Status.EventsLeft >= 0);
//We technically should do i->Waiter->StillWaiting = Waiter->Status.EventsLeft != 0
//but the only time it'll be equal to zero is if we're the last event, so no one
//else will be checking the StillWaiting flag. We're good to go without it.
}
else
{
info->Waiter->Status.FiredEvent = info->WaitIndex;
info->Waiter->StillWaiting = false;
}
result = pthread_mutex_unlock(&info->Waiter->Mutex);
assert(result == 0);
result = pthread_cond_signal(&info->Waiter->CVariable);
assert(result == 0);
}
event->RegisteredWaits.clear();
#endif
result = pthread_mutex_unlock(&event->Mutex);
assert(result == 0);
result = pthread_cond_broadcast(&event->CVariable);
assert(result == 0);
}
return 0;
}
int ResetEvent(neosmart_event_t event)
{
int result = pthread_mutex_lock(&event->Mutex);
assert(result == 0);
event->State = false;
result = pthread_mutex_unlock(&event->Mutex);
assert(result == 0);
return 0;
}
#ifdef PULSE
int PulseEvent(neosmart_event_t event)
{
//This may look like it's a horribly inefficient kludge with the sole intention of reducing code duplication,
//but in reality this is what any PulseEvent() implementation must look like. The only overhead (function
//calls aside, which your compiler will likely optimize away, anyway), is if only WFMO auto-reset waits are active
//there will be overhead to unnecessarily obtain the event mutex for ResetEvent() after. In all other cases (being
//no pending waits, WFMO manual-reset waits, or any WFSO waits), the event mutex must first be released for the
//waiting thread to resume action prior to locking the mutex again in order to set the event state to unsignaled,
//or else the waiting threads will loop back into a wait (due to checks for spurious CVariable wakeups).
int result = SetEvent(event);
assert(result == 0);
result = ResetEvent(event);
assert(result == 0);
return 0;
}
#endif
}
#endif //_WIN32