]> git.proxmox.com Git - mirror_qemu.git/blame - util/qemu-thread-win32.c
Remove qemu-common.h include from most units
[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 */
12f8def0 13
aafd7584 14#include "qemu/osdep.h"
1de7afc9 15#include "qemu/thread.h"
ef57137f 16#include "qemu/notify.h"
f1aff7aa 17#include "qemu-thread-common.h"
9257d46d 18#include <process.h>
9257d46d 19
8f480de0
DDAG
20static bool name_threads;
21
22void qemu_thread_naming(bool enable)
23{
24 /* But note we don't actually name them on Windows yet */
25 name_threads = enable;
5c312079
DDAG
26
27 fprintf(stderr, "qemu: thread naming not supported on this host\n");
8f480de0
DDAG
28}
29
9257d46d
PB
30static void error_exit(int err, const char *msg)
31{
32 char *pstr;
33
34 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
35 NULL, err, 0, (LPTSTR)&pstr, 2, NULL);
36 fprintf(stderr, "qemu: %s: %s\n", msg, pstr);
37 LocalFree(pstr);
53380ac3 38 abort();
9257d46d
PB
39}
40
41void qemu_mutex_init(QemuMutex *mutex)
42{
12f8def0 43 InitializeSRWLock(&mutex->lock);
f1aff7aa 44 qemu_mutex_post_init(mutex);
9257d46d
PB
45}
46
1a290aea
SW
47void qemu_mutex_destroy(QemuMutex *mutex)
48{
c096358e
FZ
49 assert(mutex->initialized);
50 mutex->initialized = false;
12f8def0 51 InitializeSRWLock(&mutex->lock);
1a290aea
SW
52}
53
6c27a0de 54void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
9257d46d 55{
c096358e 56 assert(mutex->initialized);
f1aff7aa 57 qemu_mutex_pre_lock(mutex, file, line);
12f8def0 58 AcquireSRWLockExclusive(&mutex->lock);
f1aff7aa 59 qemu_mutex_post_lock(mutex, file, line);
9257d46d
PB
60}
61
6c27a0de 62int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
9257d46d
PB
63{
64 int owned;
65
c096358e 66 assert(mutex->initialized);
12f8def0 67 owned = TryAcquireSRWLockExclusive(&mutex->lock);
31f5a726 68 if (owned) {
f1aff7aa 69 qemu_mutex_post_lock(mutex, file, line);
31f5a726
JRZ
70 return 0;
71 }
72 return -EBUSY;
9257d46d
PB
73}
74
6c27a0de 75void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
9257d46d 76{
c096358e 77 assert(mutex->initialized);
f1aff7aa 78 qemu_mutex_pre_unlock(mutex, file, line);
12f8def0 79 ReleaseSRWLockExclusive(&mutex->lock);
9257d46d
PB
80}
81
feadec63
PB
82void qemu_rec_mutex_init(QemuRecMutex *mutex)
83{
84 InitializeCriticalSection(&mutex->lock);
c096358e 85 mutex->initialized = true;
feadec63
PB
86}
87
88void qemu_rec_mutex_destroy(QemuRecMutex *mutex)
89{
c096358e
FZ
90 assert(mutex->initialized);
91 mutex->initialized = false;
feadec63
PB
92 DeleteCriticalSection(&mutex->lock);
93}
94
fe9959a2 95void qemu_rec_mutex_lock_impl(QemuRecMutex *mutex, const char *file, int line)
feadec63 96{
c096358e 97 assert(mutex->initialized);
feadec63
PB
98 EnterCriticalSection(&mutex->lock);
99}
100
fe9959a2 101int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line)
feadec63 102{
c096358e 103 assert(mutex->initialized);
feadec63
PB
104 return !TryEnterCriticalSection(&mutex->lock);
105}
106
9c75bae7 107void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, const char *file, int line)
feadec63 108{
c096358e 109 assert(mutex->initialized);
feadec63
PB
110 LeaveCriticalSection(&mutex->lock);
111}
112
9257d46d
PB
113void qemu_cond_init(QemuCond *cond)
114{
115 memset(cond, 0, sizeof(*cond));
12f8def0 116 InitializeConditionVariable(&cond->var);
c096358e 117 cond->initialized = true;
9257d46d
PB
118}
119
1a290aea
SW
120void qemu_cond_destroy(QemuCond *cond)
121{
c096358e
FZ
122 assert(cond->initialized);
123 cond->initialized = false;
12f8def0 124 InitializeConditionVariable(&cond->var);
1a290aea
SW
125}
126
9257d46d
PB
127void qemu_cond_signal(QemuCond *cond)
128{
c096358e 129 assert(cond->initialized);
12f8def0 130 WakeConditionVariable(&cond->var);
9257d46d
PB
131}
132
133void qemu_cond_broadcast(QemuCond *cond)
134{
c096358e 135 assert(cond->initialized);
12f8def0 136 WakeAllConditionVariable(&cond->var);
9257d46d
PB
137}
138
6c27a0de 139void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
9257d46d 140{
c096358e 141 assert(cond->initialized);
f1aff7aa 142 qemu_mutex_pre_unlock(mutex, file, line);
12f8def0 143 SleepConditionVariableSRW(&cond->var, &mutex->lock, INFINITE, 0);
f1aff7aa 144 qemu_mutex_post_lock(mutex, file, line);
9257d46d
PB
145}
146
3dcc9c6e
YK
147bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms,
148 const char *file, const int line)
149{
150 int rc = 0;
151
152 assert(cond->initialized);
153 trace_qemu_mutex_unlock(mutex, file, line);
154 if (!SleepConditionVariableSRW(&cond->var, &mutex->lock, ms, 0)) {
155 rc = GetLastError();
156 }
157 trace_qemu_mutex_locked(mutex, file, line);
158 if (rc && rc != ERROR_TIMEOUT) {
159 error_exit(rc, __func__);
160 }
161 return rc != ERROR_TIMEOUT;
162}
163
38b14db3
PB
164void qemu_sem_init(QemuSemaphore *sem, int init)
165{
166 /* Manual reset. */
167 sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL);
c096358e 168 sem->initialized = true;
38b14db3
PB
169}
170
171void qemu_sem_destroy(QemuSemaphore *sem)
172{
c096358e
FZ
173 assert(sem->initialized);
174 sem->initialized = false;
38b14db3
PB
175 CloseHandle(sem->sema);
176}
177
178void qemu_sem_post(QemuSemaphore *sem)
179{
c096358e 180 assert(sem->initialized);
38b14db3
PB
181 ReleaseSemaphore(sem->sema, 1, NULL);
182}
183
184int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
185{
c096358e
FZ
186 int rc;
187
188 assert(sem->initialized);
189 rc = WaitForSingleObject(sem->sema, ms);
38b14db3
PB
190 if (rc == WAIT_OBJECT_0) {
191 return 0;
192 }
193 if (rc != WAIT_TIMEOUT) {
194 error_exit(GetLastError(), __func__);
195 }
196 return -1;
197}
198
199void qemu_sem_wait(QemuSemaphore *sem)
200{
c096358e 201 assert(sem->initialized);
38b14db3
PB
202 if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) {
203 error_exit(GetLastError(), __func__);
204 }
205}
206
7c9b2bf6
PB
207/* Wrap a Win32 manual-reset event with a fast userspace path. The idea
208 * is to reset the Win32 event lazily, as part of a test-reset-test-wait
209 * sequence. Such a sequence is, indeed, how QemuEvents are used by
210 * RCU and other subsystems!
211 *
212 * Valid transitions:
213 * - free->set, when setting the event
fbcc3e50 214 * - busy->set, when setting the event, followed by SetEvent
7c9b2bf6
PB
215 * - set->free, when resetting the event
216 * - free->busy, when waiting
217 *
218 * set->busy does not happen (it can be observed from the outside but
219 * it really is set->free->busy).
220 *
221 * busy->free provably cannot happen; to enforce it, the set->free transition
222 * is done with an OR, which becomes a no-op if the event has concurrently
223 * transitioned to free or busy (and is faster than cmpxchg).
224 */
225
226#define EV_SET 0
227#define EV_FREE 1
228#define EV_BUSY -1
229
c7c4d063
PB
230void qemu_event_init(QemuEvent *ev, bool init)
231{
232 /* Manual reset. */
7c9b2bf6
PB
233 ev->event = CreateEvent(NULL, TRUE, TRUE, NULL);
234 ev->value = (init ? EV_SET : EV_FREE);
c096358e 235 ev->initialized = true;
c7c4d063
PB
236}
237
238void qemu_event_destroy(QemuEvent *ev)
239{
c096358e
FZ
240 assert(ev->initialized);
241 ev->initialized = false;
c7c4d063
PB
242 CloseHandle(ev->event);
243}
244
245void qemu_event_set(QemuEvent *ev)
246{
c096358e 247 assert(ev->initialized);
374293ca
PB
248 /* qemu_event_set has release semantics, but because it *loads*
249 * ev->value we need a full memory barrier here.
250 */
251 smp_mb();
d73415a3
SH
252 if (qatomic_read(&ev->value) != EV_SET) {
253 if (qatomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
7c9b2bf6
PB
254 /* There were waiters, wake them up. */
255 SetEvent(ev->event);
256 }
257 }
c7c4d063
PB
258}
259
260void qemu_event_reset(QemuEvent *ev)
261{
374293ca
PB
262 unsigned value;
263
c096358e 264 assert(ev->initialized);
d73415a3 265 value = qatomic_read(&ev->value);
374293ca
PB
266 smp_mb_acquire();
267 if (value == EV_SET) {
7c9b2bf6
PB
268 /* If there was a concurrent reset (or even reset+wait),
269 * do nothing. Otherwise change EV_SET->EV_FREE.
270 */
d73415a3 271 qatomic_or(&ev->value, EV_FREE);
7c9b2bf6 272 }
c7c4d063
PB
273}
274
275void qemu_event_wait(QemuEvent *ev)
276{
7c9b2bf6
PB
277 unsigned value;
278
c096358e 279 assert(ev->initialized);
d73415a3 280 value = qatomic_read(&ev->value);
374293ca 281 smp_mb_acquire();
7c9b2bf6
PB
282 if (value != EV_SET) {
283 if (value == EV_FREE) {
284 /* qemu_event_set is not yet going to call SetEvent, but we are
285 * going to do another check for EV_SET below when setting EV_BUSY.
286 * At that point it is safe to call WaitForSingleObject.
287 */
288 ResetEvent(ev->event);
289
290 /* Tell qemu_event_set that there are waiters. No need to retry
8cc360b9 291 * because there cannot be a concurrent busy->free transition.
7c9b2bf6
PB
292 * After the CAS, the event will be either set or busy.
293 */
d73415a3 294 if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
7c9b2bf6
PB
295 value = EV_SET;
296 } else {
297 value = EV_BUSY;
298 }
299 }
300 if (value == EV_BUSY) {
301 WaitForSingleObject(ev->event, INFINITE);
302 }
303 }
c7c4d063
PB
304}
305
9257d46d 306struct QemuThreadData {
403e6331
PB
307 /* Passed to win32_start_routine. */
308 void *(*start_routine)(void *);
309 void *arg;
310 short mode;
ef57137f 311 NotifierList exit;
403e6331
PB
312
313 /* Only used for joinable threads. */
314 bool exited;
315 void *ret;
316 CRITICAL_SECTION cs;
9257d46d
PB
317};
318
ef57137f
PB
319static bool atexit_registered;
320static NotifierList main_thread_exit;
321
6265e4ff 322static __thread QemuThreadData *qemu_thread_data;
9257d46d 323
ef57137f
PB
324static void run_main_thread_exit(void)
325{
326 notifier_list_notify(&main_thread_exit, NULL);
327}
328
329void qemu_thread_atexit_add(Notifier *notifier)
330{
331 if (!qemu_thread_data) {
332 if (!atexit_registered) {
333 atexit_registered = true;
334 atexit(run_main_thread_exit);
335 }
336 notifier_list_add(&main_thread_exit, notifier);
337 } else {
338 notifier_list_add(&qemu_thread_data->exit, notifier);
339 }
340}
341
342void qemu_thread_atexit_remove(Notifier *notifier)
343{
344 notifier_remove(notifier);
345}
346
9257d46d
PB
347static unsigned __stdcall win32_start_routine(void *arg)
348{
403e6331
PB
349 QemuThreadData *data = (QemuThreadData *) arg;
350 void *(*start_routine)(void *) = data->start_routine;
351 void *thread_arg = data->arg;
352
6265e4ff 353 qemu_thread_data = data;
403e6331 354 qemu_thread_exit(start_routine(thread_arg));
9257d46d
PB
355 abort();
356}
357
358void qemu_thread_exit(void *arg)
359{
6265e4ff
JK
360 QemuThreadData *data = qemu_thread_data;
361
ef57137f
PB
362 notifier_list_notify(&data->exit, NULL);
363 if (data->mode == QEMU_THREAD_JOINABLE) {
403e6331
PB
364 data->ret = arg;
365 EnterCriticalSection(&data->cs);
366 data->exited = true;
367 LeaveCriticalSection(&data->cs);
ef57137f
PB
368 } else {
369 g_free(data);
403e6331
PB
370 }
371 _endthreadex(0);
372}
373
374void *qemu_thread_join(QemuThread *thread)
375{
376 QemuThreadData *data;
377 void *ret;
378 HANDLE handle;
379
380 data = thread->data;
ef57137f 381 if (data->mode == QEMU_THREAD_DETACHED) {
403e6331
PB
382 return NULL;
383 }
ef57137f 384
403e6331
PB
385 /*
386 * Because multiple copies of the QemuThread can exist via
387 * qemu_thread_get_self, we need to store a value that cannot
388 * leak there. The simplest, non racy way is to store the TID,
389 * discard the handle that _beginthreadex gives back, and
390 * get another copy of the handle here.
391 */
1ecf47bf
PB
392 handle = qemu_thread_get_handle(thread);
393 if (handle) {
403e6331
PB
394 WaitForSingleObject(handle, INFINITE);
395 CloseHandle(handle);
403e6331
PB
396 }
397 ret = data->ret;
398 DeleteCriticalSection(&data->cs);
399 g_free(data);
400 return ret;
9257d46d
PB
401}
402
4900116e 403void qemu_thread_create(QemuThread *thread, const char *name,
9257d46d 404 void *(*start_routine)(void *),
cf218714 405 void *arg, int mode)
9257d46d
PB
406{
407 HANDLE hThread;
9257d46d 408 struct QemuThreadData *data;
6265e4ff 409
7267c094 410 data = g_malloc(sizeof *data);
9257d46d
PB
411 data->start_routine = start_routine;
412 data->arg = arg;
403e6331
PB
413 data->mode = mode;
414 data->exited = false;
ef57137f 415 notifier_list_init(&data->exit);
9257d46d 416
edc1de97
SW
417 if (data->mode != QEMU_THREAD_DETACHED) {
418 InitializeCriticalSection(&data->cs);
419 }
420
9257d46d 421 hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
403e6331 422 data, 0, &thread->tid);
9257d46d
PB
423 if (!hThread) {
424 error_exit(GetLastError(), __func__);
425 }
426 CloseHandle(hThread);
ef57137f 427 thread->data = data;
9257d46d
PB
428}
429
430void qemu_thread_get_self(QemuThread *thread)
431{
6265e4ff 432 thread->data = qemu_thread_data;
403e6331 433 thread->tid = GetCurrentThreadId();
9257d46d
PB
434}
435
1ecf47bf
PB
436HANDLE qemu_thread_get_handle(QemuThread *thread)
437{
438 QemuThreadData *data;
439 HANDLE handle;
440
441 data = thread->data;
ef57137f 442 if (data->mode == QEMU_THREAD_DETACHED) {
1ecf47bf
PB
443 return NULL;
444 }
445
446 EnterCriticalSection(&data->cs);
447 if (!data->exited) {
b0cb0a66
VP
448 handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME |
449 THREAD_SET_CONTEXT, FALSE, thread->tid);
1ecf47bf
PB
450 } else {
451 handle = NULL;
452 }
453 LeaveCriticalSection(&data->cs);
454 return handle;
455}
456
2d797b65 457bool qemu_thread_is_self(QemuThread *thread)
9257d46d 458{
403e6331 459 return GetCurrentThreadId() == thread->tid;
9257d46d 460}