| 1 |
/** |
| 2 |
* pthread_cond API for Win32 |
| 3 |
* |
| 4 |
* ACE(TM), TAO(TM), CIAO(TM), DAnCE>(TM), and CoSMIC(TM) (henceforth |
| 5 |
* referred to as "DOC software") are copyrighted by Douglas C. Schmidt |
| 6 |
* and his research group at Washington University, University of California, |
| 7 |
* Irvine, and Vanderbilt University, Copyright (c) 1993-2009, all rights |
| 8 |
* reserved. |
| 9 |
* |
| 10 |
* Since DOC software is open-source, freely available software, you are free |
| 11 |
* to use, modify, copy, and distribute--perpetually and irrevocably--the DOC |
| 12 |
* software source code and object code produced from the source, as well as |
| 13 |
* copy and distribute modified versions of this software. You must, however, |
| 14 |
* include this copyright statement along with any code built using DOC |
| 15 |
* software that you release. |
| 16 |
* |
| 17 |
* No copyright statement needs to be provided if you just ship binary |
| 18 |
* executables of your software products. |
| 19 |
* |
| 20 |
* See "Strategies for Implementing POSIX Condition Variables on Win32" at |
| 21 |
* http://www.cs.wustl.edu/~schmidt/win32-cv-1.html |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <windows.h> |
| 25 |
|
| 26 |
#include "win32cond.h" |
| 27 |
|
| 28 |
int win32_cond_init(win32_cond_t *cv) |
| 29 |
{ |
| 30 |
cv->waiters_count_ = 0; |
| 31 |
cv->was_broadcast_ = 0; |
| 32 |
cv->sema_ = CreateSemaphore (NULL, // no security |
| 33 |
0, // initially 0 |
| 34 |
0x7fffffff, // max count |
| 35 |
NULL); // unnamed |
| 36 |
InitializeCriticalSection (&cv->waiters_count_lock_); |
| 37 |
cv->waiters_done_ = CreateEvent (NULL, // no security |
| 38 |
FALSE, // auto-reset |
| 39 |
FALSE, // non-signaled initially |
| 40 |
NULL); // unnamed |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
int win32_cond_destroy(win32_cond_t *cv) |
| 45 |
{ |
| 46 |
CloseHandle(cv->waiters_done_); |
| 47 |
DeleteCriticalSection(&cv->waiters_count_lock_); |
| 48 |
CloseHandle(cv->sema_); |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
int win32_cond_wait(win32_cond_t *cv, HANDLE *external_mutex) |
| 53 |
{ |
| 54 |
int last_waiter; |
| 55 |
|
| 56 |
// Avoid race conditions. |
| 57 |
EnterCriticalSection (&cv->waiters_count_lock_); |
| 58 |
cv->waiters_count_++; |
| 59 |
LeaveCriticalSection (&cv->waiters_count_lock_); |
| 60 |
|
| 61 |
// This call atomically releases the mutex and waits on the |
| 62 |
// semaphore until <pthread_cond_signal> or <pthread_cond_broadcast> |
| 63 |
// are called by another thread. |
| 64 |
SignalObjectAndWait (*external_mutex, cv->sema_, INFINITE, FALSE); |
| 65 |
|
| 66 |
// Reacquire lock to avoid race conditions. |
| 67 |
EnterCriticalSection (&cv->waiters_count_lock_); |
| 68 |
|
| 69 |
// We're no longer waiting... |
| 70 |
cv->waiters_count_--; |
| 71 |
|
| 72 |
// Check to see if we're the last waiter after <pthread_cond_broadcast>. |
| 73 |
last_waiter = cv->was_broadcast_ && cv->waiters_count_ == 0; |
| 74 |
|
| 75 |
LeaveCriticalSection (&cv->waiters_count_lock_); |
| 76 |
|
| 77 |
// If we're the last waiter thread during this particular broadcast |
| 78 |
// then let all the other threads proceed. |
| 79 |
if (last_waiter) |
| 80 |
// This call atomically signals the <waiters_done_> event and waits until |
| 81 |
// it can acquire the <external_mutex>. This is required to ensure fairness. |
| 82 |
SignalObjectAndWait (cv->waiters_done_, *external_mutex, INFINITE, FALSE); |
| 83 |
else |
| 84 |
// Always regain the external mutex since that's the guarantee we |
| 85 |
// give to our callers. |
| 86 |
WaitForSingleObject (*external_mutex, INFINITE); |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
int win32_cond_signal(win32_cond_t *cv) |
| 91 |
{ |
| 92 |
int have_waiters; |
| 93 |
|
| 94 |
EnterCriticalSection (&cv->waiters_count_lock_); |
| 95 |
have_waiters = cv->waiters_count_ > 0; |
| 96 |
LeaveCriticalSection (&cv->waiters_count_lock_); |
| 97 |
|
| 98 |
// If there aren't any waiters, then this is a no-op. |
| 99 |
if (have_waiters) |
| 100 |
ReleaseSemaphore (cv->sema_, 1, 0); |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
int win32_cond_broadcast(win32_cond_t *cv) |
| 105 |
{ |
| 106 |
int have_waiters = 0; |
| 107 |
|
| 108 |
// This is needed to ensure that <waiters_count_> and <was_broadcast_> are |
| 109 |
// consistent relative to each other. |
| 110 |
EnterCriticalSection (&cv->waiters_count_lock_); |
| 111 |
|
| 112 |
if (cv->waiters_count_ > 0) { |
| 113 |
// We are broadcasting, even if there is just one waiter... |
| 114 |
// Record that we are broadcasting, which helps optimize |
| 115 |
// <pthread_cond_wait> for the non-broadcast case. |
| 116 |
cv->was_broadcast_ = 1; |
| 117 |
have_waiters = 1; |
| 118 |
} |
| 119 |
|
| 120 |
if (have_waiters) { |
| 121 |
// Wake up all the waiters atomically. |
| 122 |
ReleaseSemaphore (cv->sema_, cv->waiters_count_, 0); |
| 123 |
|
| 124 |
LeaveCriticalSection (&cv->waiters_count_lock_); |
| 125 |
|
| 126 |
// Wait for all the awakened threads to acquire the counting |
| 127 |
// semaphore. |
| 128 |
WaitForSingleObject (cv->waiters_done_, INFINITE); |
| 129 |
// This assignment is okay, even without the <waiters_count_lock_> held |
| 130 |
// because no other waiter threads can wake up to access it. |
| 131 |
cv->was_broadcast_ = 0; |
| 132 |
} |
| 133 |
else |
| 134 |
LeaveCriticalSection (&cv->waiters_count_lock_); |
| 135 |
return 0; |
| 136 |
} |