]> Git Repo - qemu.git/blame - util/qemu-thread-win32.c
Merge remote-tracking branch 'remotes/jnsnow/tags/bitmaps-pull-request' into staging
[qemu.git] / util / qemu-thread-win32.c
CommitLineData
9257d46d
PB
1/*
2 * Win32 implementation for mutex/cond/thread functions
3 *
4 * Copyright Red Hat, Inc. 2010
5 *
6 * Author:
7 * Paolo Bonzini <[email protected]>
8 *
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.
11 *
12 */
12f8def0
AS
13
14#ifndef _WIN32_WINNT
15#define _WIN32_WINNT 0x0600
16#endif
17
aafd7584 18#include "qemu/osdep.h"
9257d46d 19#include "qemu-common.h"
1de7afc9 20#include "qemu/thread.h"
ef57137f 21#include "qemu/notify.h"
31f5a726 22#include "trace.h"
9257d46d 23#include <process.h>
9257d46d 24
8f480de0
DDAG
25static bool name_threads;
26
27void qemu_thread_naming(bool enable)
28{
29 /* But note we don't actually name them on Windows yet */
30 name_threads = enable;
5c312079
DDAG
31
32 fprintf(stderr, "qemu: thread naming not supported on this host\n");
8f480de0
DDAG
33}
34
9257d46d
PB
35static void error_exit(int err, const char *msg)
36{
37 char *pstr;
38
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);
42 LocalFree(pstr);
53380ac3 43 abort();
9257d46d
PB
44}
45
46void qemu_mutex_init(QemuMutex *mutex)
47{
12f8def0 48 InitializeSRWLock(&mutex->lock);
c096358e 49 mutex->initialized = true;
9257d46d
PB
50}
51
1a290aea
SW
52void qemu_mutex_destroy(QemuMutex *mutex)
53{
c096358e
FZ
54 assert(mutex->initialized);
55 mutex->initialized = false;
12f8def0 56 InitializeSRWLock(&mutex->lock);
1a290aea
SW
57}
58
6c27a0de 59void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
9257d46d 60{
c096358e 61 assert(mutex->initialized);
6c27a0de
AB
62 trace_qemu_mutex_lock(mutex, file, line);
63
12f8def0 64 AcquireSRWLockExclusive(&mutex->lock);
6c27a0de 65 trace_qemu_mutex_locked(mutex, file, line);
9257d46d
PB
66}
67
6c27a0de 68int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
9257d46d
PB
69{
70 int owned;
71
c096358e 72 assert(mutex->initialized);
12f8def0 73 owned = TryAcquireSRWLockExclusive(&mutex->lock);
31f5a726 74 if (owned) {
6c27a0de 75 trace_qemu_mutex_locked(mutex, file, line);
31f5a726
JRZ
76 return 0;
77 }
78 return -EBUSY;
9257d46d
PB
79}
80
6c27a0de 81void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
9257d46d 82{
c096358e 83 assert(mutex->initialized);
6c27a0de 84 trace_qemu_mutex_unlock(mutex, file, line);
12f8def0 85 ReleaseSRWLockExclusive(&mutex->lock);
9257d46d
PB
86}
87
feadec63
PB
88void qemu_rec_mutex_init(QemuRecMutex *mutex)
89{
90 InitializeCriticalSection(&mutex->lock);
c096358e 91 mutex->initialized = true;
feadec63
PB
92}
93
94void qemu_rec_mutex_destroy(QemuRecMutex *mutex)
95{
c096358e
FZ
96 assert(mutex->initialized);
97 mutex->initialized = false;
feadec63
PB
98 DeleteCriticalSection(&mutex->lock);
99}
100
101void qemu_rec_mutex_lock(QemuRecMutex *mutex)
102{
c096358e 103 assert(mutex->initialized);
feadec63
PB
104 EnterCriticalSection(&mutex->lock);
105}
106
107int qemu_rec_mutex_trylock(QemuRecMutex *mutex)
108{
c096358e 109 assert(mutex->initialized);
feadec63
PB
110 return !TryEnterCriticalSection(&mutex->lock);
111}
112
113void qemu_rec_mutex_unlock(QemuRecMutex *mutex)
114{
c096358e 115 assert(mutex->initialized);
feadec63
PB
116 LeaveCriticalSection(&mutex->lock);
117}
118
9257d46d
PB
119void qemu_cond_init(QemuCond *cond)
120{
121 memset(cond, 0, sizeof(*cond));
12f8def0 122 InitializeConditionVariable(&cond->var);
c096358e 123 cond->initialized = true;
9257d46d
PB
124}
125
1a290aea
SW
126void qemu_cond_destroy(QemuCond *cond)
127{
c096358e
FZ
128 assert(cond->initialized);
129 cond->initialized = false;
12f8def0 130 InitializeConditionVariable(&cond->var);
1a290aea
SW
131}
132
9257d46d
PB
133void qemu_cond_signal(QemuCond *cond)
134{
c096358e 135 assert(cond->initialized);
12f8def0 136 WakeConditionVariable(&cond->var);
9257d46d
PB
137}
138
139void qemu_cond_broadcast(QemuCond *cond)
140{
c096358e 141 assert(cond->initialized);
12f8def0 142 WakeAllConditionVariable(&cond->var);
9257d46d
PB
143}
144
6c27a0de 145void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
9257d46d 146{
c096358e 147 assert(cond->initialized);
6c27a0de 148 trace_qemu_mutex_unlock(mutex, file, line);
12f8def0 149 SleepConditionVariableSRW(&cond->var, &mutex->lock, INFINITE, 0);
6c27a0de 150 trace_qemu_mutex_locked(mutex, file, line);
9257d46d
PB
151}
152
38b14db3
PB
153void qemu_sem_init(QemuSemaphore *sem, int init)
154{
155 /* Manual reset. */
156 sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL);
c096358e 157 sem->initialized = true;
38b14db3
PB
158}
159
160void qemu_sem_destroy(QemuSemaphore *sem)
161{
c096358e
FZ
162 assert(sem->initialized);
163 sem->initialized = false;
38b14db3
PB
164 CloseHandle(sem->sema);
165}
166
167void qemu_sem_post(QemuSemaphore *sem)
168{
c096358e 169 assert(sem->initialized);
38b14db3
PB
170 ReleaseSemaphore(sem->sema, 1, NULL);
171}
172
173int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
174{
c096358e
FZ
175 int rc;
176
177 assert(sem->initialized);
178 rc = WaitForSingleObject(sem->sema, ms);
38b14db3
PB
179 if (rc == WAIT_OBJECT_0) {
180 return 0;
181 }
182 if (rc != WAIT_TIMEOUT) {
183 error_exit(GetLastError(), __func__);
184 }
185 return -1;
186}
187
188void qemu_sem_wait(QemuSemaphore *sem)
189{
c096358e 190 assert(sem->initialized);
38b14db3
PB
191 if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) {
192 error_exit(GetLastError(), __func__);
193 }
194}
195
7c9b2bf6
PB
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!
200 *
201 * Valid transitions:
202 * - free->set, when setting the event
fbcc3e50 203 * - busy->set, when setting the event, followed by SetEvent
7c9b2bf6
PB
204 * - set->free, when resetting the event
205 * - free->busy, when waiting
206 *
207 * set->busy does not happen (it can be observed from the outside but
208 * it really is set->free->busy).
209 *
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).
213 */
214
215#define EV_SET 0
216#define EV_FREE 1
217#define EV_BUSY -1
218
c7c4d063
PB
219void qemu_event_init(QemuEvent *ev, bool init)
220{
221 /* Manual reset. */
7c9b2bf6
PB
222 ev->event = CreateEvent(NULL, TRUE, TRUE, NULL);
223 ev->value = (init ? EV_SET : EV_FREE);
c096358e 224 ev->initialized = true;
c7c4d063
PB
225}
226
227void qemu_event_destroy(QemuEvent *ev)
228{
c096358e
FZ
229 assert(ev->initialized);
230 ev->initialized = false;
c7c4d063
PB
231 CloseHandle(ev->event);
232}
233
234void qemu_event_set(QemuEvent *ev)
235{
c096358e 236 assert(ev->initialized);
374293ca
PB
237 /* qemu_event_set has release semantics, but because it *loads*
238 * ev->value we need a full memory barrier here.
239 */
240 smp_mb();
241 if (atomic_read(&ev->value) != EV_SET) {
7c9b2bf6
PB
242 if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
243 /* There were waiters, wake them up. */
244 SetEvent(ev->event);
245 }
246 }
c7c4d063
PB
247}
248
249void qemu_event_reset(QemuEvent *ev)
250{
374293ca
PB
251 unsigned value;
252
c096358e 253 assert(ev->initialized);
374293ca
PB
254 value = atomic_read(&ev->value);
255 smp_mb_acquire();
256 if (value == EV_SET) {
7c9b2bf6
PB
257 /* If there was a concurrent reset (or even reset+wait),
258 * do nothing. Otherwise change EV_SET->EV_FREE.
259 */
260 atomic_or(&ev->value, EV_FREE);
261 }
c7c4d063
PB
262}
263
264void qemu_event_wait(QemuEvent *ev)
265{
7c9b2bf6
PB
266 unsigned value;
267
c096358e 268 assert(ev->initialized);
374293ca
PB
269 value = atomic_read(&ev->value);
270 smp_mb_acquire();
7c9b2bf6
PB
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.
276 */
277 ResetEvent(ev->event);
278
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.
282 */
283 if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
284 value = EV_SET;
285 } else {
286 value = EV_BUSY;
287 }
288 }
289 if (value == EV_BUSY) {
290 WaitForSingleObject(ev->event, INFINITE);
291 }
292 }
c7c4d063
PB
293}
294
9257d46d 295struct QemuThreadData {
403e6331
PB
296 /* Passed to win32_start_routine. */
297 void *(*start_routine)(void *);
298 void *arg;
299 short mode;
ef57137f 300 NotifierList exit;
403e6331
PB
301
302 /* Only used for joinable threads. */
303 bool exited;
304 void *ret;
305 CRITICAL_SECTION cs;
9257d46d
PB
306};
307
ef57137f
PB
308static bool atexit_registered;
309static NotifierList main_thread_exit;
310
6265e4ff 311static __thread QemuThreadData *qemu_thread_data;
9257d46d 312
ef57137f
PB
313static void run_main_thread_exit(void)
314{
315 notifier_list_notify(&main_thread_exit, NULL);
316}
317
318void qemu_thread_atexit_add(Notifier *notifier)
319{
320 if (!qemu_thread_data) {
321 if (!atexit_registered) {
322 atexit_registered = true;
323 atexit(run_main_thread_exit);
324 }
325 notifier_list_add(&main_thread_exit, notifier);
326 } else {
327 notifier_list_add(&qemu_thread_data->exit, notifier);
328 }
329}
330
331void qemu_thread_atexit_remove(Notifier *notifier)
332{
333 notifier_remove(notifier);
334}
335
9257d46d
PB
336static unsigned __stdcall win32_start_routine(void *arg)
337{
403e6331
PB
338 QemuThreadData *data = (QemuThreadData *) arg;
339 void *(*start_routine)(void *) = data->start_routine;
340 void *thread_arg = data->arg;
341
6265e4ff 342 qemu_thread_data = data;
403e6331 343 qemu_thread_exit(start_routine(thread_arg));
9257d46d
PB
344 abort();
345}
346
347void qemu_thread_exit(void *arg)
348{
6265e4ff
JK
349 QemuThreadData *data = qemu_thread_data;
350
ef57137f
PB
351 notifier_list_notify(&data->exit, NULL);
352 if (data->mode == QEMU_THREAD_JOINABLE) {
403e6331
PB
353 data->ret = arg;
354 EnterCriticalSection(&data->cs);
355 data->exited = true;
356 LeaveCriticalSection(&data->cs);
ef57137f
PB
357 } else {
358 g_free(data);
403e6331
PB
359 }
360 _endthreadex(0);
361}
362
363void *qemu_thread_join(QemuThread *thread)
364{
365 QemuThreadData *data;
366 void *ret;
367 HANDLE handle;
368
369 data = thread->data;
ef57137f 370 if (data->mode == QEMU_THREAD_DETACHED) {
403e6331
PB
371 return NULL;
372 }
ef57137f 373
403e6331
PB
374 /*
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.
380 */
1ecf47bf
PB
381 handle = qemu_thread_get_handle(thread);
382 if (handle) {
403e6331
PB
383 WaitForSingleObject(handle, INFINITE);
384 CloseHandle(handle);
403e6331
PB
385 }
386 ret = data->ret;
387 DeleteCriticalSection(&data->cs);
388 g_free(data);
389 return ret;
9257d46d
PB
390}
391
4900116e 392void qemu_thread_create(QemuThread *thread, const char *name,
9257d46d 393 void *(*start_routine)(void *),
cf218714 394 void *arg, int mode)
9257d46d
PB
395{
396 HANDLE hThread;
9257d46d 397 struct QemuThreadData *data;
6265e4ff 398
7267c094 399 data = g_malloc(sizeof *data);
9257d46d
PB
400 data->start_routine = start_routine;
401 data->arg = arg;
403e6331
PB
402 data->mode = mode;
403 data->exited = false;
ef57137f 404 notifier_list_init(&data->exit);
9257d46d 405
edc1de97
SW
406 if (data->mode != QEMU_THREAD_DETACHED) {
407 InitializeCriticalSection(&data->cs);
408 }
409
9257d46d 410 hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
403e6331 411 data, 0, &thread->tid);
9257d46d
PB
412 if (!hThread) {
413 error_exit(GetLastError(), __func__);
414 }
415 CloseHandle(hThread);
ef57137f 416 thread->data = data;
9257d46d
PB
417}
418
419void qemu_thread_get_self(QemuThread *thread)
420{
6265e4ff 421 thread->data = qemu_thread_data;
403e6331 422 thread->tid = GetCurrentThreadId();
9257d46d
PB
423}
424
1ecf47bf
PB
425HANDLE qemu_thread_get_handle(QemuThread *thread)
426{
427 QemuThreadData *data;
428 HANDLE handle;
429
430 data = thread->data;
ef57137f 431 if (data->mode == QEMU_THREAD_DETACHED) {
1ecf47bf
PB
432 return NULL;
433 }
434
435 EnterCriticalSection(&data->cs);
436 if (!data->exited) {
b0cb0a66
VP
437 handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME |
438 THREAD_SET_CONTEXT, FALSE, thread->tid);
1ecf47bf
PB
439 } else {
440 handle = NULL;
441 }
442 LeaveCriticalSection(&data->cs);
443 return handle;
444}
445
2d797b65 446bool qemu_thread_is_self(QemuThread *thread)
9257d46d 447{
403e6331 448 return GetCurrentThreadId() == thread->tid;
9257d46d 449}
This page took 0.52491 seconds and 4 git commands to generate.