40
45
void qemu_mutex_init(QemuMutex *mutex)
43
InitializeCriticalSection(&mutex->lock);
47
InitializeSRWLock(&mutex->lock);
46
50
void qemu_mutex_destroy(QemuMutex *mutex)
48
assert(mutex->owner == 0);
49
DeleteCriticalSection(&mutex->lock);
52
InitializeSRWLock(&mutex->lock);
52
55
void qemu_mutex_lock(QemuMutex *mutex)
54
EnterCriticalSection(&mutex->lock);
56
/* Win32 CRITICAL_SECTIONs are recursive. Assert that we're not
59
assert(mutex->owner == 0);
60
mutex->owner = GetCurrentThreadId();
57
AcquireSRWLockExclusive(&mutex->lock);
63
60
int qemu_mutex_trylock(QemuMutex *mutex)
67
owned = TryEnterCriticalSection(&mutex->lock);
69
assert(mutex->owner == 0);
70
mutex->owner = GetCurrentThreadId();
64
owned = TryAcquireSRWLockExclusive(&mutex->lock);
75
68
void qemu_mutex_unlock(QemuMutex *mutex)
77
assert(mutex->owner == GetCurrentThreadId());
79
LeaveCriticalSection(&mutex->lock);
70
ReleaseSRWLockExclusive(&mutex->lock);
82
73
void qemu_rec_mutex_init(QemuRecMutex *mutex)
107
98
void qemu_cond_init(QemuCond *cond)
109
100
memset(cond, 0, sizeof(*cond));
111
cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
113
error_exit(GetLastError(), __func__);
115
cond->continue_event = CreateEvent(NULL, /* security */
116
FALSE, /* auto-reset */
117
FALSE, /* not signaled */
119
if (!cond->continue_event) {
120
error_exit(GetLastError(), __func__);
101
InitializeConditionVariable(&cond->var);
124
104
void qemu_cond_destroy(QemuCond *cond)
127
result = CloseHandle(cond->continue_event);
129
error_exit(GetLastError(), __func__);
131
cond->continue_event = 0;
132
result = CloseHandle(cond->sema);
134
error_exit(GetLastError(), __func__);
106
InitializeConditionVariable(&cond->var);
139
109
void qemu_cond_signal(QemuCond *cond)
144
* Signal only when there are waiters. cond->waiters is
145
* incremented by pthread_cond_wait under the external lock,
146
* so we are safe about that.
148
if (cond->waiters == 0) {
153
* Waiting threads decrement it outside the external lock, but
154
* only if another thread is executing pthread_cond_broadcast and
155
* has the mutex. So, it also cannot be decremented concurrently
156
* with this particular access.
158
cond->target = cond->waiters - 1;
159
result = SignalObjectAndWait(cond->sema, cond->continue_event,
161
if (result == WAIT_ABANDONED || result == WAIT_FAILED) {
162
error_exit(GetLastError(), __func__);
111
WakeConditionVariable(&cond->var);
166
114
void qemu_cond_broadcast(QemuCond *cond)
170
* As in pthread_cond_signal, access to cond->waiters and
171
* cond->target is locked via the external mutex.
173
if (cond->waiters == 0) {
178
result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
180
error_exit(GetLastError(), __func__);
184
* At this point all waiters continue. Each one takes its
185
* slice of the semaphore. Now it's our turn to wait: Since
186
* the external mutex is held, no thread can leave cond_wait,
187
* yet. For this reason, we can be sure that no thread gets
188
* a chance to eat *more* than one slice. OTOH, it means
189
* that the last waiter must send us a wake-up.
191
WaitForSingleObject(cond->continue_event, INFINITE);
116
WakeAllConditionVariable(&cond->var);
194
119
void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
197
* This access is protected under the mutex.
202
* Unlock external mutex and wait for signal.
203
* NOTE: we've held mutex locked long enough to increment
204
* waiters count above, so there's no problem with
205
* leaving mutex unlocked before we wait on semaphore.
207
qemu_mutex_unlock(mutex);
208
WaitForSingleObject(cond->sema, INFINITE);
210
/* Now waiters must rendez-vous with the signaling thread and
211
* let it continue. For cond_broadcast this has heavy contention
212
* and triggers thundering herd. So goes life.
214
* Decrease waiters count. The mutex is not taken, so we have
215
* to do this atomically.
217
* All waiters contend for the mutex at the end of this function
218
* until the signaling thread relinquishes it. To ensure
219
* each waiter consumes exactly one slice of the semaphore,
220
* the signaling thread stops until it is told by the last
221
* waiter that it can go on.
223
if (InterlockedDecrement(&cond->waiters) == cond->target) {
224
SetEvent(cond->continue_event);
227
qemu_mutex_lock(mutex);
121
SleepConditionVariableSRW(&cond->var, &mutex->lock, INFINITE, 0);
230
124
void qemu_sem_init(QemuSemaphore *sem, int init)