]> git.proxmox.com Git - mirror_qemu.git/blame - util/qemu-thread-win32.c
iothread: release AioContext around aio_poll
[mirror_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 <pbonzini@redhat.com>
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 */
aafd7584 13#include "qemu/osdep.h"
9257d46d 14#include "qemu-common.h"
1de7afc9 15#include "qemu/thread.h"
ef57137f 16#include "qemu/notify.h"
9257d46d 17#include <process.h>
9257d46d 18
8f480de0
DDAG
19static bool name_threads;
20
21void qemu_thread_naming(bool enable)
22{
23 /* But note we don't actually name them on Windows yet */
24 name_threads = enable;
5c312079
DDAG
25
26 fprintf(stderr, "qemu: thread naming not supported on this host\n");
8f480de0
DDAG
27}
28
9257d46d
PB
29static void error_exit(int err, const char *msg)
30{
31 char *pstr;
32
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);
53380ac3 37 abort();
9257d46d
PB
38}
39
40void qemu_mutex_init(QemuMutex *mutex)
41{
42 mutex->owner = 0;
43 InitializeCriticalSection(&mutex->lock);
44}
45
1a290aea
SW
46void qemu_mutex_destroy(QemuMutex *mutex)
47{
48 assert(mutex->owner == 0);
49 DeleteCriticalSection(&mutex->lock);
50}
51
9257d46d
PB
52void qemu_mutex_lock(QemuMutex *mutex)
53{
54 EnterCriticalSection(&mutex->lock);
55
56 /* Win32 CRITICAL_SECTIONs are recursive. Assert that we're not
57 * using them as such.
58 */
59 assert(mutex->owner == 0);
60 mutex->owner = GetCurrentThreadId();
61}
62
63int qemu_mutex_trylock(QemuMutex *mutex)
64{
65 int owned;
66
67 owned = TryEnterCriticalSection(&mutex->lock);
68 if (owned) {
69 assert(mutex->owner == 0);
70 mutex->owner = GetCurrentThreadId();
71 }
72 return !owned;
73}
74
75void qemu_mutex_unlock(QemuMutex *mutex)
76{
77 assert(mutex->owner == GetCurrentThreadId());
78 mutex->owner = 0;
79 LeaveCriticalSection(&mutex->lock);
80}
81
82void qemu_cond_init(QemuCond *cond)
83{
84 memset(cond, 0, sizeof(*cond));
85
86 cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
87 if (!cond->sema) {
88 error_exit(GetLastError(), __func__);
89 }
90 cond->continue_event = CreateEvent(NULL, /* security */
91 FALSE, /* auto-reset */
92 FALSE, /* not signaled */
93 NULL); /* name */
94 if (!cond->continue_event) {
95 error_exit(GetLastError(), __func__);
96 }
97}
98
1a290aea
SW
99void qemu_cond_destroy(QemuCond *cond)
100{
101 BOOL result;
102 result = CloseHandle(cond->continue_event);
103 if (!result) {
104 error_exit(GetLastError(), __func__);
105 }
106 cond->continue_event = 0;
107 result = CloseHandle(cond->sema);
108 if (!result) {
109 error_exit(GetLastError(), __func__);
110 }
111 cond->sema = 0;
112}
113
9257d46d
PB
114void qemu_cond_signal(QemuCond *cond)
115{
116 DWORD result;
117
118 /*
119 * Signal only when there are waiters. cond->waiters is
120 * incremented by pthread_cond_wait under the external lock,
121 * so we are safe about that.
122 */
123 if (cond->waiters == 0) {
124 return;
125 }
126
127 /*
128 * Waiting threads decrement it outside the external lock, but
129 * only if another thread is executing pthread_cond_broadcast and
130 * has the mutex. So, it also cannot be decremented concurrently
131 * with this particular access.
132 */
133 cond->target = cond->waiters - 1;
134 result = SignalObjectAndWait(cond->sema, cond->continue_event,
135 INFINITE, FALSE);
136 if (result == WAIT_ABANDONED || result == WAIT_FAILED) {
137 error_exit(GetLastError(), __func__);
138 }
139}
140
141void qemu_cond_broadcast(QemuCond *cond)
142{
143 BOOLEAN result;
144 /*
145 * As in pthread_cond_signal, access to cond->waiters and
146 * cond->target is locked via the external mutex.
147 */
148 if (cond->waiters == 0) {
149 return;
150 }
151
152 cond->target = 0;
153 result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
154 if (!result) {
155 error_exit(GetLastError(), __func__);
156 }
157
158 /*
159 * At this point all waiters continue. Each one takes its
160 * slice of the semaphore. Now it's our turn to wait: Since
161 * the external mutex is held, no thread can leave cond_wait,
162 * yet. For this reason, we can be sure that no thread gets
163 * a chance to eat *more* than one slice. OTOH, it means
164 * that the last waiter must send us a wake-up.
165 */
166 WaitForSingleObject(cond->continue_event, INFINITE);
167}
168
169void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
170{
171 /*
172 * This access is protected under the mutex.
173 */
174 cond->waiters++;
175
176 /*
177 * Unlock external mutex and wait for signal.
178 * NOTE: we've held mutex locked long enough to increment
179 * waiters count above, so there's no problem with
180 * leaving mutex unlocked before we wait on semaphore.
181 */
182 qemu_mutex_unlock(mutex);
183 WaitForSingleObject(cond->sema, INFINITE);
184
185 /* Now waiters must rendez-vous with the signaling thread and
186 * let it continue. For cond_broadcast this has heavy contention
187 * and triggers thundering herd. So goes life.
188 *
189 * Decrease waiters count. The mutex is not taken, so we have
190 * to do this atomically.
191 *
192 * All waiters contend for the mutex at the end of this function
193 * until the signaling thread relinquishes it. To ensure
194 * each waiter consumes exactly one slice of the semaphore,
195 * the signaling thread stops until it is told by the last
196 * waiter that it can go on.
197 */
198 if (InterlockedDecrement(&cond->waiters) == cond->target) {
199 SetEvent(cond->continue_event);
200 }
201
202 qemu_mutex_lock(mutex);
203}
204
38b14db3
PB
205void qemu_sem_init(QemuSemaphore *sem, int init)
206{
207 /* Manual reset. */
208 sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL);
209}
210
211void qemu_sem_destroy(QemuSemaphore *sem)
212{
213 CloseHandle(sem->sema);
214}
215
216void qemu_sem_post(QemuSemaphore *sem)
217{
218 ReleaseSemaphore(sem->sema, 1, NULL);
219}
220
221int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
222{
223 int rc = WaitForSingleObject(sem->sema, ms);
224 if (rc == WAIT_OBJECT_0) {
225 return 0;
226 }
227 if (rc != WAIT_TIMEOUT) {
228 error_exit(GetLastError(), __func__);
229 }
230 return -1;
231}
232
233void qemu_sem_wait(QemuSemaphore *sem)
234{
235 if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) {
236 error_exit(GetLastError(), __func__);
237 }
238}
239
7c9b2bf6
PB
240/* Wrap a Win32 manual-reset event with a fast userspace path. The idea
241 * is to reset the Win32 event lazily, as part of a test-reset-test-wait
242 * sequence. Such a sequence is, indeed, how QemuEvents are used by
243 * RCU and other subsystems!
244 *
245 * Valid transitions:
246 * - free->set, when setting the event
247 * - busy->set, when setting the event, followed by futex_wake
248 * - set->free, when resetting the event
249 * - free->busy, when waiting
250 *
251 * set->busy does not happen (it can be observed from the outside but
252 * it really is set->free->busy).
253 *
254 * busy->free provably cannot happen; to enforce it, the set->free transition
255 * is done with an OR, which becomes a no-op if the event has concurrently
256 * transitioned to free or busy (and is faster than cmpxchg).
257 */
258
259#define EV_SET 0
260#define EV_FREE 1
261#define EV_BUSY -1
262
c7c4d063
PB
263void qemu_event_init(QemuEvent *ev, bool init)
264{
265 /* Manual reset. */
7c9b2bf6
PB
266 ev->event = CreateEvent(NULL, TRUE, TRUE, NULL);
267 ev->value = (init ? EV_SET : EV_FREE);
c7c4d063
PB
268}
269
270void qemu_event_destroy(QemuEvent *ev)
271{
272 CloseHandle(ev->event);
273}
274
275void qemu_event_set(QemuEvent *ev)
276{
374293ca
PB
277 /* qemu_event_set has release semantics, but because it *loads*
278 * ev->value we need a full memory barrier here.
279 */
280 smp_mb();
281 if (atomic_read(&ev->value) != EV_SET) {
7c9b2bf6
PB
282 if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
283 /* There were waiters, wake them up. */
284 SetEvent(ev->event);
285 }
286 }
c7c4d063
PB
287}
288
289void qemu_event_reset(QemuEvent *ev)
290{
374293ca
PB
291 unsigned value;
292
293 value = atomic_read(&ev->value);
294 smp_mb_acquire();
295 if (value == EV_SET) {
7c9b2bf6
PB
296 /* If there was a concurrent reset (or even reset+wait),
297 * do nothing. Otherwise change EV_SET->EV_FREE.
298 */
299 atomic_or(&ev->value, EV_FREE);
300 }
c7c4d063
PB
301}
302
303void qemu_event_wait(QemuEvent *ev)
304{
7c9b2bf6
PB
305 unsigned value;
306
374293ca
PB
307 value = atomic_read(&ev->value);
308 smp_mb_acquire();
7c9b2bf6
PB
309 if (value != EV_SET) {
310 if (value == EV_FREE) {
311 /* qemu_event_set is not yet going to call SetEvent, but we are
312 * going to do another check for EV_SET below when setting EV_BUSY.
313 * At that point it is safe to call WaitForSingleObject.
314 */
315 ResetEvent(ev->event);
316
317 /* Tell qemu_event_set that there are waiters. No need to retry
318 * because there cannot be a concurent busy->free transition.
319 * After the CAS, the event will be either set or busy.
320 */
321 if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
322 value = EV_SET;
323 } else {
324 value = EV_BUSY;
325 }
326 }
327 if (value == EV_BUSY) {
328 WaitForSingleObject(ev->event, INFINITE);
329 }
330 }
c7c4d063
PB
331}
332
9257d46d 333struct QemuThreadData {
403e6331
PB
334 /* Passed to win32_start_routine. */
335 void *(*start_routine)(void *);
336 void *arg;
337 short mode;
ef57137f 338 NotifierList exit;
403e6331
PB
339
340 /* Only used for joinable threads. */
341 bool exited;
342 void *ret;
343 CRITICAL_SECTION cs;
9257d46d
PB
344};
345
ef57137f
PB
346static bool atexit_registered;
347static NotifierList main_thread_exit;
348
6265e4ff 349static __thread QemuThreadData *qemu_thread_data;
9257d46d 350
ef57137f
PB
351static void run_main_thread_exit(void)
352{
353 notifier_list_notify(&main_thread_exit, NULL);
354}
355
356void qemu_thread_atexit_add(Notifier *notifier)
357{
358 if (!qemu_thread_data) {
359 if (!atexit_registered) {
360 atexit_registered = true;
361 atexit(run_main_thread_exit);
362 }
363 notifier_list_add(&main_thread_exit, notifier);
364 } else {
365 notifier_list_add(&qemu_thread_data->exit, notifier);
366 }
367}
368
369void qemu_thread_atexit_remove(Notifier *notifier)
370{
371 notifier_remove(notifier);
372}
373
9257d46d
PB
374static unsigned __stdcall win32_start_routine(void *arg)
375{
403e6331
PB
376 QemuThreadData *data = (QemuThreadData *) arg;
377 void *(*start_routine)(void *) = data->start_routine;
378 void *thread_arg = data->arg;
379
6265e4ff 380 qemu_thread_data = data;
403e6331 381 qemu_thread_exit(start_routine(thread_arg));
9257d46d
PB
382 abort();
383}
384
385void qemu_thread_exit(void *arg)
386{
6265e4ff
JK
387 QemuThreadData *data = qemu_thread_data;
388
ef57137f
PB
389 notifier_list_notify(&data->exit, NULL);
390 if (data->mode == QEMU_THREAD_JOINABLE) {
403e6331
PB
391 data->ret = arg;
392 EnterCriticalSection(&data->cs);
393 data->exited = true;
394 LeaveCriticalSection(&data->cs);
ef57137f
PB
395 } else {
396 g_free(data);
403e6331
PB
397 }
398 _endthreadex(0);
399}
400
401void *qemu_thread_join(QemuThread *thread)
402{
403 QemuThreadData *data;
404 void *ret;
405 HANDLE handle;
406
407 data = thread->data;
ef57137f 408 if (data->mode == QEMU_THREAD_DETACHED) {
403e6331
PB
409 return NULL;
410 }
ef57137f 411
403e6331
PB
412 /*
413 * Because multiple copies of the QemuThread can exist via
414 * qemu_thread_get_self, we need to store a value that cannot
415 * leak there. The simplest, non racy way is to store the TID,
416 * discard the handle that _beginthreadex gives back, and
417 * get another copy of the handle here.
418 */
1ecf47bf
PB
419 handle = qemu_thread_get_handle(thread);
420 if (handle) {
403e6331
PB
421 WaitForSingleObject(handle, INFINITE);
422 CloseHandle(handle);
403e6331
PB
423 }
424 ret = data->ret;
425 DeleteCriticalSection(&data->cs);
426 g_free(data);
427 return ret;
9257d46d
PB
428}
429
4900116e 430void qemu_thread_create(QemuThread *thread, const char *name,
9257d46d 431 void *(*start_routine)(void *),
cf218714 432 void *arg, int mode)
9257d46d
PB
433{
434 HANDLE hThread;
9257d46d 435 struct QemuThreadData *data;
6265e4ff 436
7267c094 437 data = g_malloc(sizeof *data);
9257d46d
PB
438 data->start_routine = start_routine;
439 data->arg = arg;
403e6331
PB
440 data->mode = mode;
441 data->exited = false;
ef57137f 442 notifier_list_init(&data->exit);
9257d46d 443
edc1de97
SW
444 if (data->mode != QEMU_THREAD_DETACHED) {
445 InitializeCriticalSection(&data->cs);
446 }
447
9257d46d 448 hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
403e6331 449 data, 0, &thread->tid);
9257d46d
PB
450 if (!hThread) {
451 error_exit(GetLastError(), __func__);
452 }
453 CloseHandle(hThread);
ef57137f 454 thread->data = data;
9257d46d
PB
455}
456
457void qemu_thread_get_self(QemuThread *thread)
458{
6265e4ff 459 thread->data = qemu_thread_data;
403e6331 460 thread->tid = GetCurrentThreadId();
9257d46d
PB
461}
462
1ecf47bf
PB
463HANDLE qemu_thread_get_handle(QemuThread *thread)
464{
465 QemuThreadData *data;
466 HANDLE handle;
467
468 data = thread->data;
ef57137f 469 if (data->mode == QEMU_THREAD_DETACHED) {
1ecf47bf
PB
470 return NULL;
471 }
472
473 EnterCriticalSection(&data->cs);
474 if (!data->exited) {
475 handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME, FALSE,
476 thread->tid);
477 } else {
478 handle = NULL;
479 }
480 LeaveCriticalSection(&data->cs);
481 return handle;
482}
483
2d797b65 484bool qemu_thread_is_self(QemuThread *thread)
9257d46d 485{
403e6331 486 return GetCurrentThreadId() == thread->tid;
9257d46d 487}