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"
19 static bool name_threads
;
21 void qemu_thread_naming(bool enable
)
23 name_threads
= enable
;
25 #ifndef CONFIG_THREAD_SETNAME_BYTHREAD
26 /* This is a debugging option, not fatal */
28 fprintf(stderr
, "qemu: thread naming not supported on this host\n");
33 static void error_exit(int err
, const char *msg
)
35 fprintf(stderr
, "qemu: %s: %s\n", msg
, strerror(err
));
39 static void compute_abs_deadline(struct timespec
*ts
, int ms
)
42 gettimeofday(&tv
, NULL
);
43 ts
->tv_nsec
= tv
.tv_usec
* 1000 + (ms
% 1000) * 1000000;
44 ts
->tv_sec
= tv
.tv_sec
+ ms
/ 1000;
45 if (ts
->tv_nsec
>= 1000000000) {
47 ts
->tv_nsec
-= 1000000000;
51 void qemu_mutex_init(QemuMutex
*mutex
)
55 err
= pthread_mutex_init(&mutex
->lock
, NULL
);
57 error_exit(err
, __func__
);
58 qemu_mutex_post_init(mutex
);
61 void qemu_mutex_destroy(QemuMutex
*mutex
)
65 assert(mutex
->initialized
);
66 mutex
->initialized
= false;
67 err
= pthread_mutex_destroy(&mutex
->lock
);
69 error_exit(err
, __func__
);
72 void qemu_mutex_lock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
76 assert(mutex
->initialized
);
77 qemu_mutex_pre_lock(mutex
, file
, line
);
78 err
= pthread_mutex_lock(&mutex
->lock
);
80 error_exit(err
, __func__
);
81 qemu_mutex_post_lock(mutex
, file
, line
);
84 int qemu_mutex_trylock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
88 assert(mutex
->initialized
);
89 err
= pthread_mutex_trylock(&mutex
->lock
);
91 qemu_mutex_post_lock(mutex
, file
, line
);
95 error_exit(err
, __func__
);
100 void qemu_mutex_unlock_impl(QemuMutex
*mutex
, const char *file
, const int line
)
104 assert(mutex
->initialized
);
105 qemu_mutex_pre_unlock(mutex
, file
, line
);
106 err
= pthread_mutex_unlock(&mutex
->lock
);
108 error_exit(err
, __func__
);
111 void qemu_rec_mutex_init(QemuRecMutex
*mutex
)
114 pthread_mutexattr_t attr
;
116 pthread_mutexattr_init(&attr
);
117 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
118 err
= pthread_mutex_init(&mutex
->lock
, &attr
);
119 pthread_mutexattr_destroy(&attr
);
121 error_exit(err
, __func__
);
123 mutex
->initialized
= true;
126 void qemu_cond_init(QemuCond
*cond
)
130 err
= pthread_cond_init(&cond
->cond
, NULL
);
132 error_exit(err
, __func__
);
133 cond
->initialized
= true;
136 void qemu_cond_destroy(QemuCond
*cond
)
140 assert(cond
->initialized
);
141 cond
->initialized
= false;
142 err
= pthread_cond_destroy(&cond
->cond
);
144 error_exit(err
, __func__
);
147 void qemu_cond_signal(QemuCond
*cond
)
151 assert(cond
->initialized
);
152 err
= pthread_cond_signal(&cond
->cond
);
154 error_exit(err
, __func__
);
157 void qemu_cond_broadcast(QemuCond
*cond
)
161 assert(cond
->initialized
);
162 err
= pthread_cond_broadcast(&cond
->cond
);
164 error_exit(err
, __func__
);
167 void qemu_cond_wait_impl(QemuCond
*cond
, QemuMutex
*mutex
, const char *file
, const int line
)
171 assert(cond
->initialized
);
172 qemu_mutex_pre_unlock(mutex
, file
, line
);
173 err
= pthread_cond_wait(&cond
->cond
, &mutex
->lock
);
174 qemu_mutex_post_lock(mutex
, file
, line
);
176 error_exit(err
, __func__
);
179 bool qemu_cond_timedwait_impl(QemuCond
*cond
, QemuMutex
*mutex
, int ms
,
180 const char *file
, const int line
)
185 assert(cond
->initialized
);
186 trace_qemu_mutex_unlock(mutex
, file
, line
);
187 compute_abs_deadline(&ts
, ms
);
188 err
= pthread_cond_timedwait(&cond
->cond
, &mutex
->lock
, &ts
);
189 trace_qemu_mutex_locked(mutex
, file
, line
);
190 if (err
&& err
!= ETIMEDOUT
) {
191 error_exit(err
, __func__
);
193 return err
!= ETIMEDOUT
;
196 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
200 #ifndef CONFIG_SEM_TIMEDWAIT
201 rc
= pthread_mutex_init(&sem
->lock
, NULL
);
203 error_exit(rc
, __func__
);
205 rc
= pthread_cond_init(&sem
->cond
, NULL
);
207 error_exit(rc
, __func__
);
210 error_exit(EINVAL
, __func__
);
214 rc
= sem_init(&sem
->sem
, 0, init
);
216 error_exit(errno
, __func__
);
219 sem
->initialized
= true;
222 void qemu_sem_destroy(QemuSemaphore
*sem
)
226 assert(sem
->initialized
);
227 sem
->initialized
= false;
228 #ifndef CONFIG_SEM_TIMEDWAIT
229 rc
= pthread_cond_destroy(&sem
->cond
);
231 error_exit(rc
, __func__
);
233 rc
= pthread_mutex_destroy(&sem
->lock
);
235 error_exit(rc
, __func__
);
238 rc
= sem_destroy(&sem
->sem
);
240 error_exit(errno
, __func__
);
245 void qemu_sem_post(QemuSemaphore
*sem
)
249 assert(sem
->initialized
);
250 #ifndef CONFIG_SEM_TIMEDWAIT
251 pthread_mutex_lock(&sem
->lock
);
252 if (sem
->count
== UINT_MAX
) {
256 rc
= pthread_cond_signal(&sem
->cond
);
258 pthread_mutex_unlock(&sem
->lock
);
260 error_exit(rc
, __func__
);
263 rc
= sem_post(&sem
->sem
);
265 error_exit(errno
, __func__
);
270 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
275 assert(sem
->initialized
);
276 #ifndef CONFIG_SEM_TIMEDWAIT
278 compute_abs_deadline(&ts
, ms
);
279 pthread_mutex_lock(&sem
->lock
);
280 while (sem
->count
== 0) {
281 rc
= pthread_cond_timedwait(&sem
->cond
, &sem
->lock
, &ts
);
282 if (rc
== ETIMEDOUT
) {
286 error_exit(rc
, __func__
);
289 if (rc
!= ETIMEDOUT
) {
292 pthread_mutex_unlock(&sem
->lock
);
293 return (rc
== ETIMEDOUT
? -1 : 0);
296 /* This is cheaper than sem_timedwait. */
298 rc
= sem_trywait(&sem
->sem
);
299 } while (rc
== -1 && errno
== EINTR
);
300 if (rc
== -1 && errno
== EAGAIN
) {
304 compute_abs_deadline(&ts
, ms
);
306 rc
= sem_timedwait(&sem
->sem
, &ts
);
307 } while (rc
== -1 && errno
== EINTR
);
308 if (rc
== -1 && errno
== ETIMEDOUT
) {
313 error_exit(errno
, __func__
);
319 void qemu_sem_wait(QemuSemaphore
*sem
)
323 assert(sem
->initialized
);
324 #ifndef CONFIG_SEM_TIMEDWAIT
325 pthread_mutex_lock(&sem
->lock
);
326 while (sem
->count
== 0) {
327 rc
= pthread_cond_wait(&sem
->cond
, &sem
->lock
);
329 error_exit(rc
, __func__
);
333 pthread_mutex_unlock(&sem
->lock
);
336 rc
= sem_wait(&sem
->sem
);
337 } while (rc
== -1 && errno
== EINTR
);
339 error_exit(errno
, __func__
);
345 #include "qemu/futex.h"
347 static inline void qemu_futex_wake(QemuEvent
*ev
, int n
)
349 assert(ev
->initialized
);
350 pthread_mutex_lock(&ev
->lock
);
352 pthread_cond_signal(&ev
->cond
);
354 pthread_cond_broadcast(&ev
->cond
);
356 pthread_mutex_unlock(&ev
->lock
);
359 static inline void qemu_futex_wait(QemuEvent
*ev
, unsigned val
)
361 assert(ev
->initialized
);
362 pthread_mutex_lock(&ev
->lock
);
363 if (ev
->value
== val
) {
364 pthread_cond_wait(&ev
->cond
, &ev
->lock
);
366 pthread_mutex_unlock(&ev
->lock
);
370 /* Valid transitions:
371 * - free->set, when setting the event
372 * - busy->set, when setting the event, followed by qemu_futex_wake
373 * - set->free, when resetting the event
374 * - free->busy, when waiting
376 * set->busy does not happen (it can be observed from the outside but
377 * it really is set->free->busy).
379 * busy->free provably cannot happen; to enforce it, the set->free transition
380 * is done with an OR, which becomes a no-op if the event has concurrently
381 * transitioned to free or busy.
388 void qemu_event_init(QemuEvent
*ev
, bool init
)
391 pthread_mutex_init(&ev
->lock
, NULL
);
392 pthread_cond_init(&ev
->cond
, NULL
);
395 ev
->value
= (init
? EV_SET
: EV_FREE
);
396 ev
->initialized
= true;
399 void qemu_event_destroy(QemuEvent
*ev
)
401 assert(ev
->initialized
);
402 ev
->initialized
= false;
404 pthread_mutex_destroy(&ev
->lock
);
405 pthread_cond_destroy(&ev
->cond
);
409 void qemu_event_set(QemuEvent
*ev
)
411 /* qemu_event_set has release semantics, but because it *loads*
412 * ev->value we need a full memory barrier here.
414 assert(ev
->initialized
);
416 if (atomic_read(&ev
->value
) != EV_SET
) {
417 if (atomic_xchg(&ev
->value
, EV_SET
) == EV_BUSY
) {
418 /* There were waiters, wake them up. */
419 qemu_futex_wake(ev
, INT_MAX
);
424 void qemu_event_reset(QemuEvent
*ev
)
428 assert(ev
->initialized
);
429 value
= atomic_read(&ev
->value
);
431 if (value
== EV_SET
) {
433 * If there was a concurrent reset (or even reset+wait),
434 * do nothing. Otherwise change EV_SET->EV_FREE.
436 atomic_or(&ev
->value
, EV_FREE
);
440 void qemu_event_wait(QemuEvent
*ev
)
444 assert(ev
->initialized
);
445 value
= atomic_read(&ev
->value
);
447 if (value
!= EV_SET
) {
448 if (value
== EV_FREE
) {
450 * Leave the event reset and tell qemu_event_set that there
451 * are waiters. No need to retry, because there cannot be
452 * a concurrent busy->free transition. After the CAS, the
453 * event will be either set or busy.
455 if (atomic_cmpxchg(&ev
->value
, EV_FREE
, EV_BUSY
) == EV_SET
) {
459 qemu_futex_wait(ev
, EV_BUSY
);
463 static __thread NotifierList thread_exit
;
466 * Note that in this implementation you can register a thread-exit
467 * notifier for the main thread, but it will never be called.
468 * This is OK because main thread exit can only happen when the
469 * entire process is exiting, and the API allows notifiers to not
470 * be called on process exit.
472 void qemu_thread_atexit_add(Notifier
*notifier
)
474 notifier_list_add(&thread_exit
, notifier
);
477 void qemu_thread_atexit_remove(Notifier
*notifier
)
479 notifier_remove(notifier
);
482 static void qemu_thread_atexit_notify(void *arg
)
485 * Called when non-main thread exits (via qemu_thread_exit()
486 * or by returning from its start routine.)
488 notifier_list_notify(&thread_exit
, NULL
);
492 void *(*start_routine
)(void *);
497 static void *qemu_thread_start(void *args
)
499 QemuThreadArgs
*qemu_thread_args
= args
;
500 void *(*start_routine
)(void *) = qemu_thread_args
->start_routine
;
501 void *arg
= qemu_thread_args
->arg
;
504 #ifdef CONFIG_THREAD_SETNAME_BYTHREAD
505 /* Attempt to set the threads name; note that this is for debug, so
506 * we're not going to fail if we can't set it.
508 if (name_threads
&& qemu_thread_args
->name
) {
509 # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
510 pthread_setname_np(pthread_self(), qemu_thread_args
->name
);
511 # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
512 pthread_setname_np(qemu_thread_args
->name
);
516 g_free(qemu_thread_args
->name
);
517 g_free(qemu_thread_args
);
518 pthread_cleanup_push(qemu_thread_atexit_notify
, NULL
);
519 r
= start_routine(arg
);
520 pthread_cleanup_pop(1);
524 void qemu_thread_create(QemuThread
*thread
, const char *name
,
525 void *(*start_routine
)(void*),
528 sigset_t set
, oldset
;
531 QemuThreadArgs
*qemu_thread_args
;
533 err
= pthread_attr_init(&attr
);
535 error_exit(err
, __func__
);
538 if (mode
== QEMU_THREAD_DETACHED
) {
539 pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
542 /* Leave signal handling to the iothread. */
544 /* Blocking the signals can result in undefined behaviour. */
545 sigdelset(&set
, SIGSEGV
);
546 sigdelset(&set
, SIGFPE
);
547 sigdelset(&set
, SIGILL
);
548 /* TODO avoid SIGBUS loss on macOS */
549 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
551 qemu_thread_args
= g_new0(QemuThreadArgs
, 1);
552 qemu_thread_args
->name
= g_strdup(name
);
553 qemu_thread_args
->start_routine
= start_routine
;
554 qemu_thread_args
->arg
= arg
;
556 err
= pthread_create(&thread
->thread
, &attr
,
557 qemu_thread_start
, qemu_thread_args
);
560 error_exit(err
, __func__
);
562 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
564 pthread_attr_destroy(&attr
);
567 void qemu_thread_get_self(QemuThread
*thread
)
569 thread
->thread
= pthread_self();
572 bool qemu_thread_is_self(QemuThread
*thread
)
574 return pthread_equal(pthread_self(), thread
->thread
);
577 void qemu_thread_exit(void *retval
)
579 pthread_exit(retval
);
582 void *qemu_thread_join(QemuThread
*thread
)
587 err
= pthread_join(thread
->thread
, &ret
);
589 error_exit(err
, __func__
);