audio: run downstream playback queue unconditionally
[qemu/ar7.git] / util / qemu-thread-posix.c
blobac1d56e673f37e45cb870ec5cc6c182d68ec24ea
1 /*
2 * Wrappers around mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2009
6 * Author:
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 */
29 if (enable) {
30 fprintf(stderr, "qemu: thread naming not supported on this host\n");
32 #endif
35 static void error_exit(int err, const char *msg)
37 fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
38 abort();
41 static inline clockid_t qemu_timedwait_clockid(void)
43 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
44 return CLOCK_MONOTONIC;
45 #else
46 return CLOCK_REALTIME;
47 #endif
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) {
56 ts->tv_sec++;
57 ts->tv_nsec -= 1000000000;
61 void qemu_mutex_init(QemuMutex *mutex)
63 int err;
65 err = pthread_mutex_init(&mutex->lock, NULL);
66 if (err)
67 error_exit(err, __func__);
68 qemu_mutex_post_init(mutex);
71 void qemu_mutex_destroy(QemuMutex *mutex)
73 int err;
75 assert(mutex->initialized);
76 mutex->initialized = false;
77 err = pthread_mutex_destroy(&mutex->lock);
78 if (err)
79 error_exit(err, __func__);
82 void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
84 int err;
86 assert(mutex->initialized);
87 qemu_mutex_pre_lock(mutex, file, line);
88 err = pthread_mutex_lock(&mutex->lock);
89 if (err)
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)
96 int err;
98 assert(mutex->initialized);
99 err = pthread_mutex_trylock(&mutex->lock);
100 if (err == 0) {
101 qemu_mutex_post_lock(mutex, file, line);
102 return 0;
104 if (err != EBUSY) {
105 error_exit(err, __func__);
107 return -EBUSY;
110 void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
112 int err;
114 assert(mutex->initialized);
115 qemu_mutex_pre_unlock(mutex, file, line);
116 err = pthread_mutex_unlock(&mutex->lock);
117 if (err)
118 error_exit(err, __func__);
121 void qemu_rec_mutex_init(QemuRecMutex *mutex)
123 int err;
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);
130 if (err) {
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;
159 int err;
161 err = pthread_condattr_init(&attr);
162 if (err) {
163 error_exit(err, __func__);
165 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
166 err = pthread_condattr_setclock(&attr, qemu_timedwait_clockid());
167 if (err) {
168 error_exit(err, __func__);
170 #endif
171 err = pthread_cond_init(&cond->cond, &attr);
172 if (err) {
173 error_exit(err, __func__);
175 err = pthread_condattr_destroy(&attr);
176 if (err) {
177 error_exit(err, __func__);
179 cond->initialized = true;
182 void qemu_cond_destroy(QemuCond *cond)
184 int err;
186 assert(cond->initialized);
187 cond->initialized = false;
188 err = pthread_cond_destroy(&cond->cond);
189 if (err)
190 error_exit(err, __func__);
193 void qemu_cond_signal(QemuCond *cond)
195 int err;
197 assert(cond->initialized);
198 err = pthread_cond_signal(&cond->cond);
199 if (err)
200 error_exit(err, __func__);
203 void qemu_cond_broadcast(QemuCond *cond)
205 int err;
207 assert(cond->initialized);
208 err = pthread_cond_broadcast(&cond->cond);
209 if (err)
210 error_exit(err, __func__);
213 void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
215 int err;
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);
221 if (err)
222 error_exit(err, __func__);
225 static bool
226 qemu_cond_timedwait_ts(QemuCond *cond, QemuMutex *mutex, struct timespec *ts,
227 const char *file, const int line)
229 int err;
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)
244 struct timespec ts;
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);
255 if (init < 0) {
256 error_exit(EINVAL, __func__);
258 sem->count = init;
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__);
272 } else {
273 sem->count++;
274 qemu_cond_signal(&sem->cond);
276 qemu_mutex_unlock(&sem->mutex);
279 int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
281 bool rc = true;
282 struct timespec ts;
284 compute_abs_deadline(&ts, ms);
285 qemu_mutex_lock(&sem->mutex);
286 while (sem->count == 0) {
287 if (ms == 0) {
288 rc = false;
289 } else {
290 rc = qemu_cond_timedwait_ts(&sem->cond, &sem->mutex, &ts,
291 __FILE__, __LINE__);
293 if (!rc) { /* timeout */
294 break;
297 if (rc) {
298 --sem->count;
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);
310 --sem->count;
311 qemu_mutex_unlock(&sem->mutex);
314 #ifdef __linux__
315 #include "qemu/futex.h"
316 #else
317 static inline void qemu_futex_wake(QemuEvent *ev, int n)
319 assert(ev->initialized);
320 pthread_mutex_lock(&ev->lock);
321 if (n == 1) {
322 pthread_cond_signal(&ev->cond);
323 } else {
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);
338 #endif
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.
354 #define EV_SET 0
355 #define EV_FREE 1
356 #define EV_BUSY -1
358 void qemu_event_init(QemuEvent *ev, bool init)
360 #ifndef __linux__
361 pthread_mutex_init(&ev->lock, NULL);
362 pthread_cond_init(&ev->cond, NULL);
363 #endif
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;
373 #ifndef __linux__
374 pthread_mutex_destroy(&ev->lock);
375 pthread_cond_destroy(&ev->cond);
376 #endif
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);
385 smp_mb();
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)
396 unsigned value;
398 assert(ev->initialized);
399 value = qatomic_read(&ev->value);
400 smp_mb_acquire();
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)
412 unsigned value;
414 assert(ev->initialized);
415 value = qatomic_read(&ev->value);
416 smp_mb_acquire();
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) {
426 return;
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);
461 typedef struct {
462 void *(*start_routine)(void *);
463 void *arg;
464 char *name;
465 } QemuThreadArgs;
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;
472 void *r;
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);
482 # endif
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
499 #ifndef __clang__
500 #pragma GCC diagnostic ignored "-Wstringop-overflow"
501 #endif
503 pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
504 r = start_routine(arg);
505 pthread_cleanup_pop(1);
507 #pragma GCC diagnostic pop
509 return r;
512 void qemu_thread_create(QemuThread *thread, const char *name,
513 void *(*start_routine)(void*),
514 void *arg, int mode)
516 sigset_t set, oldset;
517 int err;
518 pthread_attr_t attr;
519 QemuThreadArgs *qemu_thread_args;
521 err = pthread_attr_init(&attr);
522 if (err) {
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. */
531 sigfillset(&set);
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);
547 if (err)
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)
572 int err;
573 void *ret;
575 err = pthread_join(thread->thread, &ret);
576 if (err) {
577 error_exit(err, __func__);
579 return ret;