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.
24 #include <sys/syscall.h>
25 #include <linux/futex.h>
27 #include "qemu/thread.h"
28 #include "qemu/atomic.h"
30 static bool name_threads
;
32 void qemu_thread_naming(bool enable
)
34 name_threads
= enable
;
36 #ifndef CONFIG_THREAD_SETNAME_BYTHREAD
37 /* This is a debugging option, not fatal */
39 fprintf(stderr
, "qemu: thread naming not supported on this host\n");
44 static void error_exit(int err
, const char *msg
)
46 fprintf(stderr
, "qemu: %s: %s\n", msg
, strerror(err
));
50 void qemu_mutex_init(QemuMutex
*mutex
)
53 pthread_mutexattr_t mutexattr
;
55 pthread_mutexattr_init(&mutexattr
);
56 pthread_mutexattr_settype(&mutexattr
, PTHREAD_MUTEX_ERRORCHECK
);
57 err
= pthread_mutex_init(&mutex
->lock
, &mutexattr
);
58 pthread_mutexattr_destroy(&mutexattr
);
60 error_exit(err
, __func__
);
63 void qemu_mutex_destroy(QemuMutex
*mutex
)
67 err
= pthread_mutex_destroy(&mutex
->lock
);
69 error_exit(err
, __func__
);
72 void qemu_mutex_lock(QemuMutex
*mutex
)
76 err
= pthread_mutex_lock(&mutex
->lock
);
78 error_exit(err
, __func__
);
81 int qemu_mutex_trylock(QemuMutex
*mutex
)
83 return pthread_mutex_trylock(&mutex
->lock
);
86 void qemu_mutex_unlock(QemuMutex
*mutex
)
90 err
= pthread_mutex_unlock(&mutex
->lock
);
92 error_exit(err
, __func__
);
95 void qemu_cond_init(QemuCond
*cond
)
99 err
= pthread_cond_init(&cond
->cond
, NULL
);
101 error_exit(err
, __func__
);
104 void qemu_cond_destroy(QemuCond
*cond
)
108 err
= pthread_cond_destroy(&cond
->cond
);
110 error_exit(err
, __func__
);
113 void qemu_cond_signal(QemuCond
*cond
)
117 err
= pthread_cond_signal(&cond
->cond
);
119 error_exit(err
, __func__
);
122 void qemu_cond_broadcast(QemuCond
*cond
)
126 err
= pthread_cond_broadcast(&cond
->cond
);
128 error_exit(err
, __func__
);
131 void qemu_cond_wait(QemuCond
*cond
, QemuMutex
*mutex
)
135 err
= pthread_cond_wait(&cond
->cond
, &mutex
->lock
);
137 error_exit(err
, __func__
);
140 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
144 #if defined(__APPLE__) || defined(__NetBSD__)
145 rc
= pthread_mutex_init(&sem
->lock
, NULL
);
147 error_exit(rc
, __func__
);
149 rc
= pthread_cond_init(&sem
->cond
, NULL
);
151 error_exit(rc
, __func__
);
154 error_exit(EINVAL
, __func__
);
158 rc
= sem_init(&sem
->sem
, 0, init
);
160 error_exit(errno
, __func__
);
165 void qemu_sem_destroy(QemuSemaphore
*sem
)
169 #if defined(__APPLE__) || defined(__NetBSD__)
170 rc
= pthread_cond_destroy(&sem
->cond
);
172 error_exit(rc
, __func__
);
174 rc
= pthread_mutex_destroy(&sem
->lock
);
176 error_exit(rc
, __func__
);
179 rc
= sem_destroy(&sem
->sem
);
181 error_exit(errno
, __func__
);
186 void qemu_sem_post(QemuSemaphore
*sem
)
190 #if defined(__APPLE__) || defined(__NetBSD__)
191 pthread_mutex_lock(&sem
->lock
);
192 if (sem
->count
== UINT_MAX
) {
196 rc
= pthread_cond_signal(&sem
->cond
);
198 pthread_mutex_unlock(&sem
->lock
);
200 error_exit(rc
, __func__
);
203 rc
= sem_post(&sem
->sem
);
205 error_exit(errno
, __func__
);
210 static void compute_abs_deadline(struct timespec
*ts
, int ms
)
213 gettimeofday(&tv
, NULL
);
214 ts
->tv_nsec
= tv
.tv_usec
* 1000 + (ms
% 1000) * 1000000;
215 ts
->tv_sec
= tv
.tv_sec
+ ms
/ 1000;
216 if (ts
->tv_nsec
>= 1000000000) {
218 ts
->tv_nsec
-= 1000000000;
222 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
227 #if defined(__APPLE__) || defined(__NetBSD__)
229 compute_abs_deadline(&ts
, ms
);
230 pthread_mutex_lock(&sem
->lock
);
231 while (sem
->count
== 0) {
232 rc
= pthread_cond_timedwait(&sem
->cond
, &sem
->lock
, &ts
);
233 if (rc
== ETIMEDOUT
) {
237 error_exit(rc
, __func__
);
240 if (rc
!= ETIMEDOUT
) {
243 pthread_mutex_unlock(&sem
->lock
);
244 return (rc
== ETIMEDOUT
? -1 : 0);
247 /* This is cheaper than sem_timedwait. */
249 rc
= sem_trywait(&sem
->sem
);
250 } while (rc
== -1 && errno
== EINTR
);
251 if (rc
== -1 && errno
== EAGAIN
) {
255 compute_abs_deadline(&ts
, ms
);
257 rc
= sem_timedwait(&sem
->sem
, &ts
);
258 } while (rc
== -1 && errno
== EINTR
);
259 if (rc
== -1 && errno
== ETIMEDOUT
) {
264 error_exit(errno
, __func__
);
270 void qemu_sem_wait(QemuSemaphore
*sem
)
274 #if defined(__APPLE__) || defined(__NetBSD__)
275 pthread_mutex_lock(&sem
->lock
);
276 while (sem
->count
== 0) {
277 rc
= pthread_cond_wait(&sem
->cond
, &sem
->lock
);
279 error_exit(rc
, __func__
);
283 pthread_mutex_unlock(&sem
->lock
);
286 rc
= sem_wait(&sem
->sem
);
287 } while (rc
== -1 && errno
== EINTR
);
289 error_exit(errno
, __func__
);
295 #define futex(...) syscall(__NR_futex, __VA_ARGS__)
297 static inline void futex_wake(QemuEvent
*ev
, int n
)
299 futex(ev
, FUTEX_WAKE
, n
, NULL
, NULL
, 0);
302 static inline void futex_wait(QemuEvent
*ev
, unsigned val
)
304 futex(ev
, FUTEX_WAIT
, (int) val
, NULL
, NULL
, 0);
307 static inline void futex_wake(QemuEvent
*ev
, int n
)
309 pthread_mutex_lock(&ev
->lock
);
311 pthread_cond_signal(&ev
->cond
);
313 pthread_cond_broadcast(&ev
->cond
);
315 pthread_mutex_unlock(&ev
->lock
);
318 static inline void futex_wait(QemuEvent
*ev
, unsigned val
)
320 pthread_mutex_lock(&ev
->lock
);
321 if (ev
->value
== val
) {
322 pthread_cond_wait(&ev
->cond
, &ev
->lock
);
324 pthread_mutex_unlock(&ev
->lock
);
328 /* Valid transitions:
329 * - free->set, when setting the event
330 * - busy->set, when setting the event, followed by futex_wake
331 * - set->free, when resetting the event
332 * - free->busy, when waiting
334 * set->busy does not happen (it can be observed from the outside but
335 * it really is set->free->busy).
337 * busy->free provably cannot happen; to enforce it, the set->free transition
338 * is done with an OR, which becomes a no-op if the event has concurrently
339 * transitioned to free or busy.
346 void qemu_event_init(QemuEvent
*ev
, bool init
)
349 pthread_mutex_init(&ev
->lock
, NULL
);
350 pthread_cond_init(&ev
->cond
, NULL
);
353 ev
->value
= (init
? EV_SET
: EV_FREE
);
356 void qemu_event_destroy(QemuEvent
*ev
)
359 pthread_mutex_destroy(&ev
->lock
);
360 pthread_cond_destroy(&ev
->cond
);
364 void qemu_event_set(QemuEvent
*ev
)
366 if (atomic_mb_read(&ev
->value
) != EV_SET
) {
367 if (atomic_xchg(&ev
->value
, EV_SET
) == EV_BUSY
) {
368 /* There were waiters, wake them up. */
369 futex_wake(ev
, INT_MAX
);
374 void qemu_event_reset(QemuEvent
*ev
)
376 if (atomic_mb_read(&ev
->value
) == EV_SET
) {
378 * If there was a concurrent reset (or even reset+wait),
379 * do nothing. Otherwise change EV_SET->EV_FREE.
381 atomic_or(&ev
->value
, EV_FREE
);
385 void qemu_event_wait(QemuEvent
*ev
)
389 value
= atomic_mb_read(&ev
->value
);
390 if (value
!= EV_SET
) {
391 if (value
== EV_FREE
) {
393 * Leave the event reset and tell qemu_event_set that there
394 * are waiters. No need to retry, because there cannot be
395 * a concurent busy->free transition. After the CAS, the
396 * event will be either set or busy.
398 if (atomic_cmpxchg(&ev
->value
, EV_FREE
, EV_BUSY
) == EV_SET
) {
402 futex_wait(ev
, EV_BUSY
);
406 /* Attempt to set the threads name; note that this is for debug, so
407 * we're not going to fail if we can't set it.
409 static void qemu_thread_set_name(QemuThread
*thread
, const char *name
)
411 #ifdef CONFIG_PTHREAD_SETNAME_NP
412 pthread_setname_np(thread
->thread
, name
);
416 void qemu_thread_create(QemuThread
*thread
, const char *name
,
417 void *(*start_routine
)(void*),
420 sigset_t set
, oldset
;
424 err
= pthread_attr_init(&attr
);
426 error_exit(err
, __func__
);
428 if (mode
== QEMU_THREAD_DETACHED
) {
429 err
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
431 error_exit(err
, __func__
);
435 /* Leave signal handling to the iothread. */
437 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
438 err
= pthread_create(&thread
->thread
, &attr
, start_routine
, arg
);
440 error_exit(err
, __func__
);
443 qemu_thread_set_name(thread
, name
);
446 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
448 pthread_attr_destroy(&attr
);
451 void qemu_thread_get_self(QemuThread
*thread
)
453 thread
->thread
= pthread_self();
456 bool qemu_thread_is_self(QemuThread
*thread
)
458 return pthread_equal(pthread_self(), thread
->thread
);
461 void qemu_thread_exit(void *retval
)
463 pthread_exit(retval
);
466 void *qemu_thread_join(QemuThread
*thread
)
471 err
= pthread_join(thread
->thread
, &ret
);
473 error_exit(err
, __func__
);