2 * Wrappers around mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2009
7 * Marcelo Tosatti <mtosatti@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include "qemu/thread.h"
15 #include "qemu/atomic.h"
16 #include "qemu/notify.h"
17 #include "qemu-thread-common.h"
18 #include "qemu/tsan.h"
20 static bool name_threads
;
22 void qemu_thread_naming(bool enable
)
24 name_threads
= enable
;
26 #if !defined CONFIG_PTHREAD_SETNAME_NP_W_TID && \
27 !defined CONFIG_PTHREAD_SETNAME_NP_WO_TID
28 /* This is a debugging option, not fatal */
30 fprintf(stderr
, "qemu: thread naming not supported on this host\n");
35 static void error_exit(int err
, const char *msg
)
37 fprintf(stderr
, "qemu: %s: %s\n", msg
, strerror(err
));
41 static inline clockid_t
qemu_timedwait_clockid(void)
43 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
44 return CLOCK_MONOTONIC
;
46 return CLOCK_REALTIME
;
50 static void compute_abs_deadline(struct timespec
*ts
, int ms
)
52 clock_gettime(qemu_timedwait_clockid(), ts
);
53 ts
->tv_nsec
+= (ms
% 1000) * 1000000;
54 ts
->tv_sec
+= ms
/ 1000;
55 if (ts
->tv_nsec
>= 1000000000) {
57 ts
->tv_nsec
-= 1000000000;
61 void qemu_mutex_init(QemuMutex
*mutex
)
65 err
= pthread_mutex_init(&mutex
->lock
, NULL
);
67 error_exit(err
, __func__
);
68 qemu_mutex_post_init(mutex
);
71 void qemu_mutex_destroy(QemuMutex
*mutex
)
75 assert(mutex
->initialized
);
76 mutex
->initialized
= false;
77 err
= pthread_mutex_destroy(&mutex
->lock
);
79 error_exit(err
, __func__
);
82 void qemu_mutex_lock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
86 assert(mutex
->initialized
);
87 qemu_mutex_pre_lock(mutex
, file
, line
);
88 err
= pthread_mutex_lock(&mutex
->lock
);
90 error_exit(err
, __func__
);
91 qemu_mutex_post_lock(mutex
, file
, line
);
94 int qemu_mutex_trylock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
98 assert(mutex
->initialized
);
99 err
= pthread_mutex_trylock(&mutex
->lock
);
101 qemu_mutex_post_lock(mutex
, file
, line
);
105 error_exit(err
, __func__
);
110 void qemu_mutex_unlock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
114 assert(mutex
->initialized
);
115 qemu_mutex_pre_unlock(mutex
, file
, line
);
116 err
= pthread_mutex_unlock(&mutex
->lock
);
118 error_exit(err
, __func__
);
121 void qemu_rec_mutex_init(QemuRecMutex
*mutex
)
124 pthread_mutexattr_t attr
;
126 pthread_mutexattr_init(&attr
);
127 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
128 err
= pthread_mutex_init(&mutex
->m
.lock
, &attr
);
129 pthread_mutexattr_destroy(&attr
);
131 error_exit(err
, __func__
);
133 mutex
->m
.initialized
= true;
136 void qemu_rec_mutex_destroy(QemuRecMutex
*mutex
)
138 qemu_mutex_destroy(&mutex
->m
);
141 void qemu_rec_mutex_lock_impl(QemuRecMutex
*mutex
, const char *file
, int line
)
143 qemu_mutex_lock_impl(&mutex
->m
, file
, line
);
146 int qemu_rec_mutex_trylock_impl(QemuRecMutex
*mutex
, const char *file
, int line
)
148 return qemu_mutex_trylock_impl(&mutex
->m
, file
, line
);
151 void qemu_rec_mutex_unlock_impl(QemuRecMutex
*mutex
, const char *file
, int line
)
153 qemu_mutex_unlock_impl(&mutex
->m
, file
, line
);
156 void qemu_cond_init(QemuCond
*cond
)
158 pthread_condattr_t attr
;
161 err
= pthread_condattr_init(&attr
);
163 error_exit(err
, __func__
);
165 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
166 err
= pthread_condattr_setclock(&attr
, qemu_timedwait_clockid());
168 error_exit(err
, __func__
);
171 err
= pthread_cond_init(&cond
->cond
, &attr
);
173 error_exit(err
, __func__
);
175 err
= pthread_condattr_destroy(&attr
);
177 error_exit(err
, __func__
);
179 cond
->initialized
= true;
182 void qemu_cond_destroy(QemuCond
*cond
)
186 assert(cond
->initialized
);
187 cond
->initialized
= false;
188 err
= pthread_cond_destroy(&cond
->cond
);
190 error_exit(err
, __func__
);
193 void qemu_cond_signal(QemuCond
*cond
)
197 assert(cond
->initialized
);
198 err
= pthread_cond_signal(&cond
->cond
);
200 error_exit(err
, __func__
);
203 void qemu_cond_broadcast(QemuCond
*cond
)
207 assert(cond
->initialized
);
208 err
= pthread_cond_broadcast(&cond
->cond
);
210 error_exit(err
, __func__
);
213 void qemu_cond_wait_impl(QemuCond
*cond
, QemuMutex
*mutex
, const char *file
, const int line
)
217 assert(cond
->initialized
);
218 qemu_mutex_pre_unlock(mutex
, file
, line
);
219 err
= pthread_cond_wait(&cond
->cond
, &mutex
->lock
);
220 qemu_mutex_post_lock(mutex
, file
, line
);
222 error_exit(err
, __func__
);
226 qemu_cond_timedwait_ts(QemuCond
*cond
, QemuMutex
*mutex
, struct timespec
*ts
,
227 const char *file
, const int line
)
231 assert(cond
->initialized
);
232 trace_qemu_mutex_unlock(mutex
, file
, line
);
233 err
= pthread_cond_timedwait(&cond
->cond
, &mutex
->lock
, ts
);
234 trace_qemu_mutex_locked(mutex
, file
, line
);
235 if (err
&& err
!= ETIMEDOUT
) {
236 error_exit(err
, __func__
);
238 return err
!= ETIMEDOUT
;
241 bool qemu_cond_timedwait_impl(QemuCond
*cond
, QemuMutex
*mutex
, int ms
,
242 const char *file
, const int line
)
246 compute_abs_deadline(&ts
, ms
);
247 return qemu_cond_timedwait_ts(cond
, mutex
, &ts
, file
, line
);
250 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
252 qemu_mutex_init(&sem
->mutex
);
253 qemu_cond_init(&sem
->cond
);
256 error_exit(EINVAL
, __func__
);
261 void qemu_sem_destroy(QemuSemaphore
*sem
)
263 qemu_cond_destroy(&sem
->cond
);
264 qemu_mutex_destroy(&sem
->mutex
);
267 void qemu_sem_post(QemuSemaphore
*sem
)
269 qemu_mutex_lock(&sem
->mutex
);
270 if (sem
->count
== UINT_MAX
) {
271 error_exit(EINVAL
, __func__
);
274 qemu_cond_signal(&sem
->cond
);
276 qemu_mutex_unlock(&sem
->mutex
);
279 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
284 compute_abs_deadline(&ts
, ms
);
285 qemu_mutex_lock(&sem
->mutex
);
286 while (sem
->count
== 0) {
290 rc
= qemu_cond_timedwait_ts(&sem
->cond
, &sem
->mutex
, &ts
,
293 if (!rc
) { /* timeout */
300 qemu_mutex_unlock(&sem
->mutex
);
301 return (rc
? 0 : -1);
304 void qemu_sem_wait(QemuSemaphore
*sem
)
306 qemu_mutex_lock(&sem
->mutex
);
307 while (sem
->count
== 0) {
308 qemu_cond_wait(&sem
->cond
, &sem
->mutex
);
311 qemu_mutex_unlock(&sem
->mutex
);
315 #include "qemu/futex.h"
317 static inline void qemu_futex_wake(QemuEvent
*ev
, int n
)
319 assert(ev
->initialized
);
320 pthread_mutex_lock(&ev
->lock
);
322 pthread_cond_signal(&ev
->cond
);
324 pthread_cond_broadcast(&ev
->cond
);
326 pthread_mutex_unlock(&ev
->lock
);
329 static inline void qemu_futex_wait(QemuEvent
*ev
, unsigned val
)
331 assert(ev
->initialized
);
332 pthread_mutex_lock(&ev
->lock
);
333 if (ev
->value
== val
) {
334 pthread_cond_wait(&ev
->cond
, &ev
->lock
);
336 pthread_mutex_unlock(&ev
->lock
);
340 /* Valid transitions:
341 * - free->set, when setting the event
342 * - busy->set, when setting the event, followed by qemu_futex_wake
343 * - set->free, when resetting the event
344 * - free->busy, when waiting
346 * set->busy does not happen (it can be observed from the outside but
347 * it really is set->free->busy).
349 * busy->free provably cannot happen; to enforce it, the set->free transition
350 * is done with an OR, which becomes a no-op if the event has concurrently
351 * transitioned to free or busy.
358 void qemu_event_init(QemuEvent
*ev
, bool init
)
361 pthread_mutex_init(&ev
->lock
, NULL
);
362 pthread_cond_init(&ev
->cond
, NULL
);
365 ev
->value
= (init
? EV_SET
: EV_FREE
);
366 ev
->initialized
= true;
369 void qemu_event_destroy(QemuEvent
*ev
)
371 assert(ev
->initialized
);
372 ev
->initialized
= false;
374 pthread_mutex_destroy(&ev
->lock
);
375 pthread_cond_destroy(&ev
->cond
);
379 void qemu_event_set(QemuEvent
*ev
)
381 /* qemu_event_set has release semantics, but because it *loads*
382 * ev->value we need a full memory barrier here.
384 assert(ev
->initialized
);
386 if (qatomic_read(&ev
->value
) != EV_SET
) {
387 if (qatomic_xchg(&ev
->value
, EV_SET
) == EV_BUSY
) {
388 /* There were waiters, wake them up. */
389 qemu_futex_wake(ev
, INT_MAX
);
394 void qemu_event_reset(QemuEvent
*ev
)
398 assert(ev
->initialized
);
399 value
= qatomic_read(&ev
->value
);
401 if (value
== EV_SET
) {
403 * If there was a concurrent reset (or even reset+wait),
404 * do nothing. Otherwise change EV_SET->EV_FREE.
406 qatomic_or(&ev
->value
, EV_FREE
);
410 void qemu_event_wait(QemuEvent
*ev
)
414 assert(ev
->initialized
);
415 value
= qatomic_read(&ev
->value
);
417 if (value
!= EV_SET
) {
418 if (value
== EV_FREE
) {
420 * Leave the event reset and tell qemu_event_set that there
421 * are waiters. No need to retry, because there cannot be
422 * a concurrent busy->free transition. After the CAS, the
423 * event will be either set or busy.
425 if (qatomic_cmpxchg(&ev
->value
, EV_FREE
, EV_BUSY
) == EV_SET
) {
429 qemu_futex_wait(ev
, EV_BUSY
);
433 static __thread NotifierList thread_exit
;
436 * Note that in this implementation you can register a thread-exit
437 * notifier for the main thread, but it will never be called.
438 * This is OK because main thread exit can only happen when the
439 * entire process is exiting, and the API allows notifiers to not
440 * be called on process exit.
442 void qemu_thread_atexit_add(Notifier
*notifier
)
444 notifier_list_add(&thread_exit
, notifier
);
447 void qemu_thread_atexit_remove(Notifier
*notifier
)
449 notifier_remove(notifier
);
452 static void qemu_thread_atexit_notify(void *arg
)
455 * Called when non-main thread exits (via qemu_thread_exit()
456 * or by returning from its start routine.)
458 notifier_list_notify(&thread_exit
, NULL
);
462 void *(*start_routine
)(void *);
467 static void *qemu_thread_start(void *args
)
469 QemuThreadArgs
*qemu_thread_args
= args
;
470 void *(*start_routine
)(void *) = qemu_thread_args
->start_routine
;
471 void *arg
= qemu_thread_args
->arg
;
474 /* Attempt to set the threads name; note that this is for debug, so
475 * we're not going to fail if we can't set it.
477 if (name_threads
&& qemu_thread_args
->name
) {
478 # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
479 pthread_setname_np(pthread_self(), qemu_thread_args
->name
);
480 # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
481 pthread_setname_np(qemu_thread_args
->name
);
484 QEMU_TSAN_ANNOTATE_THREAD_NAME(qemu_thread_args
->name
);
485 g_free(qemu_thread_args
->name
);
486 g_free(qemu_thread_args
);
489 * GCC 11 with glibc 2.17 on PowerPC reports
491 * qemu-thread-posix.c:540:5: error: ‘__sigsetjmp’ accessing 656 bytes
492 * in a region of size 528 [-Werror=stringop-overflow=]
493 * 540 | pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
494 * | ^~~~~~~~~~~~~~~~~~~~
496 * which is clearly nonsense.
498 #pragma GCC diagnostic push
500 #pragma GCC diagnostic ignored "-Wstringop-overflow"
503 pthread_cleanup_push(qemu_thread_atexit_notify
, NULL
);
504 r
= start_routine(arg
);
505 pthread_cleanup_pop(1);
507 #pragma GCC diagnostic pop
512 void qemu_thread_create(QemuThread
*thread
, const char *name
,
513 void *(*start_routine
)(void*),
516 sigset_t set
, oldset
;
519 QemuThreadArgs
*qemu_thread_args
;
521 err
= pthread_attr_init(&attr
);
523 error_exit(err
, __func__
);
526 if (mode
== QEMU_THREAD_DETACHED
) {
527 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
530 /* Leave signal handling to the iothread. */
532 /* Blocking the signals can result in undefined behaviour. */
533 sigdelset(&set
, SIGSEGV
);
534 sigdelset(&set
, SIGFPE
);
535 sigdelset(&set
, SIGILL
);
536 /* TODO avoid SIGBUS loss on macOS */
537 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
539 qemu_thread_args
= g_new0(QemuThreadArgs
, 1);
540 qemu_thread_args
->name
= g_strdup(name
);
541 qemu_thread_args
->start_routine
= start_routine
;
542 qemu_thread_args
->arg
= arg
;
544 err
= pthread_create(&thread
->thread
, &attr
,
545 qemu_thread_start
, qemu_thread_args
);
548 error_exit(err
, __func__
);
550 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
552 pthread_attr_destroy(&attr
);
555 void qemu_thread_get_self(QemuThread
*thread
)
557 thread
->thread
= pthread_self();
560 bool qemu_thread_is_self(QemuThread
*thread
)
562 return pthread_equal(pthread_self(), thread
->thread
);
565 void qemu_thread_exit(void *retval
)
567 pthread_exit(retval
);
570 void *qemu_thread_join(QemuThread
*thread
)
575 err
= pthread_join(thread
->thread
, &ret
);
577 error_exit(err
, __func__
);