-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConditionVariable.cs
559 lines (514 loc) · 23.7 KB
/
ConditionVariable.cs
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
Copyright (c) 2015 - 2018, Code Ex Machina, LLC.
All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Threading;
namespace CodeExMachina
{
/// <summary>
/// Condition variables are synchronization primitives that enable threads to
/// wait until a particular condition occurs. Condition variables are user-mode
/// objects that cannot be shared across processes.
///
/// Condition variables enable threads to release a lock and enter the waiting state.
///
/// Condition variables support operations that "wake one" or "wake all" waiting
/// threads. After a thread is woken, it re-acquires the lock it released when the
/// thread entered the waiting state.
/// </summary>
public class ConditionVariable : IDisposable
{
private int _waiters = 0;
private Object _waiters_lock = new object();
private SemaphoreSlim _sema = new SemaphoreSlim(0, Int32.MaxValue);
private bool _was_pulse_all = false;
private AutoResetEvent _waiters_done = new AutoResetEvent(false);
private bool _is_disposed = false;
/// <summary>
/// Initializes a new instance of the ConditionVariable class.
/// </summary>
public ConditionVariable()
{ }
/// <summary>
/// Wakes all threads waiting on this condition variable.
/// </summary>
/// <exception cref="System.ObjectDisposedException">
/// The current instance has already been disposed.
/// </exception>
/// <remarks>The PulseAll wakes all waiting threads while the Pulse wakes
/// only a single thread. Waking one thread is similar to setting an auto-reset
/// event, while waking all threads is similar to pulsing a manual reset event
/// but more reliable.
///
/// NOTE: The critical section lock must be held before this call is made.
/// </remarks>
/// <example>
/// This examples shows how to call PulseAll.
/// <code>
/// ConditionVariable cond = new ConditionVariable();
/// object cs = new object();
///
/// lock(cs)
/// {
/// cond.PulseAll();
/// }
/// </code>
/// </example>
public void PulseAll()
{
CheckDisposed();
bool have_waiters = false;
lock (_waiters_lock)
{
if (_waiters > 0)
{
// broadcasting even if there is just one waiter
_was_pulse_all = have_waiters = true;
}
if (have_waiters)
{
// wake up all the waiters
_sema.Release(_waiters);
}
}
if(have_waiters)
{
// wait for all woken threads to acquire their part of semaphore.
_waiters_done.WaitOne();
_was_pulse_all = false;
}
}
/// <summary>
/// Wakes a single thread waiting on this condition variable.
/// </summary>
/// <exception cref="System.ObjectDisposedException">
/// The current instance has already been disposed.
/// </exception>
/// <remarks>The PulseAll wakes all waiting threads while the Pulse wakes
/// only a single thread. Waking one thread is similar to setting an auto-reset
/// event, while waking all threads is similar to pulsing a manual reset event
/// but more reliable.
///
/// NOTE: The critical section lock must be held before this call is made.
/// </remarks>
/// <example>
/// This examples shows how to call Pulse.
/// <code>
/// ConditionVariable cond = new ConditionVariable();
/// object cs = new object();
///
/// lock(cs)
/// {
/// cond.Pulse();
/// }
/// </code>
/// </example>
public void Pulse()
{
CheckDisposed();
bool have_waiters;
lock (_waiters_lock)
{
have_waiters = _waiters > 0;
}
if (have_waiters)
_sema.Release();
}
/// <summary>
/// Waits on this condition variable and releases the specified critical section.
/// </summary>
/// <param name="obj">The critical section to release.</param>
/// <exception cref="System.ObjectDisposedException">
/// The current instance has already been disposed.
/// </exception>
/// <exception cref="System.Threading.SynchronizationLockException">
/// The critical section is not owned by the caller at the time this method is called.
/// </exception>
/// <remarks>
/// A thread that is waiting on a condition variable can be woken before the
/// a time-out interval has elapsed using the Pulse or PulseAll function.
///
/// In this case, the thread wakes when the wake processing is complete, and not
/// when its time-out interval elapses. After the thread is woken, it re-acquires
/// the critical section it released when the thread entered the waiting state.
///
/// Condition variables are subject to spurious wakeups (those not associated with
/// an explicit wake) and stolen wakeups (another thread manages to run before the
/// woken thread). Therefore, you should recheck a predicate (typically in a while
/// loop) after a wait operation returns.
/// </remarks>
/// <example>
/// This examples shows how to call Wait.
/// <code>
/// bool empty = true;
/// ConditionVariable cond = new ConditionVariable();
/// object obj = new object();
///
/// lock(obj)
/// {
/// while(empty)
/// {
/// cond.Wait(obj);
/// }
/// }
/// </code>
/// </example>
public void Wait(object obj)
{
Wait_i(obj, Timeout.Infinite, CancellationToken.None);
}
/// <summary>
/// Waits on this condition variable and releases the specified critical section
/// while observing a cancellation token.
/// </summary>
/// <param name="obj">The critical section to release.</param>
/// <param name="token">The CancellationToken token to observe.</param>
/// <exception cref="System.ObjectDisposedException">
/// The current instance has already been disposed.
/// </exception>
/// <exception cref="System.Threading.SynchronizationLockException">
/// The critical section is not owned by the caller at the time this method is called.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The specified token was cancelled.
/// </exception>
/// <remarks>
/// A thread that is waiting on a condition variable can be woken before the
/// a time-out interval has elapsed using the Pulse or PulseAll function.
/// In this case, the thread wakes when the wake processing is complete, and not
/// when its time-out interval elapses. After the thread is woken, it re-acquires
/// the critical section it released when the thread entered the waiting state.
///
/// Condition variables are subject to spurious wakeups (those not associated with
/// an explicit wake) and stolen wakeups (another thread manages to run before the
/// woken thread). Therefore, you should recheck a predicate (typically in a while
/// loop) after a wait operation returns.
///
/// If the token is cancelled, the method throws a OperationCancelledException.
/// </remarks>
/// <example>
/// This examples shows how to call Wait using a CancellationToken.
/// <code>
/// bool empty = true;
/// ConditionVariable cond = new ConditionVariable();
/// object obj = new object();
/// CancellationTokenSource cts = new CancellationTokenSource();
///
/// lock(obj)
/// {
/// while(empty)
/// {
/// cond.Wait(obj, cts.Token);
/// }
/// }
/// </code>
/// </example>
public void Wait(object obj, CancellationToken token)
{
Wait_i(obj, Timeout.Infinite, token);
}
/// <summary>
/// Waits on this condition variable for a specified time interval and releases the specified critical section.
/// </summary>
/// <param name="obj">The critical section to release.</param>
/// <param name="millisecondsTimeout">The number of milliseconds to wait, or Infinite(-1) to wait indefinitely.</param>
/// <returns>True if condition variable was successfully waited on. Or false if time out occurs while waiting for condition variable.</returns>
/// <exception cref="System.ObjectDisposedException">
/// The current instance has already been disposed.
/// </exception>
/// <exception cref="System.Threading.SynchronizationLockException">
/// The critical section is not owned by the caller at the time this method is called.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.
/// </exception>
/// <remarks>
/// A thread that is waiting on a condition variable can be woken before the
/// a time-out interval has elapsed using the Pulse or PulseAll function.
/// In this case, the thread wakes when the wake processing is complete, and not
/// when its time-out interval elapses. After the thread is woken, it re-acquires
/// the critical section it released when the thread entered the waiting state.
///
/// Condition variables are subject to spurious wakeups (those not associated with
/// an explicit wake) and stolen wakeups (another thread manages to run before the
/// woken thread). Therefore, you should recheck a predicate (typically in a while
/// loop) after a wait operation returns.
/// </remarks>
/// <example>
/// This examples shows how to call Wait using a time out.
/// <code>
/// bool empty = true;
/// ConditionVariable cond = new ConditionVariable();
/// object obj = new object();
///
/// lock(obj)
/// {
/// while(empty)
/// {
/// bool timed_out = !cond.Wait(obj, 100);
/// }
/// }
/// </code>
/// </example>
public bool Wait(object obj, int millisecondsTimeout)
{
ValidateMillisecondsTimeout(millisecondsTimeout);
return Wait_i(obj, millisecondsTimeout, CancellationToken.None);
}
/// <summary>
/// Waits on this condition variable for a specified time interval and releases the specified critical section
/// while observing a cancellation token.
/// </summary>
/// <param name="obj">The critical section to release.</param>
/// <param name="millisecondsTimeout">The number of milliseconds to wait, or Infinite(-1) to wait indefinitely.</param>
/// <param name="token">The CancellationToken token to observe.</param>
/// <returns>True if condition variable was successfully waited on. Or false if time out occurs while waiting for condition variable.</returns>
/// <exception cref="System.ObjectDisposedException">
/// The current instance has already been disposed.
/// </exception>
/// <exception cref="System.Threading.SynchronizationLockException">
/// The critical section is not owned by the caller at the time this method is called.
/// </exception>
/// <exception cref="System.ArgumentOutOfRangeException">
/// millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The specified token was cancelled.
/// </exception>
/// <remarks>
/// A thread that is waiting on a condition variable can be woken before the
/// a time-out interval has elapsed using the Pulse or PulseAll function.
/// In this case, the thread wakes when the wake processing is complete, and not
/// when its time-out interval elapses. After the thread is woken, it re-acquires
/// the critical section it released when the thread entered the waiting state.
///
/// Condition variables are subject to spurious wakeups (those not associated with
/// an explicit wake) and stolen wakeups (another thread manages to run before the
/// woken thread). Therefore, you should recheck a predicate (typically in a while
/// loop) after a wait operation returns.
///
/// If the token is cancelled, the method throws a OperationCancelledException.
/// </remarks>
/// <example>
/// This examples shows how to call Wait using a time out and cancellation token.
/// <code>
/// bool empty = true;
/// ConditionVariable cond = new ConditionVariable();
/// object obj = new object();
/// CancellationTokenSource cts = new CancellationTokenSource();
///
/// lock(obj)
/// {
/// while(empty)
/// {
/// bool timed_out = !cond.Wait(obj, 100, cts.Token);
/// }
/// }
/// </code>
/// </example>
public bool Wait(object obj, int millisecondsTimeout, CancellationToken token)
{
ValidateMillisecondsTimeout(millisecondsTimeout);
return Wait_i(obj, millisecondsTimeout, token);
}
/// <summary>
/// Waits on this condition variable using a TimeSpan to specify the time interval and releases the specified critical section.
/// </summary>
/// <param name="obj">The critical section to release.</param>
/// <param name="timeout">A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.</param>
/// <returns>True if condition variable was successfully waited on. Or false if time out occurs while waiting for condition variable.</returns>
/// <exception cref="System.ObjectDisposedException">
/// The current instance has already been disposed.
/// </exception>
/// <exception cref="System.Threading.SynchronizationLockException">
/// The critical section is not owned by the caller at the time this method is called.
/// </exception>
/// <remarks>
/// A thread that is waiting on a condition variable can be woken before the
/// a time-out interval has elapsed using the Pulse or PulseAll function.
/// In this case, the thread wakes when the wake processing is complete, and not
/// when its time-out interval elapses. After the thread is woken, it re-acquires
/// the critical section it released when the thread entered the waiting state.
///
/// Condition variables are subject to spurious wakeups (those not associated with
/// an explicit wake) and stolen wakeups (another thread manages to run before the
/// woken thread). Therefore, you should recheck a predicate (typically in a while
/// loop) after a wait operation returns.
/// </remarks>
/// <example>
/// This examples shows how to call Wait using a TimeSpan.
/// <code>
/// bool empty = true;
/// ConditionVariable cond = new ConditionVariable();
/// object obj = new object();
///
/// lock(obj)
/// {
/// while(empty)
/// {
/// bool timed_out = !cond.Wait(obj, TimeSpan.FromMilliseconds(100));
/// }
/// }
/// </code>
/// </example>
public bool Wait(object obj, TimeSpan timeout)
{
ValidateTimeout(timeout);
return Wait_i(obj, (int)timeout.TotalMilliseconds, CancellationToken.None);
}
/// <summary>
/// Waits on this condition variable using a TimeSpan to specify the time interval and releases the specified critical section
/// while observing a cancellation token.
/// </summary>
/// <param name="obj">The critical section to release.</param>
/// <param name="timeout">A TimeSpan that represents the number of milliseconds to wait, or a TimeSpan that represents -1 milliseconds to wait indefinitely.</param>
/// <param name="token">The CancellationToken token to observe.</param>
/// <returns>True if condition variable was successfully waited on. Or false if time out occurs while waiting for condition variable.</returns>
/// <exception cref="System.ObjectDisposedException">
/// The current instance has already been disposed.
/// </exception>
/// <exception cref="System.Threading.SynchronizationLockException">
/// The critical section is not owned by the caller at the time this method is called.
/// </exception>
/// <exception cref="System.OperationCanceledException">
/// The specified token was cancelled.
/// </exception>
/// <remarks>
/// A thread that is waiting on a condition variable can be woken before the
/// a time-out interval has elapsed using the Pulse or PulseAll function.
/// In this case, the thread wakes when the wake processing is complete, and not
/// when its time-out interval elapses. After the thread is woken, it re-acquires
/// the critical section it released when the thread entered the waiting state.
///
/// Condition variables are subject to spurious wakeups (those not associated with
/// an explicit wake) and stolen wakeups (another thread manages to run before the
/// woken thread). Therefore, you should recheck a predicate (typically in a while
/// loop) after a wait operation returns.
///
/// If the token is cancelled, the method throws a OperationCancelledException.
/// </remarks>
/// <example>
/// This examples shows how to call Wait using a TimeSpan and CancellationToken.
/// <code>
/// bool empty = true;
/// ConditionVariable cond = new ConditionVariable();
/// object obj = new object();
/// CancellationTokenSource cts = new CancellationTokenSource();
///
/// lock(obj)
/// {
/// while(empty)
/// {
/// bool timed_out = !cond.Wait(obj, TimeSpan.FromMilliseconds(100), cts.Token);
/// }
/// }
/// </code>
/// </example>
public bool Wait(object obj, TimeSpan timeout, CancellationToken token)
{
ValidateTimeout(timeout);
return Wait_i(obj, (int)timeout.TotalMilliseconds, token);
}
/// <summary>
/// Releases all resources used by the current instance of the ConditionVariable class.
/// </summary>
/// <remarks>
/// Call Dispose when you are finished using the ConditionVariable. The Dispose method
/// leaves the ConditionVariable in an unusable state. After calling Dispose, you must
/// release all references to the ConditionVariable so the garbage collector can reclaim
/// the memory that the ConditionVariable was occupying.
/// </remarks>
public void Dispose()
{
Dispose(true);
//GC.SuppressFinalize(this);
}
#region Private
private bool Wait_i(object obj, int timeout, CancellationToken token)
{
CheckDisposed();
if (obj == null)
throw new ArgumentNullException("obj");
bool reacquire = true, success = false;
try
{
lock (_waiters_lock)
{
++_waiters;
}
Monitor.Exit(obj);
success = _sema.Wait(timeout, token);
}
catch(SynchronizationLockException)
{
// don't "reacquire" lock if did not own it in first place!
reacquire = false;
throw;
}
finally
{
bool last_waiter;
lock (_waiters_lock)
{
--_waiters;
last_waiter = _was_pulse_all && _waiters == 0;
}
// signal broadcaster if the last waiter
if (last_waiter)
_waiters_done.Set();
// reacquire the lock on exit
if(reacquire)
Monitor.Enter(obj);
}
return success;
}
protected virtual void Dispose(bool disposing)
{
if (!_is_disposed)
{
if (disposing)
{
_sema.Dispose();
_waiters_done.Dispose();
}
_sema = null;
_waiters_done = null;
_is_disposed = true;
}
}
private static void ValidateTimeout(TimeSpan timeout)
{
long total_milliseconds = (long)timeout.TotalMilliseconds;
if ((total_milliseconds < 0 || total_milliseconds > Int32.MaxValue) && (total_milliseconds != Timeout.Infinite))
{
throw new ArgumentOutOfRangeException("timeout");
}
}
private static void ValidateMillisecondsTimeout(int millisecondsTimeout)
{
if ((millisecondsTimeout < 0) && (millisecondsTimeout != Timeout.Infinite))
{
throw new ArgumentOutOfRangeException("millisecondsTimeout");
}
}
private void CheckDisposed()
{
if (_is_disposed)
{
throw new ObjectDisposedException("ConditionVariable");
}
}
#endregion
}
}