-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaitSet.cpp
493 lines (390 loc) · 11.2 KB
/
WaitSet.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
/*
* WaitSet.cpp
*
* Created on: Apr 12, 2012
* Author: knuthelv
*/
#include "RxConcurrent/WaitSet.h"
namespace BaseLib { namespace Concurrent {
/*----------------------------------------------------
Base class for Condition implementations
----------------------------------------------------*/
WaitSet::WaitSet()
: valid_(true)
{ }
/**
* TODO: Potential deadlock in Stop()?
*/
WaitSet::~WaitSet()
{
Stop();
}
WaitSet::Ptr WaitSet::GetPtr()
{
return shared_from_this();
}
/*----------------------------------------------------
avoid threads waiting forever
----------------------------------------------------*/
void WaitSet::Stop()
{
MutexLocker lock(&mutex_);
valid_ = false;
waitCondition_.wakeAll();
}
void WaitSet::Start()
{
MutexLocker lock(&mutex_);
valid_ = true;
waitCondition_.wakeAll();
}
void WaitSet::WakeAll() const
{
MutexLocker lock(&mutex_);
waitCondition_.wakeAll();
}
bool WaitSet::IsValid() const
{
MutexLocker lock(&mutex_);
return isValid();
}
bool WaitSet::isValid() const
{
return valid_;
}
/*----------------------------------------------------
Wait for triggered conditions
- NB! Must be used with GetTriggeredConditions()
to retrieve the triggered conditions.
----------------------------------------------------*/
bool WaitSet::Wait(int64 milliseconds)
{
MutexLocker lock(&mutex_);
if(!isValid()) return false;
return waitForTriggeredConditions(milliseconds);
}
/*----------------------------------------------------
Wait for triggered conditions and Dispatch when woken up.
The dispatch uses template typename Functor (function pointer/holder)
and calls operator()() in class.
----------------------------------------------------*/
bool WaitSet::WaitDispatch(int64 milliseconds)
{
{
MutexLocker lock(&mutex_);
if(!isValid()) return false;
waitForTriggeredConditions(milliseconds);
}
return DispatchTriggeredConditions();
}
bool WaitSet::WaitDispatchNext(int64 milliseconds)
{
AbstractCondition::Ptr cond;
{
MutexLocker lock(&mutex_);
if(!isValid()) return false;
waitForTriggeredConditions(milliseconds);
cond = dequeueTriggered();
}
return WaitSet::DispatchTriggeredCondition(cond);
}
AbstractCondition::Ptr WaitSet::WaitGetNext(int64 milliseconds)
{
ElapsedTimer timer(milliseconds);
ASSERT(!timer.HasExpired());
while(true)
{
MutexLocker lock(&mutex_);
waitForTriggeredConditions(timer.Remaining().InMillis());
AbstractCondition::Ptr cond = dequeueTriggered();
if(cond != nullptr)
{
return cond->GetPtr();
}
if(timer.HasExpired()) break;
if(!isValid()) break;
}
return AbstractCondition::Ptr();
}
bool WaitSet::WaitDispatch(AbstractCondition::Ptr condition, int64 milliseconds)
{
// Is valid?
{
MutexLocker lock(&mutex_);
if(!isValid()) return false;
}
// Wait for triggered condition
ElapsedTimer timer(milliseconds);
while(!condition->GetTriggerValue() && !timer.HasExpired())
{
MutexLocker lock(&mutex_);
bool isWoken = waitCondition_.wait(&mutex_, timer.Remaining().InMillis());
if(isWoken)
{
break;
}
else if(!isValid())
{
break;
}
}
return WaitSet::DispatchTriggeredCondition(condition);
}
/*----------------------------------------------------
Signal thread waiting in Wait or WaitDispatch
Example usage: GuardCondition->SetTriggerValue()
calls WaitSet::Signal if it is attached
----------------------------------------------------*/
void WaitSet::Signal(AbstractCondition::Ptr condition)
{
MutexLocker lock(&mutex_);
if(!isValid()) return;
ConditionSet::iterator it = conditions_.find(condition);
// -- insert --
if(it != conditions_.end())
{
enqueueTriggered(condition);
waitCondition_.wakeOne();
// -- debug --
// It is possible that a condition is triggered twice before WaitSet thread gets to run!
// ASSERT(inserted.second == true);
// -- debug --
}
else // condition not attached!
{
// -- debug --
ASSERT(it != conditions_.end());
// -- debug --
}
}
/*----------------------------------------------------
- Attach Condition to this WaitSet.
- Behind the scenes attach this Waitset to the Condition to
allow it to wakeup this WaitSet upon trigger.
----------------------------------------------------*/
bool WaitSet::Attach(AbstractCondition::Ptr condition)
{
bool isAttached = true;
// -----------------------------------
// attach wait-set to condition
// -----------------------------------
{
isAttached = condition->Attach(this);
if(!isAttached)
{
// -- debug --
IWARNING() << "Already attached";
// -- debug --
return false;
}
}
bool isTriggeredCondition = condition->GetTriggerValue();
// -----------------------------------
// insert condition to set
// -----------------------------------
{
MutexLocker lock(&mutex_);
std::pair<ConditionSet::iterator, bool> inserted = conditions_.insert(condition->GetPtr());
// -- debug --
ASSERT(inserted.second);
// -- debug --
if(isTriggeredCondition)
{
enqueueTriggered(condition);
waitCondition_.wakeOne();
}
}
// -- debug --
ASSERT(isAttached);
// -- debug --
return isAttached;
}
/*----------------------------------------------------
- Detach the Condition from the WaitSet
----------------------------------------------------*/
bool WaitSet::Detach(AbstractCondition::Ptr condition)
{
// ---------------------------
// Erase condition from set
// ---------------------------
{
MutexLocker lock(&mutex_);
conditions_.erase(condition);
triggeredConditions_.erase(condition);
triggeredQueue_.remove(condition);
}
condition->SetTriggerValue(false);
bool isDetached = condition->Detach(this);
if(!isDetached)
{
// -- debug --
IWARNING() << "Already detached";
// -- debug --
return false;
}
return isDetached;
}
/*----------------------------------------------------
Return a copy of attached conditions
----------------------------------------------------*/
ConditionSet WaitSet::GetConditions() const
{
MutexLocker lock(&mutex_);
return conditions_;
}
/*----------------------------------------------------
Return all triggered conditions
----------------------------------------------------*/
ConditionSet WaitSet::GetTriggeredConditions() const
{
ConditionSet conditions;
{
MutexLocker lock(&mutex_);
conditions = conditions_;
}
return WaitSet::getTriggeredConditions(conditions);
}
/*----------------------------------------------------
Dispatch all triggered conditions
----------------------------------------------------*/
bool WaitSet::DispatchTriggeredConditions()
{
bool dispatchedCondition = false;
while(true)
{
AbstractCondition::Ptr condition;
{
MutexLocker lock(&mutex_);
condition = dequeueTriggered();
}
if(!condition) break;
dispatchedCondition = WaitSet::DispatchTriggeredCondition(condition);
}
return dispatchedCondition;
}
/*----------------------------------------------------
Clears the queue of triggered conditions
----------------------------------------------------*/
void WaitSet::ClearTriggeredQueue()
{
MutexLocker lock(&mutex_);
clearTriggeredQueue();
}
/*----------------------------------------------------
private functions that are not thread-safe
----------------------------------------------------*/
bool WaitSet::enqueueTriggered(AbstractCondition::Ptr cond)
{
bool isEnqueued = false;
// ----------------------------------------
// If already triggered then return false
// ------------------------------------------
if(triggeredConditions_.find(cond) != triggeredConditions_.end())
{
isEnqueued = false;
}
else
{
triggeredQueue_.push_back(cond->GetPtr());
triggeredConditions_.insert(cond->GetPtr());
isEnqueued = true;
}
// -- debug --
ASSERT(triggeredQueue_.size() <= conditions_.size());
ASSERT(triggeredQueue_.size() == triggeredConditions_.size());
// -- debug --
return isEnqueued;
}
/**
* @brief WaitSet::dequeueTriggered returns a triggered condition.
*
* Note! It is allowed that a trigger value may change, but no need checking.
*
* NB! It is possible that the condition was triggered and then un-triggered,
* therefore, the implementation should handle these cases.
*
* NB! Calling cond->GetTriggerValue() may cause a deadlock between condition
* and WaitSet so avoid calling any mutexed function on condition inside mutexed function in WaitSet!
*
* @return
*/
AbstractCondition::Ptr WaitSet::dequeueTriggered()
{
AbstractCondition::Ptr cond;
while(!triggeredQueue_.empty())
{
cond = triggeredQueue_.front();
triggeredQueue_.pop_front();
triggeredConditions_.erase(cond);
if(!cond)
{
// -- debug --
ICRITICAL() << "Condition was NULL!";
ASSERT(cond);
// -- debug --
continue;
}
break;
}
// -- debug --
ASSERT(triggeredQueue_.size() == triggeredConditions_.size());
// -- debug --
return cond;
}
void WaitSet::clearTriggeredQueue()
{
triggeredQueue_.clear();
triggeredConditions_.clear();
}
bool WaitSet::waitForTriggeredConditions(int64 milliseconds)
{
ElapsedTimer timer(milliseconds);
// ---------------------------------------------
// wait if no triggered conditions
// wait if no conditions at all
// ---------------------------------------------
while(triggeredQueue_.empty() && !timer.HasExpired())
{
bool isWoken = waitCondition_.wait(&mutex_, timer.Remaining().InMillis());
if(isWoken)
{
break;
}
else if(!isValid())
{
break;
}
}
return !triggeredQueue_.empty();
}
bool WaitSet::DispatchTriggeredCondition(AbstractCondition::Ptr condition)
{
if(!condition) return false;
if(condition->GetTriggerValue())
{
condition->Dispatch(); // resets trigger to false!
return true;
}
return false;
}
ConditionSet WaitSet::getTriggeredConditions(const ConditionSet& conditions)
{
ConditionSet triggeredConditions;
std::pair<ConditionSet::iterator, bool> inserted;
for(ConditionSet::const_iterator it = conditions.begin(), it_end = conditions.end(); it != it_end; ++it)
{
AbstractCondition::Ptr condition = *it;
// -- debug --
ASSERT(condition);
// -- debug --
if(condition->GetTriggerValue())
{
inserted = triggeredConditions.insert(condition->GetPtr());
// -- debug --
ASSERT(inserted.second);
// -- debug --
}
}
return triggeredConditions;
}
}}