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 void qemu_mutex_init(QemuMutex *mutex)
43 err = pthread_mutex_init(&mutex->lock, NULL);
45 error_exit(err, __func__);
46 qemu_mutex_post_init(mutex);
49 void qemu_mutex_destroy(QemuMutex *mutex)
53 assert(mutex->initialized);
54 mutex->initialized = false;
55 err = pthread_mutex_destroy(&mutex->lock);
57 error_exit(err, __func__);
60 void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
64 assert(mutex->initialized);
65 qemu_mutex_pre_lock(mutex, file, line);
66 err = pthread_mutex_lock(&mutex->lock);
68 error_exit(err, __func__);
69 qemu_mutex_post_lock(mutex, file, line);
72 int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
76 assert(mutex->initialized);
77 err = pthread_mutex_trylock(&mutex->lock);
79 qemu_mutex_post_lock(mutex, file, line);
83 error_exit(err, __func__);
88 void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
92 assert(mutex->initialized);
93 qemu_mutex_pre_unlock(mutex, file, line);
94 err = pthread_mutex_unlock(&mutex->lock);
96 error_exit(err, __func__);
99 void qemu_rec_mutex_init(QemuRecMutex *mutex)
102 pthread_mutexattr_t attr;
104 pthread_mutexattr_init(&attr);
105 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
106 err = pthread_mutex_init(&mutex->lock, &attr);
107 pthread_mutexattr_destroy(&attr);
109 error_exit(err, __func__);
111 mutex->initialized = true;
114 void qemu_cond_init(QemuCond *cond)
118 err = pthread_cond_init(&cond->cond, NULL);
120 error_exit(err, __func__);
121 cond->initialized = true;
124 void qemu_cond_destroy(QemuCond *cond)
128 assert(cond->initialized);
129 cond->initialized = false;
130 err = pthread_cond_destroy(&cond->cond);
132 error_exit(err, __func__);
135 void qemu_cond_signal(QemuCond *cond)
139 assert(cond->initialized);
140 err = pthread_cond_signal(&cond->cond);
142 error_exit(err, __func__);
145 void qemu_cond_broadcast(QemuCond *cond)
149 assert(cond->initialized);
150 err = pthread_cond_broadcast(&cond->cond);
152 error_exit(err, __func__);
155 void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
159 assert(cond->initialized);
160 qemu_mutex_pre_unlock(mutex, file, line);
161 err = pthread_cond_wait(&cond->cond, &mutex->lock);
162 qemu_mutex_post_lock(mutex, file, line);
164 error_exit(err, __func__);
167 void qemu_sem_init(QemuSemaphore *sem, int init)
171 #ifndef CONFIG_SEM_TIMEDWAIT
172 rc = pthread_mutex_init(&sem->lock, NULL);
174 error_exit(rc, __func__);
176 rc = pthread_cond_init(&sem->cond, NULL);
178 error_exit(rc, __func__);
181 error_exit(EINVAL, __func__);
185 rc = sem_init(&sem->sem, 0, init);
187 error_exit(errno, __func__);
190 sem->initialized = true;
193 void qemu_sem_destroy(QemuSemaphore *sem)
197 assert(sem->initialized);
198 sem->initialized = false;
199 #ifndef CONFIG_SEM_TIMEDWAIT
200 rc = pthread_cond_destroy(&sem->cond);
202 error_exit(rc, __func__);
204 rc = pthread_mutex_destroy(&sem->lock);
206 error_exit(rc, __func__);
209 rc = sem_destroy(&sem->sem);
211 error_exit(errno, __func__);
216 void qemu_sem_post(QemuSemaphore *sem)
220 assert(sem->initialized);
221 #ifndef CONFIG_SEM_TIMEDWAIT
222 pthread_mutex_lock(&sem->lock);
223 if (sem->count == UINT_MAX) {
227 rc = pthread_cond_signal(&sem->cond);
229 pthread_mutex_unlock(&sem->lock);
231 error_exit(rc, __func__);
234 rc = sem_post(&sem->sem);
236 error_exit(errno, __func__);
241 static void compute_abs_deadline(struct timespec *ts, int ms)
244 gettimeofday(&tv, NULL);
245 ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
246 ts->tv_sec = tv.tv_sec + ms / 1000;
247 if (ts->tv_nsec >= 1000000000) {
249 ts->tv_nsec -= 1000000000;
253 int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
258 assert(sem->initialized);
259 #ifndef CONFIG_SEM_TIMEDWAIT
261 compute_abs_deadline(&ts, ms);
262 pthread_mutex_lock(&sem->lock);
263 while (sem->count == 0) {
264 rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts);
265 if (rc == ETIMEDOUT) {
269 error_exit(rc, __func__);
272 if (rc != ETIMEDOUT) {
275 pthread_mutex_unlock(&sem->lock);
276 return (rc == ETIMEDOUT ? -1 : 0);
279 /* This is cheaper than sem_timedwait. */
281 rc = sem_trywait(&sem->sem);
282 } while (rc == -1 && errno == EINTR);
283 if (rc == -1 && errno == EAGAIN) {
287 compute_abs_deadline(&ts, ms);
289 rc = sem_timedwait(&sem->sem, &ts);
290 } while (rc == -1 && errno == EINTR);
291 if (rc == -1 && errno == ETIMEDOUT) {
296 error_exit(errno, __func__);
302 void qemu_sem_wait(QemuSemaphore *sem)
306 assert(sem->initialized);
307 #ifndef CONFIG_SEM_TIMEDWAIT
308 pthread_mutex_lock(&sem->lock);
309 while (sem->count == 0) {
310 rc = pthread_cond_wait(&sem->cond, &sem->lock);
312 error_exit(rc, __func__);
316 pthread_mutex_unlock(&sem->lock);
319 rc = sem_wait(&sem->sem);
320 } while (rc == -1 && errno == EINTR);
322 error_exit(errno, __func__);
328 #include "qemu/futex.h"
330 static inline void qemu_futex_wake(QemuEvent *ev, int n)
332 assert(ev->initialized);
333 pthread_mutex_lock(&ev->lock);
335 pthread_cond_signal(&ev->cond);
337 pthread_cond_broadcast(&ev->cond);
339 pthread_mutex_unlock(&ev->lock);
342 static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
344 assert(ev->initialized);
345 pthread_mutex_lock(&ev->lock);
346 if (ev->value == val) {
347 pthread_cond_wait(&ev->cond, &ev->lock);
349 pthread_mutex_unlock(&ev->lock);
353 /* Valid transitions:
354 * - free->set, when setting the event
355 * - busy->set, when setting the event, followed by qemu_futex_wake
356 * - set->free, when resetting the event
357 * - free->busy, when waiting
359 * set->busy does not happen (it can be observed from the outside but
360 * it really is set->free->busy).
362 * busy->free provably cannot happen; to enforce it, the set->free transition
363 * is done with an OR, which becomes a no-op if the event has concurrently
364 * transitioned to free or busy.
371 void qemu_event_init(QemuEvent *ev, bool init)
374 pthread_mutex_init(&ev->lock, NULL);
375 pthread_cond_init(&ev->cond, NULL);
378 ev->value = (init ? EV_SET : EV_FREE);
379 ev->initialized = true;
382 void qemu_event_destroy(QemuEvent *ev)
384 assert(ev->initialized);
385 ev->initialized = false;
387 pthread_mutex_destroy(&ev->lock);
388 pthread_cond_destroy(&ev->cond);
392 void qemu_event_set(QemuEvent *ev)
394 /* qemu_event_set has release semantics, but because it *loads*
395 * ev->value we need a full memory barrier here.
397 assert(ev->initialized);
399 if (atomic_read(&ev->value) != EV_SET) {
400 if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
401 /* There were waiters, wake them up. */
402 qemu_futex_wake(ev, INT_MAX);
407 void qemu_event_reset(QemuEvent *ev)
411 assert(ev->initialized);
412 value = atomic_read(&ev->value);
414 if (value == EV_SET) {
416 * If there was a concurrent reset (or even reset+wait),
417 * do nothing. Otherwise change EV_SET->EV_FREE.
419 atomic_or(&ev->value, EV_FREE);
423 void qemu_event_wait(QemuEvent *ev)
427 assert(ev->initialized);
428 value = atomic_read(&ev->value);
430 if (value != EV_SET) {
431 if (value == EV_FREE) {
433 * Leave the event reset and tell qemu_event_set that there
434 * are waiters. No need to retry, because there cannot be
435 * a concurrent busy->free transition. After the CAS, the
436 * event will be either set or busy.
438 if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
442 qemu_futex_wait(ev, EV_BUSY);
446 static __thread NotifierList thread_exit;
449 * Note that in this implementation you can register a thread-exit
450 * notifier for the main thread, but it will never be called.
451 * This is OK because main thread exit can only happen when the
452 * entire process is exiting, and the API allows notifiers to not
453 * be called on process exit.
455 void qemu_thread_atexit_add(Notifier *notifier)
457 notifier_list_add(&thread_exit, notifier);
460 void qemu_thread_atexit_remove(Notifier *notifier)
462 notifier_remove(notifier);
465 static void qemu_thread_atexit_notify(void *arg)
468 * Called when non-main thread exits (via qemu_thread_exit()
469 * or by returning from its start routine.)
471 notifier_list_notify(&thread_exit, NULL);
475 void *(*start_routine)(void *);
480 static void *qemu_thread_start(void *args)
482 QemuThreadArgs *qemu_thread_args = args;
483 void *(*start_routine)(void *) = qemu_thread_args->start_routine;
484 void *arg = qemu_thread_args->arg;
487 #ifdef CONFIG_THREAD_SETNAME_BYTHREAD
488 /* Attempt to set the threads name; note that this is for debug, so
489 * we're not going to fail if we can't set it.
491 if (name_threads && qemu_thread_args->name) {
492 # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
493 pthread_setname_np(pthread_self(), qemu_thread_args->name);
494 # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
495 pthread_setname_np(qemu_thread_args->name);
499 g_free(qemu_thread_args->name);
500 g_free(qemu_thread_args);
501 pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
502 r = start_routine(arg);
503 pthread_cleanup_pop(1);
507 void qemu_thread_create(QemuThread *thread, const char *name,
508 void *(*start_routine)(void*),
511 sigset_t set, oldset;
514 QemuThreadArgs *qemu_thread_args;
516 err = pthread_attr_init(&attr);
518 error_exit(err, __func__);
521 if (mode == QEMU_THREAD_DETACHED) {
522 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
525 /* Leave signal handling to the iothread. */
527 pthread_sigmask(SIG_SETMASK, &set, &oldset);
529 qemu_thread_args = g_new0(QemuThreadArgs, 1);
530 qemu_thread_args->name = g_strdup(name);
531 qemu_thread_args->start_routine = start_routine;
532 qemu_thread_args->arg = arg;
534 err = pthread_create(&thread->thread, &attr,
535 qemu_thread_start, qemu_thread_args);
538 error_exit(err, __func__);
540 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
542 pthread_attr_destroy(&attr);
545 void qemu_thread_get_self(QemuThread *thread)
547 thread->thread = pthread_self();
550 bool qemu_thread_is_self(QemuThread *thread)
552 return pthread_equal(pthread_self(), thread->thread);
555 void qemu_thread_exit(void *retval)
557 pthread_exit(retval);
560 void *qemu_thread_join(QemuThread *thread)
565 err = pthread_join(thread->thread, &ret);
567 error_exit(err, __func__);