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
);
51 void qemu_mutex_destroy(QemuMutex
*mutex
)
53 InitializeSRWLock(&mutex
->lock
);
56 void qemu_mutex_lock(QemuMutex
*mutex
)
58 AcquireSRWLockExclusive(&mutex
->lock
);
59 trace_qemu_mutex_locked(mutex
);
62 int qemu_mutex_trylock(QemuMutex
*mutex
)
66 owned
= TryAcquireSRWLockExclusive(&mutex
->lock
);
68 trace_qemu_mutex_locked(mutex
);
74 void qemu_mutex_unlock(QemuMutex
*mutex
)
76 trace_qemu_mutex_unlocked(mutex
);
77 ReleaseSRWLockExclusive(&mutex
->lock
);
80 void qemu_rec_mutex_init(QemuRecMutex
*mutex
)
82 InitializeCriticalSection(&mutex
->lock
);
85 void qemu_rec_mutex_destroy(QemuRecMutex
*mutex
)
87 DeleteCriticalSection(&mutex
->lock
);
90 void qemu_rec_mutex_lock(QemuRecMutex
*mutex
)
92 EnterCriticalSection(&mutex
->lock
);
95 int qemu_rec_mutex_trylock(QemuRecMutex
*mutex
)
97 return !TryEnterCriticalSection(&mutex
->lock
);
100 void qemu_rec_mutex_unlock(QemuRecMutex
*mutex
)
102 LeaveCriticalSection(&mutex
->lock
);
105 void qemu_cond_init(QemuCond
*cond
)
107 memset(cond
, 0, sizeof(*cond
));
108 InitializeConditionVariable(&cond
->var
);
111 void qemu_cond_destroy(QemuCond
*cond
)
113 InitializeConditionVariable(&cond
->var
);
116 void qemu_cond_signal(QemuCond
*cond
)
118 WakeConditionVariable(&cond
->var
);
121 void qemu_cond_broadcast(QemuCond
*cond
)
123 WakeAllConditionVariable(&cond
->var
);
126 void qemu_cond_wait(QemuCond
*cond
, QemuMutex
*mutex
)
128 trace_qemu_mutex_unlocked(mutex
);
129 SleepConditionVariableSRW(&cond
->var
, &mutex
->lock
, INFINITE
, 0);
130 trace_qemu_mutex_locked(mutex
);
133 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
136 sem
->sema
= CreateSemaphore(NULL
, init
, LONG_MAX
, NULL
);
139 void qemu_sem_destroy(QemuSemaphore
*sem
)
141 CloseHandle(sem
->sema
);
144 void qemu_sem_post(QemuSemaphore
*sem
)
146 ReleaseSemaphore(sem
->sema
, 1, NULL
);
149 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
151 int rc
= WaitForSingleObject(sem
->sema
, ms
);
152 if (rc
== WAIT_OBJECT_0
) {
155 if (rc
!= WAIT_TIMEOUT
) {
156 error_exit(GetLastError(), __func__
);
161 void qemu_sem_wait(QemuSemaphore
*sem
)
163 if (WaitForSingleObject(sem
->sema
, INFINITE
) != WAIT_OBJECT_0
) {
164 error_exit(GetLastError(), __func__
);
168 /* Wrap a Win32 manual-reset event with a fast userspace path. The idea
169 * is to reset the Win32 event lazily, as part of a test-reset-test-wait
170 * sequence. Such a sequence is, indeed, how QemuEvents are used by
171 * RCU and other subsystems!
174 * - free->set, when setting the event
175 * - busy->set, when setting the event, followed by SetEvent
176 * - set->free, when resetting the event
177 * - free->busy, when waiting
179 * set->busy does not happen (it can be observed from the outside but
180 * it really is set->free->busy).
182 * busy->free provably cannot happen; to enforce it, the set->free transition
183 * is done with an OR, which becomes a no-op if the event has concurrently
184 * transitioned to free or busy (and is faster than cmpxchg).
191 void qemu_event_init(QemuEvent
*ev
, bool init
)
194 ev
->event
= CreateEvent(NULL
, TRUE
, TRUE
, NULL
);
195 ev
->value
= (init
? EV_SET
: EV_FREE
);
198 void qemu_event_destroy(QemuEvent
*ev
)
200 CloseHandle(ev
->event
);
203 void qemu_event_set(QemuEvent
*ev
)
205 /* qemu_event_set has release semantics, but because it *loads*
206 * ev->value we need a full memory barrier here.
209 if (atomic_read(&ev
->value
) != EV_SET
) {
210 if (atomic_xchg(&ev
->value
, EV_SET
) == EV_BUSY
) {
211 /* There were waiters, wake them up. */
217 void qemu_event_reset(QemuEvent
*ev
)
221 value
= atomic_read(&ev
->value
);
223 if (value
== EV_SET
) {
224 /* If there was a concurrent reset (or even reset+wait),
225 * do nothing. Otherwise change EV_SET->EV_FREE.
227 atomic_or(&ev
->value
, EV_FREE
);
231 void qemu_event_wait(QemuEvent
*ev
)
235 value
= atomic_read(&ev
->value
);
237 if (value
!= EV_SET
) {
238 if (value
== EV_FREE
) {
239 /* qemu_event_set is not yet going to call SetEvent, but we are
240 * going to do another check for EV_SET below when setting EV_BUSY.
241 * At that point it is safe to call WaitForSingleObject.
243 ResetEvent(ev
->event
);
245 /* Tell qemu_event_set that there are waiters. No need to retry
246 * because there cannot be a concurent busy->free transition.
247 * After the CAS, the event will be either set or busy.
249 if (atomic_cmpxchg(&ev
->value
, EV_FREE
, EV_BUSY
) == EV_SET
) {
255 if (value
== EV_BUSY
) {
256 WaitForSingleObject(ev
->event
, INFINITE
);
261 struct QemuThreadData
{
262 /* Passed to win32_start_routine. */
263 void *(*start_routine
)(void *);
268 /* Only used for joinable threads. */
274 static bool atexit_registered
;
275 static NotifierList main_thread_exit
;
277 static __thread QemuThreadData
*qemu_thread_data
;
279 static void run_main_thread_exit(void)
281 notifier_list_notify(&main_thread_exit
, NULL
);
284 void qemu_thread_atexit_add(Notifier
*notifier
)
286 if (!qemu_thread_data
) {
287 if (!atexit_registered
) {
288 atexit_registered
= true;
289 atexit(run_main_thread_exit
);
291 notifier_list_add(&main_thread_exit
, notifier
);
293 notifier_list_add(&qemu_thread_data
->exit
, notifier
);
297 void qemu_thread_atexit_remove(Notifier
*notifier
)
299 notifier_remove(notifier
);
302 static unsigned __stdcall
win32_start_routine(void *arg
)
304 QemuThreadData
*data
= (QemuThreadData
*) arg
;
305 void *(*start_routine
)(void *) = data
->start_routine
;
306 void *thread_arg
= data
->arg
;
308 qemu_thread_data
= data
;
309 qemu_thread_exit(start_routine(thread_arg
));
313 void qemu_thread_exit(void *arg
)
315 QemuThreadData
*data
= qemu_thread_data
;
317 notifier_list_notify(&data
->exit
, NULL
);
318 if (data
->mode
== QEMU_THREAD_JOINABLE
) {
320 EnterCriticalSection(&data
->cs
);
322 LeaveCriticalSection(&data
->cs
);
329 void *qemu_thread_join(QemuThread
*thread
)
331 QemuThreadData
*data
;
336 if (data
->mode
== QEMU_THREAD_DETACHED
) {
341 * Because multiple copies of the QemuThread can exist via
342 * qemu_thread_get_self, we need to store a value that cannot
343 * leak there. The simplest, non racy way is to store the TID,
344 * discard the handle that _beginthreadex gives back, and
345 * get another copy of the handle here.
347 handle
= qemu_thread_get_handle(thread
);
349 WaitForSingleObject(handle
, INFINITE
);
353 DeleteCriticalSection(&data
->cs
);
358 void qemu_thread_create(QemuThread
*thread
, const char *name
,
359 void *(*start_routine
)(void *),
363 struct QemuThreadData
*data
;
365 data
= g_malloc(sizeof *data
);
366 data
->start_routine
= start_routine
;
369 data
->exited
= false;
370 notifier_list_init(&data
->exit
);
372 if (data
->mode
!= QEMU_THREAD_DETACHED
) {
373 InitializeCriticalSection(&data
->cs
);
376 hThread
= (HANDLE
) _beginthreadex(NULL
, 0, win32_start_routine
,
377 data
, 0, &thread
->tid
);
379 error_exit(GetLastError(), __func__
);
381 CloseHandle(hThread
);
385 void qemu_thread_get_self(QemuThread
*thread
)
387 thread
->data
= qemu_thread_data
;
388 thread
->tid
= GetCurrentThreadId();
391 HANDLE
qemu_thread_get_handle(QemuThread
*thread
)
393 QemuThreadData
*data
;
397 if (data
->mode
== QEMU_THREAD_DETACHED
) {
401 EnterCriticalSection(&data
->cs
);
403 handle
= OpenThread(SYNCHRONIZE
| THREAD_SUSPEND_RESUME
|
404 THREAD_SET_CONTEXT
, FALSE
, thread
->tid
);
408 LeaveCriticalSection(&data
->cs
);
412 bool qemu_thread_is_self(QemuThread
*thread
)
414 return GetCurrentThreadId() == thread
->tid
;