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.
14 #include "qemu/osdep.h"
15 #include "qemu/thread.h"
16 #include "qemu/notify.h"
17 #include "qemu-thread-common.h"
20 static bool name_threads
;
22 typedef HRESULT (WINAPI
*pSetThreadDescription
) (HANDLE hThread
,
23 PCWSTR lpThreadDescription
);
24 static pSetThreadDescription SetThreadDescriptionFunc
;
25 static HMODULE kernel32_module
;
27 static bool load_set_thread_description(void)
29 static gsize _init_once
= 0;
31 if (g_once_init_enter(&_init_once
)) {
32 kernel32_module
= LoadLibrary("kernel32.dll");
33 if (kernel32_module
) {
34 #pragma GCC diagnostic ignored "-Wcast-function-type"
35 SetThreadDescriptionFunc
=
36 (pSetThreadDescription
)GetProcAddress(kernel32_module
,
37 "SetThreadDescription");
38 if (!SetThreadDescriptionFunc
) {
39 FreeLibrary(kernel32_module
);
42 g_once_init_leave(&_init_once
, 1);
45 return !!SetThreadDescriptionFunc
;
48 void qemu_thread_naming(bool enable
)
50 name_threads
= enable
;
52 if (enable
&& !load_set_thread_description()) {
53 fprintf(stderr
, "qemu: thread naming not supported on this host\n");
58 static void error_exit(int err
, const char *msg
)
62 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
,
63 NULL
, err
, 0, (LPTSTR
)&pstr
, 2, NULL
);
64 fprintf(stderr
, "qemu: %s: %s\n", msg
, pstr
);
69 void qemu_mutex_init(QemuMutex
*mutex
)
71 InitializeSRWLock(&mutex
->lock
);
72 qemu_mutex_post_init(mutex
);
75 void qemu_mutex_destroy(QemuMutex
*mutex
)
77 assert(mutex
->initialized
);
78 mutex
->initialized
= false;
79 InitializeSRWLock(&mutex
->lock
);
82 void qemu_mutex_lock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
84 assert(mutex
->initialized
);
85 qemu_mutex_pre_lock(mutex
, file
, line
);
86 AcquireSRWLockExclusive(&mutex
->lock
);
87 qemu_mutex_post_lock(mutex
, file
, line
);
90 int qemu_mutex_trylock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
94 assert(mutex
->initialized
);
95 owned
= TryAcquireSRWLockExclusive(&mutex
->lock
);
97 qemu_mutex_post_lock(mutex
, file
, line
);
103 void qemu_mutex_unlock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
105 assert(mutex
->initialized
);
106 qemu_mutex_pre_unlock(mutex
, file
, line
);
107 ReleaseSRWLockExclusive(&mutex
->lock
);
110 void qemu_rec_mutex_init(QemuRecMutex
*mutex
)
112 InitializeCriticalSection(&mutex
->lock
);
113 mutex
->initialized
= true;
116 void qemu_rec_mutex_destroy(QemuRecMutex
*mutex
)
118 assert(mutex
->initialized
);
119 mutex
->initialized
= false;
120 DeleteCriticalSection(&mutex
->lock
);
123 void qemu_rec_mutex_lock_impl(QemuRecMutex
*mutex
, const char *file
, int line
)
125 assert(mutex
->initialized
);
126 EnterCriticalSection(&mutex
->lock
);
129 int qemu_rec_mutex_trylock_impl(QemuRecMutex
*mutex
, const char *file
, int line
)
131 assert(mutex
->initialized
);
132 return !TryEnterCriticalSection(&mutex
->lock
);
135 void qemu_rec_mutex_unlock_impl(QemuRecMutex
*mutex
, const char *file
, int line
)
137 assert(mutex
->initialized
);
138 LeaveCriticalSection(&mutex
->lock
);
141 void qemu_cond_init(QemuCond
*cond
)
143 memset(cond
, 0, sizeof(*cond
));
144 InitializeConditionVariable(&cond
->var
);
145 cond
->initialized
= true;
148 void qemu_cond_destroy(QemuCond
*cond
)
150 assert(cond
->initialized
);
151 cond
->initialized
= false;
152 InitializeConditionVariable(&cond
->var
);
155 void qemu_cond_signal(QemuCond
*cond
)
157 assert(cond
->initialized
);
158 WakeConditionVariable(&cond
->var
);
161 void qemu_cond_broadcast(QemuCond
*cond
)
163 assert(cond
->initialized
);
164 WakeAllConditionVariable(&cond
->var
);
167 void qemu_cond_wait_impl(QemuCond
*cond
, QemuMutex
*mutex
, const char *file
, const int line
)
169 assert(cond
->initialized
);
170 qemu_mutex_pre_unlock(mutex
, file
, line
);
171 SleepConditionVariableSRW(&cond
->var
, &mutex
->lock
, INFINITE
, 0);
172 qemu_mutex_post_lock(mutex
, file
, line
);
175 bool qemu_cond_timedwait_impl(QemuCond
*cond
, QemuMutex
*mutex
, int ms
,
176 const char *file
, const int line
)
180 assert(cond
->initialized
);
181 trace_qemu_mutex_unlock(mutex
, file
, line
);
182 if (!SleepConditionVariableSRW(&cond
->var
, &mutex
->lock
, ms
, 0)) {
185 trace_qemu_mutex_locked(mutex
, file
, line
);
186 if (rc
&& rc
!= ERROR_TIMEOUT
) {
187 error_exit(rc
, __func__
);
189 return rc
!= ERROR_TIMEOUT
;
192 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
195 sem
->sema
= CreateSemaphore(NULL
, init
, LONG_MAX
, NULL
);
196 sem
->initialized
= true;
199 void qemu_sem_destroy(QemuSemaphore
*sem
)
201 assert(sem
->initialized
);
202 sem
->initialized
= false;
203 CloseHandle(sem
->sema
);
206 void qemu_sem_post(QemuSemaphore
*sem
)
208 assert(sem
->initialized
);
209 ReleaseSemaphore(sem
->sema
, 1, NULL
);
212 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
216 assert(sem
->initialized
);
217 rc
= WaitForSingleObject(sem
->sema
, ms
);
218 if (rc
== WAIT_OBJECT_0
) {
221 if (rc
!= WAIT_TIMEOUT
) {
222 error_exit(GetLastError(), __func__
);
227 void qemu_sem_wait(QemuSemaphore
*sem
)
229 assert(sem
->initialized
);
230 if (WaitForSingleObject(sem
->sema
, INFINITE
) != WAIT_OBJECT_0
) {
231 error_exit(GetLastError(), __func__
);
235 /* Wrap a Win32 manual-reset event with a fast userspace path. The idea
236 * is to reset the Win32 event lazily, as part of a test-reset-test-wait
237 * sequence. Such a sequence is, indeed, how QemuEvents are used by
238 * RCU and other subsystems!
241 * - free->set, when setting the event
242 * - busy->set, when setting the event, followed by SetEvent
243 * - set->free, when resetting the event
244 * - free->busy, when waiting
246 * set->busy does not happen (it can be observed from the outside but
247 * it really is set->free->busy).
249 * busy->free provably cannot happen; to enforce it, the set->free transition
250 * is done with an OR, which becomes a no-op if the event has concurrently
251 * transitioned to free or busy (and is faster than cmpxchg).
258 void qemu_event_init(QemuEvent
*ev
, bool init
)
261 ev
->event
= CreateEvent(NULL
, TRUE
, TRUE
, NULL
);
262 ev
->value
= (init
? EV_SET
: EV_FREE
);
263 ev
->initialized
= true;
266 void qemu_event_destroy(QemuEvent
*ev
)
268 assert(ev
->initialized
);
269 ev
->initialized
= false;
270 CloseHandle(ev
->event
);
273 void qemu_event_set(QemuEvent
*ev
)
275 assert(ev
->initialized
);
278 * Pairs with both qemu_event_reset() and qemu_event_wait().
280 * qemu_event_set has release semantics, but because it *loads*
281 * ev->value we need a full memory barrier here.
284 if (qatomic_read(&ev
->value
) != EV_SET
) {
285 int old
= qatomic_xchg(&ev
->value
, EV_SET
);
287 /* Pairs with memory barrier after ResetEvent. */
289 if (old
== EV_BUSY
) {
290 /* There were waiters, wake them up. */
296 void qemu_event_reset(QemuEvent
*ev
)
298 assert(ev
->initialized
);
301 * If there was a concurrent reset (or even reset+wait),
302 * do nothing. Otherwise change EV_SET->EV_FREE.
304 qatomic_or(&ev
->value
, EV_FREE
);
307 * Order reset before checking the condition in the caller.
308 * Pairs with the first memory barrier in qemu_event_set().
313 void qemu_event_wait(QemuEvent
*ev
)
317 assert(ev
->initialized
);
320 * qemu_event_wait must synchronize with qemu_event_set even if it does
321 * not go down the slow path, so this load-acquire is needed that
322 * synchronizes with the first memory barrier in qemu_event_set().
324 * If we do go down the slow path, there is no requirement at all: we
325 * might miss a qemu_event_set() here but ultimately the memory barrier in
326 * qemu_futex_wait() will ensure the check is done correctly.
328 value
= qatomic_load_acquire(&ev
->value
);
329 if (value
!= EV_SET
) {
330 if (value
== EV_FREE
) {
332 * Here the underlying kernel event is reset, but qemu_event_set is
333 * not yet going to call SetEvent. However, there will be another
334 * check for EV_SET below when setting EV_BUSY. At that point it
335 * is safe to call WaitForSingleObject.
337 ResetEvent(ev
->event
);
340 * It is not clear whether ResetEvent provides this barrier; kernel
341 * APIs (KeResetEvent/KeClearEvent) do not. Better safe than sorry!
346 * Leave the event reset and tell qemu_event_set that there are
347 * waiters. No need to retry, because there cannot be a concurrent
348 * busy->free transition. After the CAS, the event will be either
351 if (qatomic_cmpxchg(&ev
->value
, EV_FREE
, EV_BUSY
) == EV_SET
) {
357 * ev->value is now EV_BUSY. Since we didn't observe EV_SET,
358 * qemu_event_set() must observe EV_BUSY and call SetEvent().
360 WaitForSingleObject(ev
->event
, INFINITE
);
364 struct QemuThreadData
{
365 /* Passed to win32_start_routine. */
366 void *(*start_routine
)(void *);
371 /* Only used for joinable threads. */
377 static bool atexit_registered
;
378 static NotifierList main_thread_exit
;
380 static __thread QemuThreadData
*qemu_thread_data
;
382 static void run_main_thread_exit(void)
384 notifier_list_notify(&main_thread_exit
, NULL
);
387 void qemu_thread_atexit_add(Notifier
*notifier
)
389 if (!qemu_thread_data
) {
390 if (!atexit_registered
) {
391 atexit_registered
= true;
392 atexit(run_main_thread_exit
);
394 notifier_list_add(&main_thread_exit
, notifier
);
396 notifier_list_add(&qemu_thread_data
->exit
, notifier
);
400 void qemu_thread_atexit_remove(Notifier
*notifier
)
402 notifier_remove(notifier
);
405 static unsigned __stdcall
win32_start_routine(void *arg
)
407 QemuThreadData
*data
= (QemuThreadData
*) arg
;
408 void *(*start_routine
)(void *) = data
->start_routine
;
409 void *thread_arg
= data
->arg
;
411 qemu_thread_data
= data
;
412 qemu_thread_exit(start_routine(thread_arg
));
416 void qemu_thread_exit(void *arg
)
418 QemuThreadData
*data
= qemu_thread_data
;
420 notifier_list_notify(&data
->exit
, NULL
);
421 if (data
->mode
== QEMU_THREAD_JOINABLE
) {
423 EnterCriticalSection(&data
->cs
);
425 LeaveCriticalSection(&data
->cs
);
432 void *qemu_thread_join(QemuThread
*thread
)
434 QemuThreadData
*data
;
439 if (data
->mode
== QEMU_THREAD_DETACHED
) {
444 * Because multiple copies of the QemuThread can exist via
445 * qemu_thread_get_self, we need to store a value that cannot
446 * leak there. The simplest, non racy way is to store the TID,
447 * discard the handle that _beginthreadex gives back, and
448 * get another copy of the handle here.
450 handle
= qemu_thread_get_handle(thread
);
452 WaitForSingleObject(handle
, INFINITE
);
456 DeleteCriticalSection(&data
->cs
);
461 static bool set_thread_description(HANDLE h
, const char *name
)
464 g_autofree
wchar_t *namew
= NULL
;
466 if (!load_set_thread_description()) {
470 namew
= g_utf8_to_utf16(name
, -1, NULL
, NULL
, NULL
);
475 hr
= SetThreadDescriptionFunc(h
, namew
);
477 return SUCCEEDED(hr
);
480 void qemu_thread_create(QemuThread
*thread
, const char *name
,
481 void *(*start_routine
)(void *),
485 struct QemuThreadData
*data
;
487 data
= g_malloc(sizeof *data
);
488 data
->start_routine
= start_routine
;
491 data
->exited
= false;
492 notifier_list_init(&data
->exit
);
494 if (data
->mode
!= QEMU_THREAD_DETACHED
) {
495 InitializeCriticalSection(&data
->cs
);
498 hThread
= (HANDLE
) _beginthreadex(NULL
, 0, win32_start_routine
,
499 data
, 0, &thread
->tid
);
501 error_exit(GetLastError(), __func__
);
503 if (name_threads
&& name
&& !set_thread_description(hThread
, name
)) {
504 fprintf(stderr
, "qemu: failed to set thread description: %s\n", name
);
506 CloseHandle(hThread
);
511 int qemu_thread_set_affinity(QemuThread
*thread
, unsigned long *host_cpus
,
517 int qemu_thread_get_affinity(QemuThread
*thread
, unsigned long **host_cpus
,
518 unsigned long *nbits
)
523 void qemu_thread_get_self(QemuThread
*thread
)
525 thread
->data
= qemu_thread_data
;
526 thread
->tid
= GetCurrentThreadId();
529 HANDLE
qemu_thread_get_handle(QemuThread
*thread
)
531 QemuThreadData
*data
;
535 if (data
->mode
== QEMU_THREAD_DETACHED
) {
539 EnterCriticalSection(&data
->cs
);
541 handle
= OpenThread(SYNCHRONIZE
| THREAD_SUSPEND_RESUME
|
542 THREAD_SET_CONTEXT
, FALSE
, thread
->tid
);
546 LeaveCriticalSection(&data
->cs
);
550 bool qemu_thread_is_self(QemuThread
*thread
)
552 return GetCurrentThreadId() == thread
->tid
;