2 * Wrappers around mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2009
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"
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 void qemu_mutex_init(QemuMutex *mutex)
43 err = pthread_mutex_init(&mutex->lock, NULL);
45 error_exit(err, __func__);
48 void qemu_mutex_destroy(QemuMutex *mutex)
52 err = pthread_mutex_destroy(&mutex->lock);
54 error_exit(err, __func__);
57 void qemu_mutex_lock(QemuMutex *mutex)
61 err = pthread_mutex_lock(&mutex->lock);
63 error_exit(err, __func__);
65 trace_qemu_mutex_locked(mutex);
68 int qemu_mutex_trylock(QemuMutex *mutex)
72 err = pthread_mutex_trylock(&mutex->lock);
74 trace_qemu_mutex_locked(mutex);
78 error_exit(err, __func__);
83 void qemu_mutex_unlock(QemuMutex *mutex)
87 trace_qemu_mutex_unlocked(mutex);
88 err = pthread_mutex_unlock(&mutex->lock);
90 error_exit(err, __func__);
93 void qemu_rec_mutex_init(QemuRecMutex *mutex)
96 pthread_mutexattr_t attr;
98 pthread_mutexattr_init(&attr);
99 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
100 err = pthread_mutex_init(&mutex->lock, &attr);
101 pthread_mutexattr_destroy(&attr);
103 error_exit(err, __func__);
107 void qemu_cond_init(QemuCond *cond)
111 err = pthread_cond_init(&cond->cond, NULL);
113 error_exit(err, __func__);
116 void qemu_cond_destroy(QemuCond *cond)
120 err = pthread_cond_destroy(&cond->cond);
122 error_exit(err, __func__);
125 void qemu_cond_signal(QemuCond *cond)
129 err = pthread_cond_signal(&cond->cond);
131 error_exit(err, __func__);
134 void qemu_cond_broadcast(QemuCond *cond)
138 err = pthread_cond_broadcast(&cond->cond);
140 error_exit(err, __func__);
143 void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
147 trace_qemu_mutex_unlocked(mutex);
148 err = pthread_cond_wait(&cond->cond, &mutex->lock);
149 trace_qemu_mutex_locked(mutex);
151 error_exit(err, __func__);
154 void qemu_sem_init(QemuSemaphore *sem, int init)
158 #if defined(__APPLE__) || defined(__NetBSD__)
159 rc = pthread_mutex_init(&sem->lock, NULL);
161 error_exit(rc, __func__);
163 rc = pthread_cond_init(&sem->cond, NULL);
165 error_exit(rc, __func__);
168 error_exit(EINVAL, __func__);
172 rc = sem_init(&sem->sem, 0, init);
174 error_exit(errno, __func__);
179 void qemu_sem_destroy(QemuSemaphore *sem)
183 #if defined(__APPLE__) || defined(__NetBSD__)
184 rc = pthread_cond_destroy(&sem->cond);
186 error_exit(rc, __func__);
188 rc = pthread_mutex_destroy(&sem->lock);
190 error_exit(rc, __func__);
193 rc = sem_destroy(&sem->sem);
195 error_exit(errno, __func__);
200 void qemu_sem_post(QemuSemaphore *sem)
204 #if defined(__APPLE__) || defined(__NetBSD__)
205 pthread_mutex_lock(&sem->lock);
206 if (sem->count == UINT_MAX) {
210 rc = pthread_cond_signal(&sem->cond);
212 pthread_mutex_unlock(&sem->lock);
214 error_exit(rc, __func__);
217 rc = sem_post(&sem->sem);
219 error_exit(errno, __func__);
224 static void compute_abs_deadline(struct timespec *ts, int ms)
227 gettimeofday(&tv, NULL);
228 ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
229 ts->tv_sec = tv.tv_sec + ms / 1000;
230 if (ts->tv_nsec >= 1000000000) {
232 ts->tv_nsec -= 1000000000;
236 int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
241 #if defined(__APPLE__) || defined(__NetBSD__)
243 compute_abs_deadline(&ts, ms);
244 pthread_mutex_lock(&sem->lock);
245 while (sem->count == 0) {
246 rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts);
247 if (rc == ETIMEDOUT) {
251 error_exit(rc, __func__);
254 if (rc != ETIMEDOUT) {
257 pthread_mutex_unlock(&sem->lock);
258 return (rc == ETIMEDOUT ? -1 : 0);
261 /* This is cheaper than sem_timedwait. */
263 rc = sem_trywait(&sem->sem);
264 } while (rc == -1 && errno == EINTR);
265 if (rc == -1 && errno == EAGAIN) {
269 compute_abs_deadline(&ts, ms);
271 rc = sem_timedwait(&sem->sem, &ts);
272 } while (rc == -1 && errno == EINTR);
273 if (rc == -1 && errno == ETIMEDOUT) {
278 error_exit(errno, __func__);
284 void qemu_sem_wait(QemuSemaphore *sem)
288 #if defined(__APPLE__) || defined(__NetBSD__)
289 pthread_mutex_lock(&sem->lock);
290 while (sem->count == 0) {
291 rc = pthread_cond_wait(&sem->cond, &sem->lock);
293 error_exit(rc, __func__);
297 pthread_mutex_unlock(&sem->lock);
300 rc = sem_wait(&sem->sem);
301 } while (rc == -1 && errno == EINTR);
303 error_exit(errno, __func__);
309 #include "qemu/futex.h"
311 static inline void qemu_futex_wake(QemuEvent *ev, int n)
313 pthread_mutex_lock(&ev->lock);
315 pthread_cond_signal(&ev->cond);
317 pthread_cond_broadcast(&ev->cond);
319 pthread_mutex_unlock(&ev->lock);
322 static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
324 pthread_mutex_lock(&ev->lock);
325 if (ev->value == val) {
326 pthread_cond_wait(&ev->cond, &ev->lock);
328 pthread_mutex_unlock(&ev->lock);
332 /* Valid transitions:
333 * - free->set, when setting the event
334 * - busy->set, when setting the event, followed by qemu_futex_wake
335 * - set->free, when resetting the event
336 * - free->busy, when waiting
338 * set->busy does not happen (it can be observed from the outside but
339 * it really is set->free->busy).
341 * busy->free provably cannot happen; to enforce it, the set->free transition
342 * is done with an OR, which becomes a no-op if the event has concurrently
343 * transitioned to free or busy.
350 void qemu_event_init(QemuEvent *ev, bool init)
353 pthread_mutex_init(&ev->lock, NULL);
354 pthread_cond_init(&ev->cond, NULL);
357 ev->value = (init ? EV_SET : EV_FREE);
360 void qemu_event_destroy(QemuEvent *ev)
363 pthread_mutex_destroy(&ev->lock);
364 pthread_cond_destroy(&ev->cond);
368 void qemu_event_set(QemuEvent *ev)
370 /* qemu_event_set has release semantics, but because it *loads*
371 * ev->value we need a full memory barrier here.
374 if (atomic_read(&ev->value) != EV_SET) {
375 if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
376 /* There were waiters, wake them up. */
377 qemu_futex_wake(ev, INT_MAX);
382 void qemu_event_reset(QemuEvent *ev)
386 value = atomic_read(&ev->value);
388 if (value == EV_SET) {
390 * If there was a concurrent reset (or even reset+wait),
391 * do nothing. Otherwise change EV_SET->EV_FREE.
393 atomic_or(&ev->value, EV_FREE);
397 void qemu_event_wait(QemuEvent *ev)
401 value = atomic_read(&ev->value);
403 if (value != EV_SET) {
404 if (value == EV_FREE) {
406 * Leave the event reset and tell qemu_event_set that there
407 * are waiters. No need to retry, because there cannot be
408 * a concurrent busy->free transition. After the CAS, the
409 * event will be either set or busy.
411 if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
415 qemu_futex_wait(ev, EV_BUSY);
419 static pthread_key_t exit_key;
421 union NotifierThreadData {
425 QEMU_BUILD_BUG_ON(sizeof(union NotifierThreadData) != sizeof(void *));
427 void qemu_thread_atexit_add(Notifier *notifier)
429 union NotifierThreadData ntd;
430 ntd.ptr = pthread_getspecific(exit_key);
431 notifier_list_add(&ntd.list, notifier);
432 pthread_setspecific(exit_key, ntd.ptr);
435 void qemu_thread_atexit_remove(Notifier *notifier)
437 union NotifierThreadData ntd;
438 ntd.ptr = pthread_getspecific(exit_key);
439 notifier_remove(notifier);
440 pthread_setspecific(exit_key, ntd.ptr);
443 static void qemu_thread_atexit_run(void *arg)
445 union NotifierThreadData ntd = { .ptr = arg };
446 notifier_list_notify(&ntd.list, NULL);
449 static void __attribute__((constructor)) qemu_thread_atexit_init(void)
451 pthread_key_create(&exit_key, qemu_thread_atexit_run);
455 /* Attempt to set the threads name; note that this is for debug, so
456 * we're not going to fail if we can't set it.
458 static void qemu_thread_set_name(QemuThread *thread, const char *name)
460 #ifdef CONFIG_PTHREAD_SETNAME_NP
461 pthread_setname_np(thread->thread, name);
465 void qemu_thread_create(QemuThread *thread, const char *name,
466 void *(*start_routine)(void*),
469 sigset_t set, oldset;
473 err = pthread_attr_init(&attr);
475 error_exit(err, __func__);
478 /* Leave signal handling to the iothread. */
480 pthread_sigmask(SIG_SETMASK, &set, &oldset);
481 err = pthread_create(&thread->thread, &attr, start_routine, arg);
483 error_exit(err, __func__);
486 qemu_thread_set_name(thread, name);
489 if (mode == QEMU_THREAD_DETACHED) {
490 err = pthread_detach(thread->thread);
492 error_exit(err, __func__);
495 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
497 pthread_attr_destroy(&attr);
500 void qemu_thread_get_self(QemuThread *thread)
502 thread->thread = pthread_self();
505 bool qemu_thread_is_self(QemuThread *thread)
507 return pthread_equal(pthread_self(), thread->thread);
510 void qemu_thread_exit(void *retval)
512 pthread_exit(retval);
515 void *qemu_thread_join(QemuThread *thread)
520 err = pthread_join(thread->thread, &ret);
522 error_exit(err, __func__);