| 1 |
/** |
| 2 |
* The condition module provides a primitive for synchronized condition |
| 3 |
* checking. |
| 4 |
* |
| 5 |
* Copyright: Copyright Sean Kelly 2005 - 2009. |
| 6 |
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Authors: Sean Kelly |
| 8 |
* Source: $(DRUNTIMESRC core/sync/_condition.d) |
| 9 |
*/ |
| 10 |
|
| 11 |
/* Copyright Sean Kelly 2005 - 2009. |
| 12 |
* Distributed under the Boost Software License, Version 1.0. |
| 13 |
* (See accompanying file LICENSE or copy at |
| 14 |
* http://www.boost.org/LICENSE_1_0.txt) |
| 15 |
*/ |
| 16 |
module core.sync.condition; |
| 17 |
|
| 18 |
|
| 19 |
public import core.sync.exception; |
| 20 |
public import core.sync.mutex; |
| 21 |
public import core.time; |
| 22 |
|
| 23 |
import core.exception : AssertError, staticError; |
| 24 |
|
| 25 |
|
| 26 |
version (Windows) |
| 27 |
{ |
| 28 |
import core.sync.semaphore; |
| 29 |
import core.sys.windows.basetsd /+: HANDLE+/; |
| 30 |
import core.sys.windows.winbase /+: CloseHandle, CreateSemaphoreA, CRITICAL_SECTION, |
| 31 |
DeleteCriticalSection, EnterCriticalSection, INFINITE, InitializeCriticalSection, |
| 32 |
LeaveCriticalSection, ReleaseSemaphore, WAIT_OBJECT_0, WaitForSingleObject+/; |
| 33 |
import core.sys.windows.windef /+: BOOL, DWORD+/; |
| 34 |
import core.sys.windows.winerror /+: WAIT_TIMEOUT+/; |
| 35 |
} |
| 36 |
else version (Posix) |
| 37 |
{ |
| 38 |
import core.stdc.errno : EAGAIN, ETIMEDOUT; |
| 39 |
import core.sync.config; |
| 40 |
import core.sys.posix.pthread : pthread_cond_broadcast, pthread_cond_destroy, pthread_cond_init, |
| 41 |
pthread_cond_signal, pthread_cond_t, pthread_cond_timedwait, pthread_cond_wait; |
| 42 |
import core.sys.posix.time : timespec; |
| 43 |
} |
| 44 |
else |
| 45 |
{ |
| 46 |
static assert(false, "Platform not supported"); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
//////////////////////////////////////////////////////////////////////////////// |
| 51 |
// Condition |
| 52 |
// |
| 53 |
// void wait(); |
| 54 |
// void notify(); |
| 55 |
// void notifyAll(); |
| 56 |
//////////////////////////////////////////////////////////////////////////////// |
| 57 |
|
| 58 |
/** |
| 59 |
* This class represents a condition variable as conceived by C.A.R. Hoare. As |
| 60 |
* per Mesa type monitors however, "signal" has been replaced with "notify" to |
| 61 |
* indicate that control is not transferred to the waiter when a notification |
| 62 |
* is sent. |
| 63 |
*/ |
| 64 |
class Condition |
| 65 |
{ |
| 66 |
//////////////////////////////////////////////////////////////////////////// |
| 67 |
// Initialization |
| 68 |
//////////////////////////////////////////////////////////////////////////// |
| 69 |
|
| 70 |
/** |
| 71 |
* Initializes a condition object which is associated with the supplied |
| 72 |
* mutex object. |
| 73 |
* |
| 74 |
* Params: |
| 75 |
* m = The mutex with which this condition will be associated. |
| 76 |
* |
| 77 |
* Throws: |
| 78 |
* SyncError on error. |
| 79 |
*/ |
| 80 |
this( Mutex m ) nothrow @safe @nogc |
| 81 |
{ |
| 82 |
this(m, true); |
| 83 |
} |
| 84 |
|
| 85 |
/// ditto |
| 86 |
this( shared Mutex m ) shared nothrow @safe @nogc |
| 87 |
{ |
| 88 |
import core.atomic : atomicLoad; |
| 89 |
this(atomicLoad(m), true); |
| 90 |
} |
| 91 |
|
| 92 |
// |
| 93 |
private this(this Q, M)( M m, bool _unused_ ) nothrow @trusted @nogc |
| 94 |
if ((is(Q == Condition) && is(M == Mutex)) || |
| 95 |
(is(Q == shared Condition) && is(M == shared Mutex))) |
| 96 |
{ |
| 97 |
version (Windows) |
| 98 |
{ |
| 99 |
static if (is(Q == Condition)) |
| 100 |
{ |
| 101 |
alias HANDLE_TYPE = void*; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
alias HANDLE_TYPE = shared(void*); |
| 106 |
} |
| 107 |
m_blockLock = cast(HANDLE_TYPE) CreateSemaphoreA( null, 1, 1, null ); |
| 108 |
if ( m_blockLock == m_blockLock.init ) |
| 109 |
throw staticError!AssertError("Unable to initialize condition", __FILE__, __LINE__); |
| 110 |
scope(failure) CloseHandle( cast(void*) m_blockLock ); |
| 111 |
|
| 112 |
m_blockQueue = cast(HANDLE_TYPE) CreateSemaphoreA( null, 0, int.max, null ); |
| 113 |
if ( m_blockQueue == m_blockQueue.init ) |
| 114 |
throw staticError!AssertError("Unable to initialize condition", __FILE__, __LINE__); |
| 115 |
scope(failure) CloseHandle( cast(void*) m_blockQueue ); |
| 116 |
|
| 117 |
InitializeCriticalSection( cast(RTL_CRITICAL_SECTION*) &m_unblockLock ); |
| 118 |
m_assocMutex = m; |
| 119 |
} |
| 120 |
else version (Posix) |
| 121 |
{ |
| 122 |
static if (is(Q == shared)) |
| 123 |
{ |
| 124 |
import core.atomic : atomicLoad; |
| 125 |
m_assocMutex = atomicLoad(m); |
| 126 |
} |
| 127 |
else |
| 128 |
{ |
| 129 |
m_assocMutex = m; |
| 130 |
} |
| 131 |
static if ( is( typeof( imported!"core.sys.posix.pthread".pthread_condattr_setclock ) ) ) |
| 132 |
{ |
| 133 |
import core.sys.posix.pthread : pthread_condattr_destroy, pthread_condattr_init, |
| 134 |
pthread_condattr_setclock; |
| 135 |
import core.sys.posix.sys.types : pthread_condattr_t; |
| 136 |
import core.sys.posix.time : CLOCK_MONOTONIC; |
| 137 |
() @trusted |
| 138 |
{ |
| 139 |
pthread_condattr_t attr = void; |
| 140 |
int rc = pthread_condattr_init( &attr ); |
| 141 |
if ( rc ) |
| 142 |
throw staticError!AssertError("Unable to initialize condition", __FILE__, __LINE__); |
| 143 |
rc = pthread_condattr_setclock( &attr, CLOCK_MONOTONIC ); |
| 144 |
if ( rc ) |
| 145 |
throw staticError!AssertError("Unable to initialize condition", __FILE__, __LINE__); |
| 146 |
rc = pthread_cond_init( cast(pthread_cond_t*) &m_hndl, &attr ); |
| 147 |
if ( rc ) |
| 148 |
throw staticError!AssertError("Unable to initialize condition", __FILE__, __LINE__); |
| 149 |
rc = pthread_condattr_destroy( &attr ); |
| 150 |
if ( rc ) |
| 151 |
throw staticError!AssertError("Unable to initialize condition", __FILE__, __LINE__); |
| 152 |
} (); |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
int rc = pthread_cond_init( cast(pthread_cond_t*) &m_hndl, null ); |
| 157 |
if ( rc ) |
| 158 |
throw staticError!AssertError("Unable to initialize condition", __FILE__, __LINE__); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
~this() @nogc |
| 164 |
{ |
| 165 |
version (Windows) |
| 166 |
{ |
| 167 |
BOOL rc = CloseHandle( m_blockLock ); |
| 168 |
assert( rc, "Unable to destroy condition" ); |
| 169 |
rc = CloseHandle( m_blockQueue ); |
| 170 |
assert( rc, "Unable to destroy condition" ); |
| 171 |
DeleteCriticalSection( &m_unblockLock ); |
| 172 |
} |
| 173 |
else version (Posix) |
| 174 |
{ |
| 175 |
int rc = pthread_cond_destroy( &m_hndl ); |
| 176 |
assert( !rc, "Unable to destroy condition" ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
//////////////////////////////////////////////////////////////////////////// |
| 182 |
// General Properties |
| 183 |
//////////////////////////////////////////////////////////////////////////// |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* Gets the mutex associated with this condition. |
| 188 |
* |
| 189 |
* Returns: |
| 190 |
* The mutex associated with this condition. |
| 191 |
*/ |
| 192 |
@property Mutex mutex() |
| 193 |
{ |
| 194 |
return m_assocMutex; |
| 195 |
} |
| 196 |
|
| 197 |
/// ditto |
| 198 |
@property shared(Mutex) mutex() shared |
| 199 |
{ |
| 200 |
import core.atomic : atomicLoad; |
| 201 |
return atomicLoad(m_assocMutex); |
| 202 |
} |
| 203 |
|
| 204 |
// undocumented function for internal use |
| 205 |
final @property Mutex mutex_nothrow() pure nothrow @safe @nogc |
| 206 |
{ |
| 207 |
return m_assocMutex; |
| 208 |
} |
| 209 |
|
| 210 |
// ditto |
| 211 |
final @property shared(Mutex) mutex_nothrow() shared pure nothrow @safe @nogc |
| 212 |
{ |
| 213 |
import core.atomic : atomicLoad; |
| 214 |
return atomicLoad(m_assocMutex); |
| 215 |
} |
| 216 |
|
| 217 |
//////////////////////////////////////////////////////////////////////////// |
| 218 |
// General Actions |
| 219 |
//////////////////////////////////////////////////////////////////////////// |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Wait until notified. |
| 224 |
* |
| 225 |
* Throws: |
| 226 |
* SyncError on error. |
| 227 |
*/ |
| 228 |
void wait() |
| 229 |
{ |
| 230 |
wait!(typeof(this))(true); |
| 231 |
} |
| 232 |
|
| 233 |
/// ditto |
| 234 |
void wait() shared |
| 235 |
{ |
| 236 |
wait!(typeof(this))(true); |
| 237 |
} |
| 238 |
|
| 239 |
/// ditto |
| 240 |
void wait(this Q)( bool _unused_ ) |
| 241 |
if (is(Q == Condition) || is(Q == shared Condition)) |
| 242 |
{ |
| 243 |
version (Windows) |
| 244 |
{ |
| 245 |
timedWait( INFINITE ); |
| 246 |
} |
| 247 |
else version (Posix) |
| 248 |
{ |
| 249 |
int rc = pthread_cond_wait( cast(pthread_cond_t*) &m_hndl, (cast(Mutex) m_assocMutex).handleAddr() ); |
| 250 |
if ( rc ) |
| 251 |
throw staticError!AssertError("Unable to wait for condition", __FILE__, __LINE__); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Suspends the calling thread until a notification occurs or until the |
| 257 |
* supplied time period has elapsed. |
| 258 |
* |
| 259 |
* Params: |
| 260 |
* val = The time to wait. |
| 261 |
* |
| 262 |
* In: |
| 263 |
* val must be non-negative. |
| 264 |
* |
| 265 |
* Throws: |
| 266 |
* SyncError on error. |
| 267 |
* |
| 268 |
* Returns: |
| 269 |
* true if notified before the timeout and false if not. |
| 270 |
*/ |
| 271 |
bool wait( Duration val ) |
| 272 |
{ |
| 273 |
return wait!(typeof(this))(val, true); |
| 274 |
} |
| 275 |
|
| 276 |
/// ditto |
| 277 |
bool wait( Duration val ) shared |
| 278 |
{ |
| 279 |
return wait!(typeof(this))(val, true); |
| 280 |
} |
| 281 |
|
| 282 |
/// ditto |
| 283 |
bool wait(this Q)( Duration val, bool _unused_ ) |
| 284 |
if (is(Q == Condition) || is(Q == shared Condition)) |
| 285 |
in |
| 286 |
{ |
| 287 |
assert( !val.isNegative ); |
| 288 |
} |
| 289 |
do |
| 290 |
{ |
| 291 |
version (Windows) |
| 292 |
{ |
| 293 |
auto maxWaitMillis = dur!("msecs")( uint.max - 1 ); |
| 294 |
|
| 295 |
while ( val > maxWaitMillis ) |
| 296 |
{ |
| 297 |
if ( timedWait( cast(uint) |
| 298 |
maxWaitMillis.total!"msecs" ) ) |
| 299 |
return true; |
| 300 |
val -= maxWaitMillis; |
| 301 |
} |
| 302 |
return timedWait( cast(uint) val.total!"msecs" ); |
| 303 |
} |
| 304 |
else version (Posix) |
| 305 |
{ |
| 306 |
timespec t = void; |
| 307 |
mktspec( t, val ); |
| 308 |
|
| 309 |
int rc = pthread_cond_timedwait( cast(pthread_cond_t*) &m_hndl, |
| 310 |
(cast(Mutex) m_assocMutex).handleAddr(), |
| 311 |
&t ); |
| 312 |
if ( !rc ) |
| 313 |
return true; |
| 314 |
if ( rc == ETIMEDOUT ) |
| 315 |
return false; |
| 316 |
throw staticError!AssertError("Unable to wait for condition", __FILE__, __LINE__); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Notifies one waiter. |
| 322 |
* |
| 323 |
* Throws: |
| 324 |
* SyncError on error. |
| 325 |
*/ |
| 326 |
void notify() |
| 327 |
{ |
| 328 |
notify!(typeof(this))(true); |
| 329 |
} |
| 330 |
|
| 331 |
/// ditto |
| 332 |
void notify() shared |
| 333 |
{ |
| 334 |
notify!(typeof(this))(true); |
| 335 |
} |
| 336 |
|
| 337 |
/// ditto |
| 338 |
void notify(this Q)( bool _unused_ ) |
| 339 |
if (is(Q == Condition) || is(Q == shared Condition)) |
| 340 |
{ |
| 341 |
version (Windows) |
| 342 |
{ |
| 343 |
notify_( false ); |
| 344 |
} |
| 345 |
else version (Posix) |
| 346 |
{ |
| 347 |
// Since OS X 10.7 (Lion), pthread_cond_signal returns EAGAIN after retrying 8192 times, |
| 348 |
// so need to retrying while it returns EAGAIN. |
| 349 |
// |
| 350 |
// 10.7.0 (Lion): http://www.opensource.apple.com/source/Libc/Libc-763.11/pthreads/pthread_cond.c |
| 351 |
// 10.8.0 (Mountain Lion): http://www.opensource.apple.com/source/Libc/Libc-825.24/pthreads/pthread_cond.c |
| 352 |
// 10.10.0 (Yosemite): http://www.opensource.apple.com/source/libpthread/libpthread-105.1.4/src/pthread_cond.c |
| 353 |
// 10.11.0 (El Capitan): http://www.opensource.apple.com/source/libpthread/libpthread-137.1.1/src/pthread_cond.c |
| 354 |
// 10.12.0 (Sierra): http://www.opensource.apple.com/source/libpthread/libpthread-218.1.3/src/pthread_cond.c |
| 355 |
// 10.13.0 (High Sierra): http://www.opensource.apple.com/source/libpthread/libpthread-301.1.6/src/pthread_cond.c |
| 356 |
// 10.14.0 (Mojave): http://www.opensource.apple.com/source/libpthread/libpthread-330.201.1/src/pthread_cond.c |
| 357 |
// 10.14.1 (Mojave): http://www.opensource.apple.com/source/libpthread/libpthread-330.220.2/src/pthread_cond.c |
| 358 |
|
| 359 |
int rc; |
| 360 |
do { |
| 361 |
rc = pthread_cond_signal( cast(pthread_cond_t*) &m_hndl ); |
| 362 |
} while ( rc == EAGAIN ); |
| 363 |
if ( rc ) |
| 364 |
throw staticError!AssertError("Unable to notify condition", __FILE__, __LINE__); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Notifies all waiters. |
| 370 |
* |
| 371 |
* Throws: |
| 372 |
* SyncError on error. |
| 373 |
*/ |
| 374 |
void notifyAll() |
| 375 |
{ |
| 376 |
notifyAll!(typeof(this))(true); |
| 377 |
} |
| 378 |
|
| 379 |
/// ditto |
| 380 |
void notifyAll() shared |
| 381 |
{ |
| 382 |
notifyAll!(typeof(this))(true); |
| 383 |
} |
| 384 |
|
| 385 |
/// ditto |
| 386 |
void notifyAll(this Q)( bool _unused_ ) |
| 387 |
if (is(Q == Condition) || is(Q == shared Condition)) |
| 388 |
{ |
| 389 |
version (Windows) |
| 390 |
{ |
| 391 |
notify_( true ); |
| 392 |
} |
| 393 |
else version (Posix) |
| 394 |
{ |
| 395 |
// Since OS X 10.7 (Lion), pthread_cond_broadcast returns EAGAIN after retrying 8192 times, |
| 396 |
// so need to retrying while it returns EAGAIN. |
| 397 |
// |
| 398 |
// 10.7.0 (Lion): http://www.opensource.apple.com/source/Libc/Libc-763.11/pthreads/pthread_cond.c |
| 399 |
// 10.8.0 (Mountain Lion): http://www.opensource.apple.com/source/Libc/Libc-825.24/pthreads/pthread_cond.c |
| 400 |
// 10.10.0 (Yosemite): http://www.opensource.apple.com/source/libpthread/libpthread-105.1.4/src/pthread_cond.c |
| 401 |
// 10.11.0 (El Capitan): http://www.opensource.apple.com/source/libpthread/libpthread-137.1.1/src/pthread_cond.c |
| 402 |
// 10.12.0 (Sierra): http://www.opensource.apple.com/source/libpthread/libpthread-218.1.3/src/pthread_cond.c |
| 403 |
// 10.13.0 (High Sierra): http://www.opensource.apple.com/source/libpthread/libpthread-301.1.6/src/pthread_cond.c |
| 404 |
// 10.14.0 (Mojave): http://www.opensource.apple.com/source/libpthread/libpthread-330.201.1/src/pthread_cond.c |
| 405 |
// 10.14.1 (Mojave): http://www.opensource.apple.com/source/libpthread/libpthread-330.220.2/src/pthread_cond.c |
| 406 |
|
| 407 |
int rc; |
| 408 |
do { |
| 409 |
rc = pthread_cond_broadcast( cast(pthread_cond_t*) &m_hndl ); |
| 410 |
} while ( rc == EAGAIN ); |
| 411 |
if ( rc ) |
| 412 |
throw staticError!AssertError("Unable to notify condition", __FILE__, __LINE__); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
private: |
| 417 |
version (Windows) |
| 418 |
{ |
| 419 |
bool timedWait(this Q)( DWORD timeout ) |
| 420 |
if (is(Q == Condition) || is(Q == shared Condition)) |
| 421 |
{ |
| 422 |
static if (is(Q == Condition)) |
| 423 |
{ |
| 424 |
auto op(string o, T, V1)(ref T val, V1 mod) |
| 425 |
{ |
| 426 |
return mixin("val " ~ o ~ "mod"); |
| 427 |
} |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
auto op(string o, T, V1)(ref shared T val, V1 mod) |
| 432 |
{ |
| 433 |
import core.atomic: atomicOp; |
| 434 |
return atomicOp!o(val, mod); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
int numSignalsLeft; |
| 439 |
int numWaitersGone; |
| 440 |
DWORD rc; |
| 441 |
|
| 442 |
rc = WaitForSingleObject( cast(HANDLE) m_blockLock, INFINITE ); |
| 443 |
assert( rc == WAIT_OBJECT_0 ); |
| 444 |
|
| 445 |
op!"+="(m_numWaitersBlocked, 1); |
| 446 |
|
| 447 |
rc = ReleaseSemaphore( cast(HANDLE) m_blockLock, 1, null ); |
| 448 |
assert( rc ); |
| 449 |
|
| 450 |
m_assocMutex.unlock(); |
| 451 |
scope(failure) m_assocMutex.lock(); |
| 452 |
|
| 453 |
rc = WaitForSingleObject( cast(HANDLE) m_blockQueue, timeout ); |
| 454 |
assert( rc == WAIT_OBJECT_0 || rc == WAIT_TIMEOUT ); |
| 455 |
bool timedOut = (rc == WAIT_TIMEOUT); |
| 456 |
|
| 457 |
EnterCriticalSection( &m_unblockLock ); |
| 458 |
scope(failure) LeaveCriticalSection( &m_unblockLock ); |
| 459 |
|
| 460 |
if ( (numSignalsLeft = m_numWaitersToUnblock) != 0 ) |
| 461 |
{ |
| 462 |
if ( timedOut ) |
| 463 |
{ |
| 464 |
// timeout (or canceled) |
| 465 |
if ( m_numWaitersBlocked != 0 ) |
| 466 |
{ |
| 467 |
op!"-="(m_numWaitersBlocked, 1); |
| 468 |
// do not unblock next waiter below (already unblocked) |
| 469 |
numSignalsLeft = 0; |
| 470 |
} |
| 471 |
else |
| 472 |
{ |
| 473 |
// spurious wakeup pending!! |
| 474 |
m_numWaitersGone = 1; |
| 475 |
} |
| 476 |
} |
| 477 |
if ( op!"-="(m_numWaitersToUnblock, 1) == 0 ) |
| 478 |
{ |
| 479 |
if ( m_numWaitersBlocked != 0 ) |
| 480 |
{ |
| 481 |
// open the gate |
| 482 |
rc = ReleaseSemaphore( cast(HANDLE) m_blockLock, 1, null ); |
| 483 |
assert( rc ); |
| 484 |
// do not open the gate below again |
| 485 |
numSignalsLeft = 0; |
| 486 |
} |
| 487 |
else if ( (numWaitersGone = m_numWaitersGone) != 0 ) |
| 488 |
{ |
| 489 |
m_numWaitersGone = 0; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
else if ( op!"+="(m_numWaitersGone, 1) == int.max / 2 ) |
| 494 |
{ |
| 495 |
// timeout/canceled or spurious event :-) |
| 496 |
rc = WaitForSingleObject( cast(HANDLE) m_blockLock, INFINITE ); |
| 497 |
assert( rc == WAIT_OBJECT_0 ); |
| 498 |
// something is going on here - test of timeouts? |
| 499 |
op!"-="(m_numWaitersBlocked, m_numWaitersGone); |
| 500 |
rc = ReleaseSemaphore( cast(HANDLE) m_blockLock, 1, null ); |
| 501 |
assert( rc == WAIT_OBJECT_0 ); |
| 502 |
m_numWaitersGone = 0; |
| 503 |
} |
| 504 |
|
| 505 |
LeaveCriticalSection( &m_unblockLock ); |
| 506 |
|
| 507 |
if ( numSignalsLeft == 1 ) |
| 508 |
{ |
| 509 |
// better now than spurious later (same as ResetEvent) |
| 510 |
for ( ; numWaitersGone > 0; --numWaitersGone ) |
| 511 |
{ |
| 512 |
rc = WaitForSingleObject( cast(HANDLE) m_blockQueue, INFINITE ); |
| 513 |
assert( rc == WAIT_OBJECT_0 ); |
| 514 |
} |
| 515 |
// open the gate |
| 516 |
rc = ReleaseSemaphore( cast(HANDLE) m_blockLock, 1, null ); |
| 517 |
assert( rc ); |
| 518 |
} |
| 519 |
else if ( numSignalsLeft != 0 ) |
| 520 |
{ |
| 521 |
// unblock next waiter |
| 522 |
rc = ReleaseSemaphore( cast(HANDLE) m_blockQueue, 1, null ); |
| 523 |
assert( rc ); |
| 524 |
} |
| 525 |
m_assocMutex.lock(); |
| 526 |
return !timedOut; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
void notify_(this Q)( bool all ) |
| 531 |
if (is(Q == Condition) || is(Q == shared Condition)) |
| 532 |
{ |
| 533 |
static if (is(Q == Condition)) |
| 534 |
{ |
| 535 |
auto op(string o, T, V1)(ref T val, V1 mod) |
| 536 |
{ |
| 537 |
return mixin("val " ~ o ~ "mod"); |
| 538 |
} |
| 539 |
} |
| 540 |
else |
| 541 |
{ |
| 542 |
auto op(string o, T, V1)(ref shared T val, V1 mod) |
| 543 |
{ |
| 544 |
import core.atomic: atomicOp; |
| 545 |
return atomicOp!o(val, mod); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
DWORD rc; |
| 550 |
|
| 551 |
EnterCriticalSection( &m_unblockLock ); |
| 552 |
scope(failure) LeaveCriticalSection( &m_unblockLock ); |
| 553 |
|
| 554 |
if ( m_numWaitersToUnblock != 0 ) |
| 555 |
{ |
| 556 |
if ( m_numWaitersBlocked == 0 ) |
| 557 |
{ |
| 558 |
LeaveCriticalSection( &m_unblockLock ); |
| 559 |
return; |
| 560 |
} |
| 561 |
if ( all ) |
| 562 |
{ |
| 563 |
op!"+="(m_numWaitersToUnblock, m_numWaitersBlocked); |
| 564 |
m_numWaitersBlocked = 0; |
| 565 |
} |
| 566 |
else |
| 567 |
{ |
| 568 |
op!"+="(m_numWaitersToUnblock, 1); |
| 569 |
op!"-="(m_numWaitersBlocked, 1); |
| 570 |
} |
| 571 |
LeaveCriticalSection( &m_unblockLock ); |
| 572 |
} |
| 573 |
else if ( m_numWaitersBlocked > m_numWaitersGone ) |
| 574 |
{ |
| 575 |
rc = WaitForSingleObject( cast(HANDLE) m_blockLock, INFINITE ); |
| 576 |
assert( rc == WAIT_OBJECT_0 ); |
| 577 |
if ( 0 != m_numWaitersGone ) |
| 578 |
{ |
| 579 |
op!"-="(m_numWaitersBlocked, m_numWaitersGone); |
| 580 |
m_numWaitersGone = 0; |
| 581 |
} |
| 582 |
if ( all ) |
| 583 |
{ |
| 584 |
m_numWaitersToUnblock = m_numWaitersBlocked; |
| 585 |
m_numWaitersBlocked = 0; |
| 586 |
} |
| 587 |
else |
| 588 |
{ |
| 589 |
m_numWaitersToUnblock = 1; |
| 590 |
op!"-="(m_numWaitersBlocked, 1); |
| 591 |
} |
| 592 |
LeaveCriticalSection( &m_unblockLock ); |
| 593 |
rc = ReleaseSemaphore( cast(HANDLE) m_blockQueue, 1, null ); |
| 594 |
assert( rc ); |
| 595 |
} |
| 596 |
else |
| 597 |
{ |
| 598 |
LeaveCriticalSection( &m_unblockLock ); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
// NOTE: This implementation uses Algorithm 8c as described here: |
| 604 |
// http://groups.google.com/group/comp.programming.threads/ |
| 605 |
// browse_frm/thread/1692bdec8040ba40/e7a5f9d40e86503a |
| 606 |
HANDLE m_blockLock; // auto-reset event (now semaphore) |
| 607 |
HANDLE m_blockQueue; // auto-reset event (now semaphore) |
| 608 |
Mutex m_assocMutex; // external mutex/CS |
| 609 |
CRITICAL_SECTION m_unblockLock; // internal mutex/CS |
| 610 |
int m_numWaitersGone = 0; |
| 611 |
int m_numWaitersBlocked = 0; |
| 612 |
int m_numWaitersToUnblock = 0; |
| 613 |
} |
| 614 |
else version (Posix) |
| 615 |
{ |
| 616 |
Mutex m_assocMutex; |
| 617 |
pthread_cond_t m_hndl; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
//////////////////////////////////////////////////////////////////////////////// |
| 623 |
// Unit Tests |
| 624 |
//////////////////////////////////////////////////////////////////////////////// |
| 625 |
|
| 626 |
unittest |
| 627 |
{ |
| 628 |
import core.sync.mutex; |
| 629 |
import core.sync.semaphore; |
| 630 |
import core.thread; |
| 631 |
|
| 632 |
|
| 633 |
void testNotify() |
| 634 |
{ |
| 635 |
auto mutex = new Mutex; |
| 636 |
auto condReady = new Condition( mutex ); |
| 637 |
auto semDone = new Semaphore; |
| 638 |
auto synLoop = new Object; |
| 639 |
int numWaiters = 10; |
| 640 |
int numTries = 10; |
| 641 |
int numReady = 0; |
| 642 |
int numTotal = 0; |
| 643 |
int numDone = 0; |
| 644 |
int numPost = 0; |
| 645 |
|
| 646 |
void waiter() |
| 647 |
{ |
| 648 |
for ( int i = 0; i < numTries; ++i ) |
| 649 |
{ |
| 650 |
synchronized( mutex ) |
| 651 |
{ |
| 652 |
while ( numReady < 1 ) |
| 653 |
{ |
| 654 |
condReady.wait(); |
| 655 |
} |
| 656 |
--numReady; |
| 657 |
++numTotal; |
| 658 |
} |
| 659 |
|
| 660 |
synchronized( synLoop ) |
| 661 |
{ |
| 662 |
++numDone; |
| 663 |
} |
| 664 |
semDone.wait(); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
auto group = new ThreadGroup; |
| 669 |
|
| 670 |
for ( int i = 0; i < numWaiters; ++i ) |
| 671 |
group.create( &waiter ); |
| 672 |
|
| 673 |
for ( int i = 0; i < numTries; ++i ) |
| 674 |
{ |
| 675 |
for ( int j = 0; j < numWaiters; ++j ) |
| 676 |
{ |
| 677 |
synchronized( mutex ) |
| 678 |
{ |
| 679 |
++numReady; |
| 680 |
condReady.notify(); |
| 681 |
} |
| 682 |
} |
| 683 |
while ( true ) |
| 684 |
{ |
| 685 |
synchronized( synLoop ) |
| 686 |
{ |
| 687 |
if ( numDone >= numWaiters ) |
| 688 |
break; |
| 689 |
} |
| 690 |
Thread.yield(); |
| 691 |
} |
| 692 |
for ( int j = 0; j < numWaiters; ++j ) |
| 693 |
{ |
| 694 |
semDone.notify(); |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
group.joinAll(); |
| 699 |
assert( numTotal == numWaiters * numTries ); |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
void testNotifyAll() |
| 704 |
{ |
| 705 |
auto mutex = new Mutex; |
| 706 |
auto condReady = new Condition( mutex ); |
| 707 |
int numWaiters = 10; |
| 708 |
int numReady = 0; |
| 709 |
int numDone = 0; |
| 710 |
bool alert = false; |
| 711 |
|
| 712 |
void waiter() |
| 713 |
{ |
| 714 |
synchronized( mutex ) |
| 715 |
{ |
| 716 |
++numReady; |
| 717 |
while ( !alert ) |
| 718 |
condReady.wait(); |
| 719 |
++numDone; |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
auto group = new ThreadGroup; |
| 724 |
|
| 725 |
for ( int i = 0; i < numWaiters; ++i ) |
| 726 |
group.create( &waiter ); |
| 727 |
|
| 728 |
while ( true ) |
| 729 |
{ |
| 730 |
synchronized( mutex ) |
| 731 |
{ |
| 732 |
if ( numReady >= numWaiters ) |
| 733 |
{ |
| 734 |
alert = true; |
| 735 |
condReady.notifyAll(); |
| 736 |
break; |
| 737 |
} |
| 738 |
} |
| 739 |
Thread.yield(); |
| 740 |
} |
| 741 |
group.joinAll(); |
| 742 |
assert( numReady == numWaiters && numDone == numWaiters ); |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
void testWaitTimeout() |
| 747 |
{ |
| 748 |
auto mutex = new Mutex; |
| 749 |
auto condReady = new Condition( mutex ); |
| 750 |
bool waiting = false; |
| 751 |
bool alertedOne = true; |
| 752 |
bool alertedTwo = true; |
| 753 |
|
| 754 |
void waiter() |
| 755 |
{ |
| 756 |
synchronized( mutex ) |
| 757 |
{ |
| 758 |
waiting = true; |
| 759 |
// we never want to miss the notification (30s) |
| 760 |
alertedOne = condReady.wait( dur!"seconds"(30) ); |
| 761 |
// but we don't want to wait long for the timeout (10ms) |
| 762 |
alertedTwo = condReady.wait( dur!"msecs"(10) ); |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
auto thread = new Thread( &waiter ); |
| 767 |
thread.start(); |
| 768 |
|
| 769 |
while ( true ) |
| 770 |
{ |
| 771 |
synchronized( mutex ) |
| 772 |
{ |
| 773 |
if ( waiting ) |
| 774 |
{ |
| 775 |
condReady.notify(); |
| 776 |
break; |
| 777 |
} |
| 778 |
} |
| 779 |
Thread.yield(); |
| 780 |
} |
| 781 |
thread.join(); |
| 782 |
assert( waiting ); |
| 783 |
assert( alertedOne ); |
| 784 |
assert( !alertedTwo ); |
| 785 |
} |
| 786 |
|
| 787 |
testNotify(); |
| 788 |
testNotifyAll(); |
| 789 |
testWaitTimeout(); |
| 790 |
} |
| 791 |
|
| 792 |
unittest |
| 793 |
{ |
| 794 |
import core.sync.mutex; |
| 795 |
import core.sync.semaphore; |
| 796 |
import core.thread; |
| 797 |
|
| 798 |
|
| 799 |
void testNotify() |
| 800 |
{ |
| 801 |
auto mutex = new shared Mutex; |
| 802 |
auto condReady = new shared Condition( mutex ); |
| 803 |
auto semDone = new Semaphore; |
| 804 |
auto synLoop = new Object; |
| 805 |
int numWaiters = 10; |
| 806 |
int numTries = 10; |
| 807 |
int numReady = 0; |
| 808 |
int numTotal = 0; |
| 809 |
int numDone = 0; |
| 810 |
int numPost = 0; |
| 811 |
|
| 812 |
void waiter() |
| 813 |
{ |
| 814 |
for ( int i = 0; i < numTries; ++i ) |
| 815 |
{ |
| 816 |
synchronized( mutex ) |
| 817 |
{ |
| 818 |
while ( numReady < 1 ) |
| 819 |
{ |
| 820 |
condReady.wait(); |
| 821 |
} |
| 822 |
--numReady; |
| 823 |
++numTotal; |
| 824 |
} |
| 825 |
|
| 826 |
synchronized( synLoop ) |
| 827 |
{ |
| 828 |
++numDone; |
| 829 |
} |
| 830 |
semDone.wait(); |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
auto group = new ThreadGroup; |
| 835 |
|
| 836 |
for ( int i = 0; i < numWaiters; ++i ) |
| 837 |
group.create( &waiter ); |
| 838 |
|
| 839 |
for ( int i = 0; i < numTries; ++i ) |
| 840 |
{ |
| 841 |
for ( int j = 0; j < numWaiters; ++j ) |
| 842 |
{ |
| 843 |
synchronized( mutex ) |
| 844 |
{ |
| 845 |
++numReady; |
| 846 |
condReady.notify(); |
| 847 |
} |
| 848 |
} |
| 849 |
while ( true ) |
| 850 |
{ |
| 851 |
synchronized( synLoop ) |
| 852 |
{ |
| 853 |
if ( numDone >= numWaiters ) |
| 854 |
break; |
| 855 |
} |
| 856 |
Thread.yield(); |
| 857 |
} |
| 858 |
for ( int j = 0; j < numWaiters; ++j ) |
| 859 |
{ |
| 860 |
semDone.notify(); |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
group.joinAll(); |
| 865 |
assert( numTotal == numWaiters * numTries ); |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
void testNotifyAll() |
| 870 |
{ |
| 871 |
auto mutex = new shared Mutex; |
| 872 |
auto condReady = new shared Condition( mutex ); |
| 873 |
int numWaiters = 10; |
| 874 |
int numReady = 0; |
| 875 |
int numDone = 0; |
| 876 |
bool alert = false; |
| 877 |
|
| 878 |
void waiter() |
| 879 |
{ |
| 880 |
synchronized( mutex ) |
| 881 |
{ |
| 882 |
++numReady; |
| 883 |
while ( !alert ) |
| 884 |
condReady.wait(); |
| 885 |
++numDone; |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
auto group = new ThreadGroup; |
| 890 |
|
| 891 |
for ( int i = 0; i < numWaiters; ++i ) |
| 892 |
group.create( &waiter ); |
| 893 |
|
| 894 |
while ( true ) |
| 895 |
{ |
| 896 |
synchronized( mutex ) |
| 897 |
{ |
| 898 |
if ( numReady >= numWaiters ) |
| 899 |
{ |
| 900 |
alert = true; |
| 901 |
condReady.notifyAll(); |
| 902 |
break; |
| 903 |
} |
| 904 |
} |
| 905 |
Thread.yield(); |
| 906 |
} |
| 907 |
group.joinAll(); |
| 908 |
assert( numReady == numWaiters && numDone == numWaiters ); |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
void testWaitTimeout() |
| 913 |
{ |
| 914 |
auto mutex = new shared Mutex; |
| 915 |
auto condReady = new shared Condition( mutex ); |
| 916 |
bool waiting = false; |
| 917 |
bool alertedOne = true; |
| 918 |
bool alertedTwo = true; |
| 919 |
|
| 920 |
void waiter() |
| 921 |
{ |
| 922 |
synchronized( mutex ) |
| 923 |
{ |
| 924 |
waiting = true; |
| 925 |
// we never want to miss the notification (30s) |
| 926 |
alertedOne = condReady.wait( dur!"seconds"(30) ); |
| 927 |
// but we don't want to wait long for the timeout (10ms) |
| 928 |
alertedTwo = condReady.wait( dur!"msecs"(10) ); |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
auto thread = new Thread( &waiter ); |
| 933 |
thread.start(); |
| 934 |
|
| 935 |
while ( true ) |
| 936 |
{ |
| 937 |
synchronized( mutex ) |
| 938 |
{ |
| 939 |
if ( waiting ) |
| 940 |
{ |
| 941 |
condReady.notify(); |
| 942 |
break; |
| 943 |
} |
| 944 |
} |
| 945 |
Thread.yield(); |
| 946 |
} |
| 947 |
thread.join(); |
| 948 |
assert( waiting ); |
| 949 |
assert( alertedOne ); |
| 950 |
assert( !alertedTwo ); |
| 951 |
} |
| 952 |
|
| 953 |
testNotify(); |
| 954 |
testNotifyAll(); |
| 955 |
testWaitTimeout(); |
| 956 |
} |