xref: /openbmc/qemu/util/qemu-thread-win32.c (revision 80e5db30)
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  */
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
15 #include "qemu/thread.h"
16 #include "qemu/notify.h"
17 #include <process.h>
18 
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;
25 
26     fprintf(stderr, "qemu: thread naming not supported on this host\n");
27 }
28 
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);
37     abort();
38 }
39 
40 void qemu_mutex_init(QemuMutex *mutex)
41 {
42     mutex->owner = 0;
43     InitializeCriticalSection(&mutex->lock);
44 }
45 
46 void qemu_mutex_destroy(QemuMutex *mutex)
47 {
48     assert(mutex->owner == 0);
49     DeleteCriticalSection(&mutex->lock);
50 }
51 
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 
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 
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 
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 
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 
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 
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
272  * - busy->set, when setting the event, followed by SetEvent
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 
288 void qemu_event_init(QemuEvent *ev, bool init)
289 {
290     /* Manual reset.  */
291     ev->event = CreateEvent(NULL, TRUE, TRUE, NULL);
292     ev->value = (init ? EV_SET : EV_FREE);
293 }
294 
295 void qemu_event_destroy(QemuEvent *ev)
296 {
297     CloseHandle(ev->event);
298 }
299 
300 void qemu_event_set(QemuEvent *ev)
301 {
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) {
307         if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
308             /* There were waiters, wake them up.  */
309             SetEvent(ev->event);
310         }
311     }
312 }
313 
314 void qemu_event_reset(QemuEvent *ev)
315 {
316     unsigned value;
317 
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.
323          */
324         atomic_or(&ev->value, EV_FREE);
325     }
326 }
327 
328 void qemu_event_wait(QemuEvent *ev)
329 {
330     unsigned value;
331 
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.
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     }
356 }
357 
358 struct QemuThreadData {
359     /* Passed to win32_start_routine.  */
360     void             *(*start_routine)(void *);
361     void             *arg;
362     short             mode;
363     NotifierList      exit;
364 
365     /* Only used for joinable threads. */
366     bool              exited;
367     void             *ret;
368     CRITICAL_SECTION  cs;
369 };
370 
371 static bool atexit_registered;
372 static NotifierList main_thread_exit;
373 
374 static __thread QemuThreadData *qemu_thread_data;
375 
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 
399 static unsigned __stdcall win32_start_routine(void *arg)
400 {
401     QemuThreadData *data = (QemuThreadData *) arg;
402     void *(*start_routine)(void *) = data->start_routine;
403     void *thread_arg = data->arg;
404 
405     qemu_thread_data = data;
406     qemu_thread_exit(start_routine(thread_arg));
407     abort();
408 }
409 
410 void qemu_thread_exit(void *arg)
411 {
412     QemuThreadData *data = qemu_thread_data;
413 
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);
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;
433     if (data->mode == QEMU_THREAD_DETACHED) {
434         return NULL;
435     }
436 
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      */
444     handle = qemu_thread_get_handle(thread);
445     if (handle) {
446         WaitForSingleObject(handle, INFINITE);
447         CloseHandle(handle);
448     }
449     ret = data->ret;
450     DeleteCriticalSection(&data->cs);
451     g_free(data);
452     return ret;
453 }
454 
455 void qemu_thread_create(QemuThread *thread, const char *name,
456                        void *(*start_routine)(void *),
457                        void *arg, int mode)
458 {
459     HANDLE hThread;
460     struct QemuThreadData *data;
461 
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);
468 
469     if (data->mode != QEMU_THREAD_DETACHED) {
470         InitializeCriticalSection(&data->cs);
471     }
472 
473     hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
474                                       data, 0, &thread->tid);
475     if (!hThread) {
476         error_exit(GetLastError(), __func__);
477     }
478     CloseHandle(hThread);
479     thread->data = data;
480 }
481 
482 void qemu_thread_get_self(QemuThread *thread)
483 {
484     thread->data = qemu_thread_data;
485     thread->tid = GetCurrentThreadId();
486 }
487 
488 HANDLE qemu_thread_get_handle(QemuThread *thread)
489 {
490     QemuThreadData *data;
491     HANDLE handle;
492 
493     data = thread->data;
494     if (data->mode == QEMU_THREAD_DETACHED) {
495         return NULL;
496     }
497 
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;
504     }
505     LeaveCriticalSection(&data->cs);
506     return handle;
507 }
508 
509 bool qemu_thread_is_self(QemuThread *thread)
510 {
511     return GetCurrentThreadId() == thread->tid;
512 }
513