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 void compute_abs_deadline(struct timespec
*ts
, int ms
)
44 gettimeofday(&tv
, NULL
);
45 ts
->tv_nsec
= tv
.tv_usec
* 1000 + (ms
% 1000) * 1000000;
46 ts
->tv_sec
= tv
.tv_sec
+ ms
/ 1000;
47 if (ts
->tv_nsec
>= 1000000000) {
49 ts
->tv_nsec
-= 1000000000;
53 void qemu_mutex_init(QemuMutex
*mutex
)
57 err
= pthread_mutex_init(&mutex
->lock
, NULL
);
59 error_exit(err
, __func__
);
60 qemu_mutex_post_init(mutex
);
63 void qemu_mutex_destroy(QemuMutex
*mutex
)
67 assert(mutex
->initialized
);
68 mutex
->initialized
= false;
69 err
= pthread_mutex_destroy(&mutex
->lock
);
71 error_exit(err
, __func__
);
74 void qemu_mutex_lock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
78 assert(mutex
->initialized
);
79 qemu_mutex_pre_lock(mutex
, file
, line
);
80 err
= pthread_mutex_lock(&mutex
->lock
);
82 error_exit(err
, __func__
);
83 qemu_mutex_post_lock(mutex
, file
, line
);
86 int qemu_mutex_trylock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
90 assert(mutex
->initialized
);
91 err
= pthread_mutex_trylock(&mutex
->lock
);
93 qemu_mutex_post_lock(mutex
, file
, line
);
97 error_exit(err
, __func__
);
102 void qemu_mutex_unlock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
106 assert(mutex
->initialized
);
107 qemu_mutex_pre_unlock(mutex
, file
, line
);
108 err
= pthread_mutex_unlock(&mutex
->lock
);
110 error_exit(err
, __func__
);
113 void qemu_rec_mutex_init(QemuRecMutex
*mutex
)
116 pthread_mutexattr_t attr
;
118 pthread_mutexattr_init(&attr
);
119 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
120 err
= pthread_mutex_init(&mutex
->m
.lock
, &attr
);
121 pthread_mutexattr_destroy(&attr
);
123 error_exit(err
, __func__
);
125 mutex
->m
.initialized
= true;
128 void qemu_rec_mutex_destroy(QemuRecMutex
*mutex
)
130 qemu_mutex_destroy(&mutex
->m
);
133 void qemu_rec_mutex_lock_impl(QemuRecMutex
*mutex
, const char *file
, int line
)
135 qemu_mutex_lock_impl(&mutex
->m
, file
, line
);
138 int qemu_rec_mutex_trylock_impl(QemuRecMutex
*mutex
, const char *file
, int line
)
140 return qemu_mutex_trylock_impl(&mutex
->m
, file
, line
);
143 void qemu_rec_mutex_unlock_impl(QemuRecMutex
*mutex
, const char *file
, int line
)
145 qemu_mutex_unlock_impl(&mutex
->m
, file
, line
);
148 void qemu_cond_init(QemuCond
*cond
)
152 err
= pthread_cond_init(&cond
->cond
, NULL
);
154 error_exit(err
, __func__
);
155 cond
->initialized
= true;
158 void qemu_cond_destroy(QemuCond
*cond
)
162 assert(cond
->initialized
);
163 cond
->initialized
= false;
164 err
= pthread_cond_destroy(&cond
->cond
);
166 error_exit(err
, __func__
);
169 void qemu_cond_signal(QemuCond
*cond
)
173 assert(cond
->initialized
);
174 err
= pthread_cond_signal(&cond
->cond
);
176 error_exit(err
, __func__
);
179 void qemu_cond_broadcast(QemuCond
*cond
)
183 assert(cond
->initialized
);
184 err
= pthread_cond_broadcast(&cond
->cond
);
186 error_exit(err
, __func__
);
189 void qemu_cond_wait_impl(QemuCond
*cond
, QemuMutex
*mutex
, const char *file
, const int line
)
193 assert(cond
->initialized
);
194 qemu_mutex_pre_unlock(mutex
, file
, line
);
195 err
= pthread_cond_wait(&cond
->cond
, &mutex
->lock
);
196 qemu_mutex_post_lock(mutex
, file
, line
);
198 error_exit(err
, __func__
);
201 bool qemu_cond_timedwait_impl(QemuCond
*cond
, QemuMutex
*mutex
, int ms
,
202 const char *file
, const int line
)
207 assert(cond
->initialized
);
208 trace_qemu_mutex_unlock(mutex
, file
, line
);
209 compute_abs_deadline(&ts
, ms
);
210 err
= pthread_cond_timedwait(&cond
->cond
, &mutex
->lock
, &ts
);
211 trace_qemu_mutex_locked(mutex
, file
, line
);
212 if (err
&& err
!= ETIMEDOUT
) {
213 error_exit(err
, __func__
);
215 return err
!= ETIMEDOUT
;
218 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
222 #ifndef CONFIG_SEM_TIMEDWAIT
223 rc
= pthread_mutex_init(&sem
->lock
, NULL
);
225 error_exit(rc
, __func__
);
227 rc
= pthread_cond_init(&sem
->cond
, NULL
);
229 error_exit(rc
, __func__
);
232 error_exit(EINVAL
, __func__
);
236 rc
= sem_init(&sem
->sem
, 0, init
);
238 error_exit(errno
, __func__
);
241 sem
->initialized
= true;
244 void qemu_sem_destroy(QemuSemaphore
*sem
)
248 assert(sem
->initialized
);
249 sem
->initialized
= false;
250 #ifndef CONFIG_SEM_TIMEDWAIT
251 rc
= pthread_cond_destroy(&sem
->cond
);
253 error_exit(rc
, __func__
);
255 rc
= pthread_mutex_destroy(&sem
->lock
);
257 error_exit(rc
, __func__
);
260 rc
= sem_destroy(&sem
->sem
);
262 error_exit(errno
, __func__
);
267 void qemu_sem_post(QemuSemaphore
*sem
)
271 assert(sem
->initialized
);
272 #ifndef CONFIG_SEM_TIMEDWAIT
273 pthread_mutex_lock(&sem
->lock
);
274 if (sem
->count
== UINT_MAX
) {
278 rc
= pthread_cond_signal(&sem
->cond
);
280 pthread_mutex_unlock(&sem
->lock
);
282 error_exit(rc
, __func__
);
285 rc
= sem_post(&sem
->sem
);
287 error_exit(errno
, __func__
);
292 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
297 assert(sem
->initialized
);
298 #ifndef CONFIG_SEM_TIMEDWAIT
300 compute_abs_deadline(&ts
, ms
);
301 pthread_mutex_lock(&sem
->lock
);
302 while (sem
->count
== 0) {
303 rc
= pthread_cond_timedwait(&sem
->cond
, &sem
->lock
, &ts
);
304 if (rc
== ETIMEDOUT
) {
308 error_exit(rc
, __func__
);
311 if (rc
!= ETIMEDOUT
) {
314 pthread_mutex_unlock(&sem
->lock
);
315 return (rc
== ETIMEDOUT
? -1 : 0);
318 /* This is cheaper than sem_timedwait. */
320 rc
= sem_trywait(&sem
->sem
);
321 } while (rc
== -1 && errno
== EINTR
);
322 if (rc
== -1 && errno
== EAGAIN
) {
326 compute_abs_deadline(&ts
, ms
);
328 rc
= sem_timedwait(&sem
->sem
, &ts
);
329 } while (rc
== -1 && errno
== EINTR
);
330 if (rc
== -1 && errno
== ETIMEDOUT
) {
335 error_exit(errno
, __func__
);
341 void qemu_sem_wait(QemuSemaphore
*sem
)
345 assert(sem
->initialized
);
346 #ifndef CONFIG_SEM_TIMEDWAIT
347 pthread_mutex_lock(&sem
->lock
);
348 while (sem
->count
== 0) {
349 rc
= pthread_cond_wait(&sem
->cond
, &sem
->lock
);
351 error_exit(rc
, __func__
);
355 pthread_mutex_unlock(&sem
->lock
);
358 rc
= sem_wait(&sem
->sem
);
359 } while (rc
== -1 && errno
== EINTR
);
361 error_exit(errno
, __func__
);
367 #include "qemu/futex.h"
369 static inline void qemu_futex_wake(QemuEvent
*ev
, int n
)
371 assert(ev
->initialized
);
372 pthread_mutex_lock(&ev
->lock
);
374 pthread_cond_signal(&ev
->cond
);
376 pthread_cond_broadcast(&ev
->cond
);
378 pthread_mutex_unlock(&ev
->lock
);
381 static inline void qemu_futex_wait(QemuEvent
*ev
, unsigned val
)
383 assert(ev
->initialized
);
384 pthread_mutex_lock(&ev
->lock
);
385 if (ev
->value
== val
) {
386 pthread_cond_wait(&ev
->cond
, &ev
->lock
);
388 pthread_mutex_unlock(&ev
->lock
);
392 /* Valid transitions:
393 * - free->set, when setting the event
394 * - busy->set, when setting the event, followed by qemu_futex_wake
395 * - set->free, when resetting the event
396 * - free->busy, when waiting
398 * set->busy does not happen (it can be observed from the outside but
399 * it really is set->free->busy).
401 * busy->free provably cannot happen; to enforce it, the set->free transition
402 * is done with an OR, which becomes a no-op if the event has concurrently
403 * transitioned to free or busy.
410 void qemu_event_init(QemuEvent
*ev
, bool init
)
413 pthread_mutex_init(&ev
->lock
, NULL
);
414 pthread_cond_init(&ev
->cond
, NULL
);
417 ev
->value
= (init
? EV_SET
: EV_FREE
);
418 ev
->initialized
= true;
421 void qemu_event_destroy(QemuEvent
*ev
)
423 assert(ev
->initialized
);
424 ev
->initialized
= false;
426 pthread_mutex_destroy(&ev
->lock
);
427 pthread_cond_destroy(&ev
->cond
);
431 void qemu_event_set(QemuEvent
*ev
)
433 /* qemu_event_set has release semantics, but because it *loads*
434 * ev->value we need a full memory barrier here.
436 assert(ev
->initialized
);
438 if (qatomic_read(&ev
->value
) != EV_SET
) {
439 if (qatomic_xchg(&ev
->value
, EV_SET
) == EV_BUSY
) {
440 /* There were waiters, wake them up. */
441 qemu_futex_wake(ev
, INT_MAX
);
446 void qemu_event_reset(QemuEvent
*ev
)
450 assert(ev
->initialized
);
451 value
= qatomic_read(&ev
->value
);
453 if (value
== EV_SET
) {
455 * If there was a concurrent reset (or even reset+wait),
456 * do nothing. Otherwise change EV_SET->EV_FREE.
458 qatomic_or(&ev
->value
, EV_FREE
);
462 void qemu_event_wait(QemuEvent
*ev
)
466 assert(ev
->initialized
);
467 value
= qatomic_read(&ev
->value
);
469 if (value
!= EV_SET
) {
470 if (value
== EV_FREE
) {
472 * Leave the event reset and tell qemu_event_set that there
473 * are waiters. No need to retry, because there cannot be
474 * a concurrent busy->free transition. After the CAS, the
475 * event will be either set or busy.
477 if (qatomic_cmpxchg(&ev
->value
, EV_FREE
, EV_BUSY
) == EV_SET
) {
481 qemu_futex_wait(ev
, EV_BUSY
);
485 static __thread NotifierList thread_exit
;
488 * Note that in this implementation you can register a thread-exit
489 * notifier for the main thread, but it will never be called.
490 * This is OK because main thread exit can only happen when the
491 * entire process is exiting, and the API allows notifiers to not
492 * be called on process exit.
494 void qemu_thread_atexit_add(Notifier
*notifier
)
496 notifier_list_add(&thread_exit
, notifier
);
499 void qemu_thread_atexit_remove(Notifier
*notifier
)
501 notifier_remove(notifier
);
504 static void qemu_thread_atexit_notify(void *arg
)
507 * Called when non-main thread exits (via qemu_thread_exit()
508 * or by returning from its start routine.)
510 notifier_list_notify(&thread_exit
, NULL
);
514 void *(*start_routine
)(void *);
519 static void *qemu_thread_start(void *args
)
521 QemuThreadArgs
*qemu_thread_args
= args
;
522 void *(*start_routine
)(void *) = qemu_thread_args
->start_routine
;
523 void *arg
= qemu_thread_args
->arg
;
526 /* Attempt to set the threads name; note that this is for debug, so
527 * we're not going to fail if we can't set it.
529 if (name_threads
&& qemu_thread_args
->name
) {
530 # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
531 pthread_setname_np(pthread_self(), qemu_thread_args
->name
);
532 # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
533 pthread_setname_np(qemu_thread_args
->name
);
536 QEMU_TSAN_ANNOTATE_THREAD_NAME(qemu_thread_args
->name
);
537 g_free(qemu_thread_args
->name
);
538 g_free(qemu_thread_args
);
541 * GCC 11 with glibc 2.17 on PowerPC reports
543 * qemu-thread-posix.c:540:5: error: ‘__sigsetjmp’ accessing 656 bytes
544 * in a region of size 528 [-Werror=stringop-overflow=]
545 * 540 | pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
546 * | ^~~~~~~~~~~~~~~~~~~~
548 * which is clearly nonsense.
550 #pragma GCC diagnostic push
552 #pragma GCC diagnostic ignored "-Wstringop-overflow"
555 pthread_cleanup_push(qemu_thread_atexit_notify
, NULL
);
556 r
= start_routine(arg
);
557 pthread_cleanup_pop(1);
559 #pragma GCC diagnostic pop
564 void qemu_thread_create(QemuThread
*thread
, const char *name
,
565 void *(*start_routine
)(void*),
568 sigset_t set
, oldset
;
571 QemuThreadArgs
*qemu_thread_args
;
573 err
= pthread_attr_init(&attr
);
575 error_exit(err
, __func__
);
578 if (mode
== QEMU_THREAD_DETACHED
) {
579 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
582 /* Leave signal handling to the iothread. */
584 /* Blocking the signals can result in undefined behaviour. */
585 sigdelset(&set
, SIGSEGV
);
586 sigdelset(&set
, SIGFPE
);
587 sigdelset(&set
, SIGILL
);
588 /* TODO avoid SIGBUS loss on macOS */
589 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
591 qemu_thread_args
= g_new0(QemuThreadArgs
, 1);
592 qemu_thread_args
->name
= g_strdup(name
);
593 qemu_thread_args
->start_routine
= start_routine
;
594 qemu_thread_args
->arg
= arg
;
596 err
= pthread_create(&thread
->thread
, &attr
,
597 qemu_thread_start
, qemu_thread_args
);
600 error_exit(err
, __func__
);
602 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
604 pthread_attr_destroy(&attr
);
607 void qemu_thread_get_self(QemuThread
*thread
)
609 thread
->thread
= pthread_self();
612 bool qemu_thread_is_self(QemuThread
*thread
)
614 return pthread_equal(pthread_self(), thread
->thread
);
617 void qemu_thread_exit(void *retval
)
619 pthread_exit(retval
);
622 void *qemu_thread_join(QemuThread
*thread
)
627 err
= pthread_join(thread
->thread
, &ret
);
629 error_exit(err
, __func__
);