]>
Commit | Line | Data |
---|---|---|
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 | */ | |
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 |
19 | static bool name_threads; |
20 | ||
21 | void 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 |
29 | static 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 | ||
40 | void qemu_mutex_init(QemuMutex *mutex) | |
41 | { | |
42 | mutex->owner = 0; | |
43 | InitializeCriticalSection(&mutex->lock); | |
44 | } | |
45 | ||
1a290aea SW |
46 | void qemu_mutex_destroy(QemuMutex *mutex) |
47 | { | |
48 | assert(mutex->owner == 0); | |
49 | DeleteCriticalSection(&mutex->lock); | |
50 | } | |
51 | ||
9257d46d PB |
52 | void 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 | ||
63 | int 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 | ||
75 | void qemu_mutex_unlock(QemuMutex *mutex) | |
76 | { | |
77 | assert(mutex->owner == GetCurrentThreadId()); | |
78 | mutex->owner = 0; | |
79 | LeaveCriticalSection(&mutex->lock); | |
80 | } | |
81 | ||
feadec63 PB |
82 | void qemu_rec_mutex_init(QemuRecMutex *mutex) |
83 | { | |
84 | InitializeCriticalSection(&mutex->lock); | |
85 | } | |
86 | ||
87 | void qemu_rec_mutex_destroy(QemuRecMutex *mutex) | |
88 | { | |
89 | DeleteCriticalSection(&mutex->lock); | |
90 | } | |
91 | ||
92 | void qemu_rec_mutex_lock(QemuRecMutex *mutex) | |
93 | { | |
94 | EnterCriticalSection(&mutex->lock); | |
95 | } | |
96 | ||
97 | int qemu_rec_mutex_trylock(QemuRecMutex *mutex) | |
98 | { | |
99 | return !TryEnterCriticalSection(&mutex->lock); | |
100 | } | |
101 | ||
102 | void qemu_rec_mutex_unlock(QemuRecMutex *mutex) | |
103 | { | |
104 | LeaveCriticalSection(&mutex->lock); | |
105 | } | |
106 | ||
9257d46d PB |
107 | void qemu_cond_init(QemuCond *cond) |
108 | { | |
109 | memset(cond, 0, sizeof(*cond)); | |
110 | ||
111 | cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL); | |
112 | if (!cond->sema) { | |
113 | error_exit(GetLastError(), __func__); | |
114 | } | |
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__); | |
121 | } | |
122 | } | |
123 | ||
1a290aea SW |
124 | void qemu_cond_destroy(QemuCond *cond) |
125 | { | |
126 | BOOL result; | |
127 | result = CloseHandle(cond->continue_event); | |
128 | if (!result) { | |
129 | error_exit(GetLastError(), __func__); | |
130 | } | |
131 | cond->continue_event = 0; | |
132 | result = CloseHandle(cond->sema); | |
133 | if (!result) { | |
134 | error_exit(GetLastError(), __func__); | |
135 | } | |
136 | cond->sema = 0; | |
137 | } | |
138 | ||
9257d46d PB |
139 | void qemu_cond_signal(QemuCond *cond) |
140 | { | |
141 | DWORD result; | |
142 | ||
143 | /* | |
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. | |
147 | */ | |
148 | if (cond->waiters == 0) { | |
149 | return; | |
150 | } | |
151 | ||
152 | /* | |
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. | |
157 | */ | |
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__); | |
163 | } | |
164 | } | |
165 | ||
166 | void qemu_cond_broadcast(QemuCond *cond) | |
167 | { | |
168 | BOOLEAN result; | |
169 | /* | |
170 | * As in pthread_cond_signal, access to cond->waiters and | |
171 | * cond->target is locked via the external mutex. | |
172 | */ | |
173 | if (cond->waiters == 0) { | |
174 | return; | |
175 | } | |
176 | ||
177 | cond->target = 0; | |
178 | result = ReleaseSemaphore(cond->sema, cond->waiters, NULL); | |
179 | if (!result) { | |
180 | error_exit(GetLastError(), __func__); | |
181 | } | |
182 | ||
183 | /* | |
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. | |
190 | */ | |
191 | WaitForSingleObject(cond->continue_event, INFINITE); | |
192 | } | |
193 | ||
194 | void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) | |
195 | { | |
196 | /* | |
197 | * This access is protected under the mutex. | |
198 | */ | |
199 | cond->waiters++; | |
200 | ||
201 | /* | |
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. | |
206 | */ | |
207 | qemu_mutex_unlock(mutex); | |
208 | WaitForSingleObject(cond->sema, INFINITE); | |
209 | ||
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. | |
213 | * | |
214 | * Decrease waiters count. The mutex is not taken, so we have | |
215 | * to do this atomically. | |
216 | * | |
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. | |
222 | */ | |
223 | if (InterlockedDecrement(&cond->waiters) == cond->target) { | |
224 | SetEvent(cond->continue_event); | |
225 | } | |
226 | ||
227 | qemu_mutex_lock(mutex); | |
228 | } | |
229 | ||
38b14db3 PB |
230 | void qemu_sem_init(QemuSemaphore *sem, int init) |
231 | { | |
232 | /* Manual reset. */ | |
233 | sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL); | |
234 | } | |
235 | ||
236 | void qemu_sem_destroy(QemuSemaphore *sem) | |
237 | { | |
238 | CloseHandle(sem->sema); | |
239 | } | |
240 | ||
241 | void qemu_sem_post(QemuSemaphore *sem) | |
242 | { | |
243 | ReleaseSemaphore(sem->sema, 1, NULL); | |
244 | } | |
245 | ||
246 | int qemu_sem_timedwait(QemuSemaphore *sem, int ms) | |
247 | { | |
248 | int rc = WaitForSingleObject(sem->sema, ms); | |
249 | if (rc == WAIT_OBJECT_0) { | |
250 | return 0; | |
251 | } | |
252 | if (rc != WAIT_TIMEOUT) { | |
253 | error_exit(GetLastError(), __func__); | |
254 | } | |
255 | return -1; | |
256 | } | |
257 | ||
258 | void qemu_sem_wait(QemuSemaphore *sem) | |
259 | { | |
260 | if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) { | |
261 | error_exit(GetLastError(), __func__); | |
262 | } | |
263 | } | |
264 | ||
7c9b2bf6 PB |
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! | |
269 | * | |
270 | * Valid transitions: | |
271 | * - free->set, when setting the event | |
fbcc3e50 | 272 | * - busy->set, when setting the event, followed by SetEvent |
7c9b2bf6 PB |
273 | * - set->free, when resetting the event |
274 | * - free->busy, when waiting | |
275 | * | |
276 | * set->busy does not happen (it can be observed from the outside but | |
277 | * it really is set->free->busy). | |
278 | * | |
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). | |
282 | */ | |
283 | ||
284 | #define EV_SET 0 | |
285 | #define EV_FREE 1 | |
286 | #define EV_BUSY -1 | |
287 | ||
c7c4d063 PB |
288 | void qemu_event_init(QemuEvent *ev, bool init) |
289 | { | |
290 | /* Manual reset. */ | |
7c9b2bf6 PB |
291 | ev->event = CreateEvent(NULL, TRUE, TRUE, NULL); |
292 | ev->value = (init ? EV_SET : EV_FREE); | |
c7c4d063 PB |
293 | } |
294 | ||
295 | void qemu_event_destroy(QemuEvent *ev) | |
296 | { | |
297 | CloseHandle(ev->event); | |
298 | } | |
299 | ||
300 | void qemu_event_set(QemuEvent *ev) | |
301 | { | |
374293ca PB |
302 | /* qemu_event_set has release semantics, but because it *loads* |
303 | * ev->value we need a full memory barrier here. | |
304 | */ | |
305 | smp_mb(); | |
306 | if (atomic_read(&ev->value) != EV_SET) { | |
7c9b2bf6 PB |
307 | if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) { |
308 | /* There were waiters, wake them up. */ | |
309 | SetEvent(ev->event); | |
310 | } | |
311 | } | |
c7c4d063 PB |
312 | } |
313 | ||
314 | void qemu_event_reset(QemuEvent *ev) | |
315 | { | |
374293ca PB |
316 | unsigned value; |
317 | ||
318 | value = atomic_read(&ev->value); | |
319 | smp_mb_acquire(); | |
320 | if (value == EV_SET) { | |
7c9b2bf6 PB |
321 | /* If there was a concurrent reset (or even reset+wait), |
322 | * do nothing. Otherwise change EV_SET->EV_FREE. | |
323 | */ | |
324 | atomic_or(&ev->value, EV_FREE); | |
325 | } | |
c7c4d063 PB |
326 | } |
327 | ||
328 | void qemu_event_wait(QemuEvent *ev) | |
329 | { | |
7c9b2bf6 PB |
330 | unsigned value; |
331 | ||
374293ca PB |
332 | value = atomic_read(&ev->value); |
333 | smp_mb_acquire(); | |
7c9b2bf6 PB |
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. | |
339 | */ | |
340 | ResetEvent(ev->event); | |
341 | ||
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. | |
345 | */ | |
346 | if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) { | |
347 | value = EV_SET; | |
348 | } else { | |
349 | value = EV_BUSY; | |
350 | } | |
351 | } | |
352 | if (value == EV_BUSY) { | |
353 | WaitForSingleObject(ev->event, INFINITE); | |
354 | } | |
355 | } | |
c7c4d063 PB |
356 | } |
357 | ||
9257d46d | 358 | struct QemuThreadData { |
403e6331 PB |
359 | /* Passed to win32_start_routine. */ |
360 | void *(*start_routine)(void *); | |
361 | void *arg; | |
362 | short mode; | |
ef57137f | 363 | NotifierList exit; |
403e6331 PB |
364 | |
365 | /* Only used for joinable threads. */ | |
366 | bool exited; | |
367 | void *ret; | |
368 | CRITICAL_SECTION cs; | |
9257d46d PB |
369 | }; |
370 | ||
ef57137f PB |
371 | static bool atexit_registered; |
372 | static NotifierList main_thread_exit; | |
373 | ||
6265e4ff | 374 | static __thread QemuThreadData *qemu_thread_data; |
9257d46d | 375 | |
ef57137f PB |
376 | static void run_main_thread_exit(void) |
377 | { | |
378 | notifier_list_notify(&main_thread_exit, NULL); | |
379 | } | |
380 | ||
381 | void qemu_thread_atexit_add(Notifier *notifier) | |
382 | { | |
383 | if (!qemu_thread_data) { | |
384 | if (!atexit_registered) { | |
385 | atexit_registered = true; | |
386 | atexit(run_main_thread_exit); | |
387 | } | |
388 | notifier_list_add(&main_thread_exit, notifier); | |
389 | } else { | |
390 | notifier_list_add(&qemu_thread_data->exit, notifier); | |
391 | } | |
392 | } | |
393 | ||
394 | void qemu_thread_atexit_remove(Notifier *notifier) | |
395 | { | |
396 | notifier_remove(notifier); | |
397 | } | |
398 | ||
9257d46d PB |
399 | static unsigned __stdcall win32_start_routine(void *arg) |
400 | { | |
403e6331 PB |
401 | QemuThreadData *data = (QemuThreadData *) arg; |
402 | void *(*start_routine)(void *) = data->start_routine; | |
403 | void *thread_arg = data->arg; | |
404 | ||
6265e4ff | 405 | qemu_thread_data = data; |
403e6331 | 406 | qemu_thread_exit(start_routine(thread_arg)); |
9257d46d PB |
407 | abort(); |
408 | } | |
409 | ||
410 | void qemu_thread_exit(void *arg) | |
411 | { | |
6265e4ff JK |
412 | QemuThreadData *data = qemu_thread_data; |
413 | ||
ef57137f PB |
414 | notifier_list_notify(&data->exit, NULL); |
415 | if (data->mode == QEMU_THREAD_JOINABLE) { | |
403e6331 PB |
416 | data->ret = arg; |
417 | EnterCriticalSection(&data->cs); | |
418 | data->exited = true; | |
419 | LeaveCriticalSection(&data->cs); | |
ef57137f PB |
420 | } else { |
421 | g_free(data); | |
403e6331 PB |
422 | } |
423 | _endthreadex(0); | |
424 | } | |
425 | ||
426 | void *qemu_thread_join(QemuThread *thread) | |
427 | { | |
428 | QemuThreadData *data; | |
429 | void *ret; | |
430 | HANDLE handle; | |
431 | ||
432 | data = thread->data; | |
ef57137f | 433 | if (data->mode == QEMU_THREAD_DETACHED) { |
403e6331 PB |
434 | return NULL; |
435 | } | |
ef57137f | 436 | |
403e6331 PB |
437 | /* |
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. | |
443 | */ | |
1ecf47bf PB |
444 | handle = qemu_thread_get_handle(thread); |
445 | if (handle) { | |
403e6331 PB |
446 | WaitForSingleObject(handle, INFINITE); |
447 | CloseHandle(handle); | |
403e6331 PB |
448 | } |
449 | ret = data->ret; | |
450 | DeleteCriticalSection(&data->cs); | |
451 | g_free(data); | |
452 | return ret; | |
9257d46d PB |
453 | } |
454 | ||
4900116e | 455 | void qemu_thread_create(QemuThread *thread, const char *name, |
9257d46d | 456 | void *(*start_routine)(void *), |
cf218714 | 457 | void *arg, int mode) |
9257d46d PB |
458 | { |
459 | HANDLE hThread; | |
9257d46d | 460 | struct QemuThreadData *data; |
6265e4ff | 461 | |
7267c094 | 462 | data = g_malloc(sizeof *data); |
9257d46d PB |
463 | data->start_routine = start_routine; |
464 | data->arg = arg; | |
403e6331 PB |
465 | data->mode = mode; |
466 | data->exited = false; | |
ef57137f | 467 | notifier_list_init(&data->exit); |
9257d46d | 468 | |
edc1de97 SW |
469 | if (data->mode != QEMU_THREAD_DETACHED) { |
470 | InitializeCriticalSection(&data->cs); | |
471 | } | |
472 | ||
9257d46d | 473 | hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine, |
403e6331 | 474 | data, 0, &thread->tid); |
9257d46d PB |
475 | if (!hThread) { |
476 | error_exit(GetLastError(), __func__); | |
477 | } | |
478 | CloseHandle(hThread); | |
ef57137f | 479 | thread->data = data; |
9257d46d PB |
480 | } |
481 | ||
482 | void qemu_thread_get_self(QemuThread *thread) | |
483 | { | |
6265e4ff | 484 | thread->data = qemu_thread_data; |
403e6331 | 485 | thread->tid = GetCurrentThreadId(); |
9257d46d PB |
486 | } |
487 | ||
1ecf47bf PB |
488 | HANDLE qemu_thread_get_handle(QemuThread *thread) |
489 | { | |
490 | QemuThreadData *data; | |
491 | HANDLE handle; | |
492 | ||
493 | data = thread->data; | |
ef57137f | 494 | if (data->mode == QEMU_THREAD_DETACHED) { |
1ecf47bf PB |
495 | return NULL; |
496 | } | |
497 | ||
498 | EnterCriticalSection(&data->cs); | |
499 | if (!data->exited) { | |
b0cb0a66 VP |
500 | handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME | |
501 | THREAD_SET_CONTEXT, FALSE, thread->tid); | |
1ecf47bf PB |
502 | } else { |
503 | handle = NULL; | |
504 | } | |
505 | LeaveCriticalSection(&data->cs); | |
506 | return handle; | |
507 | } | |
508 | ||
2d797b65 | 509 | bool qemu_thread_is_self(QemuThread *thread) |
9257d46d | 510 | { |
403e6331 | 511 | return GetCurrentThreadId() == thread->tid; |
9257d46d | 512 | } |