armv7m: Move NVICState struct definition into header
[qemu/ar7.git] / util / qemu-thread-win32.c
blob29c3e4dd856e15ce5ad41adae309d0cab0f6ea49
1 /*
2 * Win32 implementation for mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2010
6 * Author:
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"
17 #include <process.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)
31 char *pstr;
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);
36 LocalFree(pstr);
37 abort();
40 void qemu_mutex_init(QemuMutex *mutex)
42 mutex->owner = 0;
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
57 * using them as such.
59 assert(mutex->owner == 0);
60 mutex->owner = GetCurrentThreadId();
63 int qemu_mutex_trylock(QemuMutex *mutex)
65 int owned;
67 owned = TryEnterCriticalSection(&mutex->lock);
68 if (owned) {
69 assert(mutex->owner == 0);
70 mutex->owner = GetCurrentThreadId();
72 return !owned;
75 void qemu_mutex_unlock(QemuMutex *mutex)
77 assert(mutex->owner == GetCurrentThreadId());
78 mutex->owner = 0;
79 LeaveCriticalSection(&mutex->lock);
82 void qemu_rec_mutex_init(QemuRecMutex *mutex)
84 InitializeCriticalSection(&mutex->lock);
87 void qemu_rec_mutex_destroy(QemuRecMutex *mutex)
89 DeleteCriticalSection(&mutex->lock);
92 void qemu_rec_mutex_lock(QemuRecMutex *mutex)
94 EnterCriticalSection(&mutex->lock);
97 int qemu_rec_mutex_trylock(QemuRecMutex *mutex)
99 return !TryEnterCriticalSection(&mutex->lock);
102 void qemu_rec_mutex_unlock(QemuRecMutex *mutex)
104 LeaveCriticalSection(&mutex->lock);
107 void qemu_cond_init(QemuCond *cond)
109 memset(cond, 0, sizeof(*cond));
111 cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
112 if (!cond->sema) {
113 error_exit(GetLastError(), __func__);
115 cond->continue_event = CreateEvent(NULL, /* security */
116 FALSE, /* auto-reset */
117 FALSE, /* not signaled */
118 NULL); /* name */
119 if (!cond->continue_event) {
120 error_exit(GetLastError(), __func__);
124 void qemu_cond_destroy(QemuCond *cond)
126 BOOL result;
127 result = CloseHandle(cond->continue_event);
128 if (!result) {
129 error_exit(GetLastError(), __func__);
131 cond->continue_event = 0;
132 result = CloseHandle(cond->sema);
133 if (!result) {
134 error_exit(GetLastError(), __func__);
136 cond->sema = 0;
139 void qemu_cond_signal(QemuCond *cond)
141 DWORD result;
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) {
149 return;
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,
160 INFINITE, FALSE);
161 if (result == WAIT_ABANDONED || result == WAIT_FAILED) {
162 error_exit(GetLastError(), __func__);
166 void qemu_cond_broadcast(QemuCond *cond)
168 BOOLEAN result;
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) {
174 return;
177 cond->target = 0;
178 result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
179 if (!result) {
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);
194 void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
197 * This access is protected under the mutex.
199 cond->waiters++;
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);
230 void qemu_sem_init(QemuSemaphore *sem, int init)
232 /* Manual reset. */
233 sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL);
236 void qemu_sem_destroy(QemuSemaphore *sem)
238 CloseHandle(sem->sema);
241 void qemu_sem_post(QemuSemaphore *sem)
243 ReleaseSemaphore(sem->sema, 1, NULL);
246 int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
248 int rc = WaitForSingleObject(sem->sema, ms);
249 if (rc == WAIT_OBJECT_0) {
250 return 0;
252 if (rc != WAIT_TIMEOUT) {
253 error_exit(GetLastError(), __func__);
255 return -1;
258 void qemu_sem_wait(QemuSemaphore *sem)
260 if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) {
261 error_exit(GetLastError(), __func__);
265 /* Wrap a Win32 manual-reset event with a fast userspace path. The idea
266 * is to reset the Win32 event lazily, as part of a test-reset-test-wait
267 * sequence. Such a sequence is, indeed, how QemuEvents are used by
268 * RCU and other subsystems!
270 * Valid transitions:
271 * - free->set, when setting the event
272 * - busy->set, when setting the event, followed by SetEvent
273 * - set->free, when resetting the event
274 * - free->busy, when waiting
276 * set->busy does not happen (it can be observed from the outside but
277 * it really is set->free->busy).
279 * busy->free provably cannot happen; to enforce it, the set->free transition
280 * is done with an OR, which becomes a no-op if the event has concurrently
281 * transitioned to free or busy (and is faster than cmpxchg).
284 #define EV_SET 0
285 #define EV_FREE 1
286 #define EV_BUSY -1
288 void qemu_event_init(QemuEvent *ev, bool init)
290 /* Manual reset. */
291 ev->event = CreateEvent(NULL, TRUE, TRUE, NULL);
292 ev->value = (init ? EV_SET : EV_FREE);
295 void qemu_event_destroy(QemuEvent *ev)
297 CloseHandle(ev->event);
300 void qemu_event_set(QemuEvent *ev)
302 /* qemu_event_set has release semantics, but because it *loads*
303 * ev->value we need a full memory barrier here.
305 smp_mb();
306 if (atomic_read(&ev->value) != EV_SET) {
307 if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
308 /* There were waiters, wake them up. */
309 SetEvent(ev->event);
314 void qemu_event_reset(QemuEvent *ev)
316 unsigned value;
318 value = atomic_read(&ev->value);
319 smp_mb_acquire();
320 if (value == EV_SET) {
321 /* If there was a concurrent reset (or even reset+wait),
322 * do nothing. Otherwise change EV_SET->EV_FREE.
324 atomic_or(&ev->value, EV_FREE);
328 void qemu_event_wait(QemuEvent *ev)
330 unsigned value;
332 value = atomic_read(&ev->value);
333 smp_mb_acquire();
334 if (value != EV_SET) {
335 if (value == EV_FREE) {
336 /* qemu_event_set is not yet going to call SetEvent, but we are
337 * going to do another check for EV_SET below when setting EV_BUSY.
338 * At that point it is safe to call WaitForSingleObject.
340 ResetEvent(ev->event);
342 /* Tell qemu_event_set that there are waiters. No need to retry
343 * because there cannot be a concurent busy->free transition.
344 * After the CAS, the event will be either set or busy.
346 if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
347 value = EV_SET;
348 } else {
349 value = EV_BUSY;
352 if (value == EV_BUSY) {
353 WaitForSingleObject(ev->event, INFINITE);
358 struct QemuThreadData {
359 /* Passed to win32_start_routine. */
360 void *(*start_routine)(void *);
361 void *arg;
362 short mode;
363 NotifierList exit;
365 /* Only used for joinable threads. */
366 bool exited;
367 void *ret;
368 CRITICAL_SECTION cs;
371 static bool atexit_registered;
372 static NotifierList main_thread_exit;
374 static __thread QemuThreadData *qemu_thread_data;
376 static void run_main_thread_exit(void)
378 notifier_list_notify(&main_thread_exit, NULL);
381 void qemu_thread_atexit_add(Notifier *notifier)
383 if (!qemu_thread_data) {
384 if (!atexit_registered) {
385 atexit_registered = true;
386 atexit(run_main_thread_exit);
388 notifier_list_add(&main_thread_exit, notifier);
389 } else {
390 notifier_list_add(&qemu_thread_data->exit, notifier);
394 void qemu_thread_atexit_remove(Notifier *notifier)
396 notifier_remove(notifier);
399 static unsigned __stdcall win32_start_routine(void *arg)
401 QemuThreadData *data = (QemuThreadData *) arg;
402 void *(*start_routine)(void *) = data->start_routine;
403 void *thread_arg = data->arg;
405 qemu_thread_data = data;
406 qemu_thread_exit(start_routine(thread_arg));
407 abort();
410 void qemu_thread_exit(void *arg)
412 QemuThreadData *data = qemu_thread_data;
414 notifier_list_notify(&data->exit, NULL);
415 if (data->mode == QEMU_THREAD_JOINABLE) {
416 data->ret = arg;
417 EnterCriticalSection(&data->cs);
418 data->exited = true;
419 LeaveCriticalSection(&data->cs);
420 } else {
421 g_free(data);
423 _endthreadex(0);
426 void *qemu_thread_join(QemuThread *thread)
428 QemuThreadData *data;
429 void *ret;
430 HANDLE handle;
432 data = thread->data;
433 if (data->mode == QEMU_THREAD_DETACHED) {
434 return NULL;
438 * Because multiple copies of the QemuThread can exist via
439 * qemu_thread_get_self, we need to store a value that cannot
440 * leak there. The simplest, non racy way is to store the TID,
441 * discard the handle that _beginthreadex gives back, and
442 * get another copy of the handle here.
444 handle = qemu_thread_get_handle(thread);
445 if (handle) {
446 WaitForSingleObject(handle, INFINITE);
447 CloseHandle(handle);
449 ret = data->ret;
450 DeleteCriticalSection(&data->cs);
451 g_free(data);
452 return ret;
455 void qemu_thread_create(QemuThread *thread, const char *name,
456 void *(*start_routine)(void *),
457 void *arg, int mode)
459 HANDLE hThread;
460 struct QemuThreadData *data;
462 data = g_malloc(sizeof *data);
463 data->start_routine = start_routine;
464 data->arg = arg;
465 data->mode = mode;
466 data->exited = false;
467 notifier_list_init(&data->exit);
469 if (data->mode != QEMU_THREAD_DETACHED) {
470 InitializeCriticalSection(&data->cs);
473 hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
474 data, 0, &thread->tid);
475 if (!hThread) {
476 error_exit(GetLastError(), __func__);
478 CloseHandle(hThread);
479 thread->data = data;
482 void qemu_thread_get_self(QemuThread *thread)
484 thread->data = qemu_thread_data;
485 thread->tid = GetCurrentThreadId();
488 HANDLE qemu_thread_get_handle(QemuThread *thread)
490 QemuThreadData *data;
491 HANDLE handle;
493 data = thread->data;
494 if (data->mode == QEMU_THREAD_DETACHED) {
495 return NULL;
498 EnterCriticalSection(&data->cs);
499 if (!data->exited) {
500 handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME |
501 THREAD_SET_CONTEXT, FALSE, thread->tid);
502 } else {
503 handle = NULL;
505 LeaveCriticalSection(&data->cs);
506 return handle;
509 bool qemu_thread_is_self(QemuThread *thread)
511 return GetCurrentThreadId() == thread->tid;