2 * Win32 implementation for mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2010
7 * Paolo Bonzini <pbonzini@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
15 #include "qemu/thread.h"
16 #include "qemu/notify.h"
19 static bool name_threads
;
21 void qemu_thread_naming(bool enable
)
23 /* But note we don't actually name them on Windows yet */
24 name_threads
= enable
;
26 fprintf(stderr
, "qemu: thread naming not supported on this host\n");
29 static void error_exit(int err
, const char *msg
)
33 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
,
34 NULL
, err
, 0, (LPTSTR
)&pstr
, 2, NULL
);
35 fprintf(stderr
, "qemu: %s: %s\n", msg
, pstr
);
40 void qemu_mutex_init(QemuMutex
*mutex
)
43 InitializeCriticalSection(&mutex
->lock
);
46 void qemu_mutex_destroy(QemuMutex
*mutex
)
48 assert(mutex
->owner
== 0);
49 DeleteCriticalSection(&mutex
->lock
);
52 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();
63 int qemu_mutex_trylock(QemuMutex
*mutex
)
67 owned
= TryEnterCriticalSection(&mutex
->lock
);
69 assert(mutex
->owner
== 0);
70 mutex
->owner
= GetCurrentThreadId();
75 void qemu_mutex_unlock(QemuMutex
*mutex
)
77 assert(mutex
->owner
== GetCurrentThreadId());
79 LeaveCriticalSection(&mutex
->lock
);
82 void qemu_cond_init(QemuCond
*cond
)
84 memset(cond
, 0, sizeof(*cond
));
86 cond
->sema
= CreateSemaphore(NULL
, 0, LONG_MAX
, NULL
);
88 error_exit(GetLastError(), __func__
);
90 cond
->continue_event
= CreateEvent(NULL
, /* security */
91 FALSE
, /* auto-reset */
92 FALSE
, /* not signaled */
94 if (!cond
->continue_event
) {
95 error_exit(GetLastError(), __func__
);
99 void qemu_cond_destroy(QemuCond
*cond
)
102 result
= CloseHandle(cond
->continue_event
);
104 error_exit(GetLastError(), __func__
);
106 cond
->continue_event
= 0;
107 result
= CloseHandle(cond
->sema
);
109 error_exit(GetLastError(), __func__
);
114 void qemu_cond_signal(QemuCond
*cond
)
119 * Signal only when there are waiters. cond->waiters is
120 * incremented by pthread_cond_wait under the external lock,
121 * so we are safe about that.
123 if (cond
->waiters
== 0) {
128 * Waiting threads decrement it outside the external lock, but
129 * only if another thread is executing pthread_cond_broadcast and
130 * has the mutex. So, it also cannot be decremented concurrently
131 * with this particular access.
133 cond
->target
= cond
->waiters
- 1;
134 result
= SignalObjectAndWait(cond
->sema
, cond
->continue_event
,
136 if (result
== WAIT_ABANDONED
|| result
== WAIT_FAILED
) {
137 error_exit(GetLastError(), __func__
);
141 void qemu_cond_broadcast(QemuCond
*cond
)
145 * As in pthread_cond_signal, access to cond->waiters and
146 * cond->target is locked via the external mutex.
148 if (cond
->waiters
== 0) {
153 result
= ReleaseSemaphore(cond
->sema
, cond
->waiters
, NULL
);
155 error_exit(GetLastError(), __func__
);
159 * At this point all waiters continue. Each one takes its
160 * slice of the semaphore. Now it's our turn to wait: Since
161 * the external mutex is held, no thread can leave cond_wait,
162 * yet. For this reason, we can be sure that no thread gets
163 * a chance to eat *more* than one slice. OTOH, it means
164 * that the last waiter must send us a wake-up.
166 WaitForSingleObject(cond
->continue_event
, INFINITE
);
169 void qemu_cond_wait(QemuCond
*cond
, QemuMutex
*mutex
)
172 * This access is protected under the mutex.
177 * Unlock external mutex and wait for signal.
178 * NOTE: we've held mutex locked long enough to increment
179 * waiters count above, so there's no problem with
180 * leaving mutex unlocked before we wait on semaphore.
182 qemu_mutex_unlock(mutex
);
183 WaitForSingleObject(cond
->sema
, INFINITE
);
185 /* Now waiters must rendez-vous with the signaling thread and
186 * let it continue. For cond_broadcast this has heavy contention
187 * and triggers thundering herd. So goes life.
189 * Decrease waiters count. The mutex is not taken, so we have
190 * to do this atomically.
192 * All waiters contend for the mutex at the end of this function
193 * until the signaling thread relinquishes it. To ensure
194 * each waiter consumes exactly one slice of the semaphore,
195 * the signaling thread stops until it is told by the last
196 * waiter that it can go on.
198 if (InterlockedDecrement(&cond
->waiters
) == cond
->target
) {
199 SetEvent(cond
->continue_event
);
202 qemu_mutex_lock(mutex
);
205 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
208 sem
->sema
= CreateSemaphore(NULL
, init
, LONG_MAX
, NULL
);
211 void qemu_sem_destroy(QemuSemaphore
*sem
)
213 CloseHandle(sem
->sema
);
216 void qemu_sem_post(QemuSemaphore
*sem
)
218 ReleaseSemaphore(sem
->sema
, 1, NULL
);
221 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
223 int rc
= WaitForSingleObject(sem
->sema
, ms
);
224 if (rc
== WAIT_OBJECT_0
) {
227 if (rc
!= WAIT_TIMEOUT
) {
228 error_exit(GetLastError(), __func__
);
233 void qemu_sem_wait(QemuSemaphore
*sem
)
235 if (WaitForSingleObject(sem
->sema
, INFINITE
) != WAIT_OBJECT_0
) {
236 error_exit(GetLastError(), __func__
);
240 /* Wrap a Win32 manual-reset event with a fast userspace path. The idea
241 * is to reset the Win32 event lazily, as part of a test-reset-test-wait
242 * sequence. Such a sequence is, indeed, how QemuEvents are used by
243 * RCU and other subsystems!
246 * - free->set, when setting the event
247 * - busy->set, when setting the event, followed by futex_wake
248 * - set->free, when resetting the event
249 * - free->busy, when waiting
251 * set->busy does not happen (it can be observed from the outside but
252 * it really is set->free->busy).
254 * busy->free provably cannot happen; to enforce it, the set->free transition
255 * is done with an OR, which becomes a no-op if the event has concurrently
256 * transitioned to free or busy (and is faster than cmpxchg).
263 void qemu_event_init(QemuEvent
*ev
, bool init
)
266 ev
->event
= CreateEvent(NULL
, TRUE
, TRUE
, NULL
);
267 ev
->value
= (init
? EV_SET
: EV_FREE
);
270 void qemu_event_destroy(QemuEvent
*ev
)
272 CloseHandle(ev
->event
);
275 void qemu_event_set(QemuEvent
*ev
)
277 /* qemu_event_set has release semantics, but because it *loads*
278 * ev->value we need a full memory barrier here.
281 if (atomic_read(&ev
->value
) != EV_SET
) {
282 if (atomic_xchg(&ev
->value
, EV_SET
) == EV_BUSY
) {
283 /* There were waiters, wake them up. */
289 void qemu_event_reset(QemuEvent
*ev
)
293 value
= atomic_read(&ev
->value
);
295 if (value
== EV_SET
) {
296 /* If there was a concurrent reset (or even reset+wait),
297 * do nothing. Otherwise change EV_SET->EV_FREE.
299 atomic_or(&ev
->value
, EV_FREE
);
303 void qemu_event_wait(QemuEvent
*ev
)
307 value
= atomic_read(&ev
->value
);
309 if (value
!= EV_SET
) {
310 if (value
== EV_FREE
) {
311 /* qemu_event_set is not yet going to call SetEvent, but we are
312 * going to do another check for EV_SET below when setting EV_BUSY.
313 * At that point it is safe to call WaitForSingleObject.
315 ResetEvent(ev
->event
);
317 /* Tell qemu_event_set that there are waiters. No need to retry
318 * because there cannot be a concurent busy->free transition.
319 * After the CAS, the event will be either set or busy.
321 if (atomic_cmpxchg(&ev
->value
, EV_FREE
, EV_BUSY
) == EV_SET
) {
327 if (value
== EV_BUSY
) {
328 WaitForSingleObject(ev
->event
, INFINITE
);
333 struct QemuThreadData
{
334 /* Passed to win32_start_routine. */
335 void *(*start_routine
)(void *);
340 /* Only used for joinable threads. */
346 static bool atexit_registered
;
347 static NotifierList main_thread_exit
;
349 static __thread QemuThreadData
*qemu_thread_data
;
351 static void run_main_thread_exit(void)
353 notifier_list_notify(&main_thread_exit
, NULL
);
356 void qemu_thread_atexit_add(Notifier
*notifier
)
358 if (!qemu_thread_data
) {
359 if (!atexit_registered
) {
360 atexit_registered
= true;
361 atexit(run_main_thread_exit
);
363 notifier_list_add(&main_thread_exit
, notifier
);
365 notifier_list_add(&qemu_thread_data
->exit
, notifier
);
369 void qemu_thread_atexit_remove(Notifier
*notifier
)
371 notifier_remove(notifier
);
374 static unsigned __stdcall
win32_start_routine(void *arg
)
376 QemuThreadData
*data
= (QemuThreadData
*) arg
;
377 void *(*start_routine
)(void *) = data
->start_routine
;
378 void *thread_arg
= data
->arg
;
380 qemu_thread_data
= data
;
381 qemu_thread_exit(start_routine(thread_arg
));
385 void qemu_thread_exit(void *arg
)
387 QemuThreadData
*data
= qemu_thread_data
;
389 notifier_list_notify(&data
->exit
, NULL
);
390 if (data
->mode
== QEMU_THREAD_JOINABLE
) {
392 EnterCriticalSection(&data
->cs
);
394 LeaveCriticalSection(&data
->cs
);
401 void *qemu_thread_join(QemuThread
*thread
)
403 QemuThreadData
*data
;
408 if (data
->mode
== QEMU_THREAD_DETACHED
) {
413 * Because multiple copies of the QemuThread can exist via
414 * qemu_thread_get_self, we need to store a value that cannot
415 * leak there. The simplest, non racy way is to store the TID,
416 * discard the handle that _beginthreadex gives back, and
417 * get another copy of the handle here.
419 handle
= qemu_thread_get_handle(thread
);
421 WaitForSingleObject(handle
, INFINITE
);
425 DeleteCriticalSection(&data
->cs
);
430 void qemu_thread_create(QemuThread
*thread
, const char *name
,
431 void *(*start_routine
)(void *),
435 struct QemuThreadData
*data
;
437 data
= g_malloc(sizeof *data
);
438 data
->start_routine
= start_routine
;
441 data
->exited
= false;
442 notifier_list_init(&data
->exit
);
444 if (data
->mode
!= QEMU_THREAD_DETACHED
) {
445 InitializeCriticalSection(&data
->cs
);
448 hThread
= (HANDLE
) _beginthreadex(NULL
, 0, win32_start_routine
,
449 data
, 0, &thread
->tid
);
451 error_exit(GetLastError(), __func__
);
453 CloseHandle(hThread
);
457 void qemu_thread_get_self(QemuThread
*thread
)
459 thread
->data
= qemu_thread_data
;
460 thread
->tid
= GetCurrentThreadId();
463 HANDLE
qemu_thread_get_handle(QemuThread
*thread
)
465 QemuThreadData
*data
;
469 if (data
->mode
== QEMU_THREAD_DETACHED
) {
473 EnterCriticalSection(&data
->cs
);
475 handle
= OpenThread(SYNCHRONIZE
| THREAD_SUSPEND_RESUME
, FALSE
,
480 LeaveCriticalSection(&data
->cs
);
484 bool qemu_thread_is_self(QemuThread
*thread
)
486 return GetCurrentThreadId() == thread
->tid
;