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.
15 #define _WIN32_WINNT 0x0600
18 #include "qemu/osdep.h"
19 #include "qemu-common.h"
20 #include "qemu/thread.h"
21 #include "qemu/notify.h"
25 static bool name_threads
;
27 void qemu_thread_naming(bool enable
)
29 /* But note we don't actually name them on Windows yet */
30 name_threads
= enable
;
32 fprintf(stderr
, "qemu: thread naming not supported on this host\n");
35 static void error_exit(int err
, const char *msg
)
39 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
,
40 NULL
, err
, 0, (LPTSTR
)&pstr
, 2, NULL
);
41 fprintf(stderr
, "qemu: %s: %s\n", msg
, pstr
);
46 void qemu_mutex_init(QemuMutex
*mutex
)
48 InitializeSRWLock(&mutex
->lock
);
49 mutex
->initialized
= true;
52 void qemu_mutex_destroy(QemuMutex
*mutex
)
54 assert(mutex
->initialized
);
55 mutex
->initialized
= false;
56 InitializeSRWLock(&mutex
->lock
);
59 void qemu_mutex_lock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
61 assert(mutex
->initialized
);
62 trace_qemu_mutex_lock(mutex
, file
, line
);
64 AcquireSRWLockExclusive(&mutex
->lock
);
65 trace_qemu_mutex_locked(mutex
, file
, line
);
68 int qemu_mutex_trylock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
72 assert(mutex
->initialized
);
73 owned
= TryAcquireSRWLockExclusive(&mutex
->lock
);
75 trace_qemu_mutex_locked(mutex
, file
, line
);
81 void qemu_mutex_unlock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
83 assert(mutex
->initialized
);
84 trace_qemu_mutex_unlock(mutex
, file
, line
);
85 ReleaseSRWLockExclusive(&mutex
->lock
);
88 void qemu_rec_mutex_init(QemuRecMutex
*mutex
)
90 InitializeCriticalSection(&mutex
->lock
);
91 mutex
->initialized
= true;
94 void qemu_rec_mutex_destroy(QemuRecMutex
*mutex
)
96 assert(mutex
->initialized
);
97 mutex
->initialized
= false;
98 DeleteCriticalSection(&mutex
->lock
);
101 void qemu_rec_mutex_lock(QemuRecMutex
*mutex
)
103 assert(mutex
->initialized
);
104 EnterCriticalSection(&mutex
->lock
);
107 int qemu_rec_mutex_trylock(QemuRecMutex
*mutex
)
109 assert(mutex
->initialized
);
110 return !TryEnterCriticalSection(&mutex
->lock
);
113 void qemu_rec_mutex_unlock(QemuRecMutex
*mutex
)
115 assert(mutex
->initialized
);
116 LeaveCriticalSection(&mutex
->lock
);
119 void qemu_cond_init(QemuCond
*cond
)
121 memset(cond
, 0, sizeof(*cond
));
122 InitializeConditionVariable(&cond
->var
);
123 cond
->initialized
= true;
126 void qemu_cond_destroy(QemuCond
*cond
)
128 assert(cond
->initialized
);
129 cond
->initialized
= false;
130 InitializeConditionVariable(&cond
->var
);
133 void qemu_cond_signal(QemuCond
*cond
)
135 assert(cond
->initialized
);
136 WakeConditionVariable(&cond
->var
);
139 void qemu_cond_broadcast(QemuCond
*cond
)
141 assert(cond
->initialized
);
142 WakeAllConditionVariable(&cond
->var
);
145 void qemu_cond_wait_impl(QemuCond
*cond
, QemuMutex
*mutex
, const char *file
, const int line
)
147 assert(cond
->initialized
);
148 trace_qemu_mutex_unlock(mutex
, file
, line
);
149 SleepConditionVariableSRW(&cond
->var
, &mutex
->lock
, INFINITE
, 0);
150 trace_qemu_mutex_locked(mutex
, file
, line
);
153 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
156 sem
->sema
= CreateSemaphore(NULL
, init
, LONG_MAX
, NULL
);
157 sem
->initialized
= true;
160 void qemu_sem_destroy(QemuSemaphore
*sem
)
162 assert(sem
->initialized
);
163 sem
->initialized
= false;
164 CloseHandle(sem
->sema
);
167 void qemu_sem_post(QemuSemaphore
*sem
)
169 assert(sem
->initialized
);
170 ReleaseSemaphore(sem
->sema
, 1, NULL
);
173 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
177 assert(sem
->initialized
);
178 rc
= WaitForSingleObject(sem
->sema
, ms
);
179 if (rc
== WAIT_OBJECT_0
) {
182 if (rc
!= WAIT_TIMEOUT
) {
183 error_exit(GetLastError(), __func__
);
188 void qemu_sem_wait(QemuSemaphore
*sem
)
190 assert(sem
->initialized
);
191 if (WaitForSingleObject(sem
->sema
, INFINITE
) != WAIT_OBJECT_0
) {
192 error_exit(GetLastError(), __func__
);
196 /* Wrap a Win32 manual-reset event with a fast userspace path. The idea
197 * is to reset the Win32 event lazily, as part of a test-reset-test-wait
198 * sequence. Such a sequence is, indeed, how QemuEvents are used by
199 * RCU and other subsystems!
202 * - free->set, when setting the event
203 * - busy->set, when setting the event, followed by SetEvent
204 * - set->free, when resetting the event
205 * - free->busy, when waiting
207 * set->busy does not happen (it can be observed from the outside but
208 * it really is set->free->busy).
210 * busy->free provably cannot happen; to enforce it, the set->free transition
211 * is done with an OR, which becomes a no-op if the event has concurrently
212 * transitioned to free or busy (and is faster than cmpxchg).
219 void qemu_event_init(QemuEvent
*ev
, bool init
)
222 ev
->event
= CreateEvent(NULL
, TRUE
, TRUE
, NULL
);
223 ev
->value
= (init
? EV_SET
: EV_FREE
);
224 ev
->initialized
= true;
227 void qemu_event_destroy(QemuEvent
*ev
)
229 assert(ev
->initialized
);
230 ev
->initialized
= false;
231 CloseHandle(ev
->event
);
234 void qemu_event_set(QemuEvent
*ev
)
236 assert(ev
->initialized
);
237 /* qemu_event_set has release semantics, but because it *loads*
238 * ev->value we need a full memory barrier here.
241 if (atomic_read(&ev
->value
) != EV_SET
) {
242 if (atomic_xchg(&ev
->value
, EV_SET
) == EV_BUSY
) {
243 /* There were waiters, wake them up. */
249 void qemu_event_reset(QemuEvent
*ev
)
253 assert(ev
->initialized
);
254 value
= atomic_read(&ev
->value
);
256 if (value
== EV_SET
) {
257 /* If there was a concurrent reset (or even reset+wait),
258 * do nothing. Otherwise change EV_SET->EV_FREE.
260 atomic_or(&ev
->value
, EV_FREE
);
264 void qemu_event_wait(QemuEvent
*ev
)
268 assert(ev
->initialized
);
269 value
= atomic_read(&ev
->value
);
271 if (value
!= EV_SET
) {
272 if (value
== EV_FREE
) {
273 /* qemu_event_set is not yet going to call SetEvent, but we are
274 * going to do another check for EV_SET below when setting EV_BUSY.
275 * At that point it is safe to call WaitForSingleObject.
277 ResetEvent(ev
->event
);
279 /* Tell qemu_event_set that there are waiters. No need to retry
280 * because there cannot be a concurent busy->free transition.
281 * After the CAS, the event will be either set or busy.
283 if (atomic_cmpxchg(&ev
->value
, EV_FREE
, EV_BUSY
) == EV_SET
) {
289 if (value
== EV_BUSY
) {
290 WaitForSingleObject(ev
->event
, INFINITE
);
295 struct QemuThreadData
{
296 /* Passed to win32_start_routine. */
297 void *(*start_routine
)(void *);
302 /* Only used for joinable threads. */
308 static bool atexit_registered
;
309 static NotifierList main_thread_exit
;
311 static __thread QemuThreadData
*qemu_thread_data
;
313 static void run_main_thread_exit(void)
315 notifier_list_notify(&main_thread_exit
, NULL
);
318 void qemu_thread_atexit_add(Notifier
*notifier
)
320 if (!qemu_thread_data
) {
321 if (!atexit_registered
) {
322 atexit_registered
= true;
323 atexit(run_main_thread_exit
);
325 notifier_list_add(&main_thread_exit
, notifier
);
327 notifier_list_add(&qemu_thread_data
->exit
, notifier
);
331 void qemu_thread_atexit_remove(Notifier
*notifier
)
333 notifier_remove(notifier
);
336 static unsigned __stdcall
win32_start_routine(void *arg
)
338 QemuThreadData
*data
= (QemuThreadData
*) arg
;
339 void *(*start_routine
)(void *) = data
->start_routine
;
340 void *thread_arg
= data
->arg
;
342 qemu_thread_data
= data
;
343 qemu_thread_exit(start_routine(thread_arg
));
347 void qemu_thread_exit(void *arg
)
349 QemuThreadData
*data
= qemu_thread_data
;
351 notifier_list_notify(&data
->exit
, NULL
);
352 if (data
->mode
== QEMU_THREAD_JOINABLE
) {
354 EnterCriticalSection(&data
->cs
);
356 LeaveCriticalSection(&data
->cs
);
363 void *qemu_thread_join(QemuThread
*thread
)
365 QemuThreadData
*data
;
370 if (data
->mode
== QEMU_THREAD_DETACHED
) {
375 * Because multiple copies of the QemuThread can exist via
376 * qemu_thread_get_self, we need to store a value that cannot
377 * leak there. The simplest, non racy way is to store the TID,
378 * discard the handle that _beginthreadex gives back, and
379 * get another copy of the handle here.
381 handle
= qemu_thread_get_handle(thread
);
383 WaitForSingleObject(handle
, INFINITE
);
387 DeleteCriticalSection(&data
->cs
);
392 void qemu_thread_create(QemuThread
*thread
, const char *name
,
393 void *(*start_routine
)(void *),
397 struct QemuThreadData
*data
;
399 data
= g_malloc(sizeof *data
);
400 data
->start_routine
= start_routine
;
403 data
->exited
= false;
404 notifier_list_init(&data
->exit
);
406 if (data
->mode
!= QEMU_THREAD_DETACHED
) {
407 InitializeCriticalSection(&data
->cs
);
410 hThread
= (HANDLE
) _beginthreadex(NULL
, 0, win32_start_routine
,
411 data
, 0, &thread
->tid
);
413 error_exit(GetLastError(), __func__
);
415 CloseHandle(hThread
);
419 void qemu_thread_get_self(QemuThread
*thread
)
421 thread
->data
= qemu_thread_data
;
422 thread
->tid
= GetCurrentThreadId();
425 HANDLE
qemu_thread_get_handle(QemuThread
*thread
)
427 QemuThreadData
*data
;
431 if (data
->mode
== QEMU_THREAD_DETACHED
) {
435 EnterCriticalSection(&data
->cs
);
437 handle
= OpenThread(SYNCHRONIZE
| THREAD_SUSPEND_RESUME
|
438 THREAD_SET_CONTEXT
, FALSE
, thread
->tid
);
442 LeaveCriticalSection(&data
->cs
);
446 bool qemu_thread_is_self(QemuThread
*thread
)
448 return GetCurrentThreadId() == thread
->tid
;