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"
18 static bool name_threads
;
20 void qemu_thread_naming(bool enable
)
22 name_threads
= enable
;
24 #ifndef CONFIG_THREAD_SETNAME_BYTHREAD
25 /* This is a debugging option, not fatal */
27 fprintf(stderr
, "qemu: thread naming not supported on this host\n");
32 static void error_exit(int err
, const char *msg
)
34 fprintf(stderr
, "qemu: %s: %s\n", msg
, strerror(err
));
38 void qemu_mutex_init(QemuMutex
*mutex
)
42 err
= pthread_mutex_init(&mutex
->lock
, NULL
);
44 error_exit(err
, __func__
);
47 void qemu_mutex_destroy(QemuMutex
*mutex
)
51 err
= pthread_mutex_destroy(&mutex
->lock
);
53 error_exit(err
, __func__
);
56 void qemu_mutex_lock(QemuMutex
*mutex
)
60 err
= pthread_mutex_lock(&mutex
->lock
);
62 error_exit(err
, __func__
);
65 int qemu_mutex_trylock(QemuMutex
*mutex
)
67 return pthread_mutex_trylock(&mutex
->lock
);
70 void qemu_mutex_unlock(QemuMutex
*mutex
)
74 err
= pthread_mutex_unlock(&mutex
->lock
);
76 error_exit(err
, __func__
);
79 void qemu_rec_mutex_init(QemuRecMutex
*mutex
)
82 pthread_mutexattr_t attr
;
84 pthread_mutexattr_init(&attr
);
85 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
86 err
= pthread_mutex_init(&mutex
->lock
, &attr
);
87 pthread_mutexattr_destroy(&attr
);
89 error_exit(err
, __func__
);
93 void qemu_cond_init(QemuCond
*cond
)
97 err
= pthread_cond_init(&cond
->cond
, NULL
);
99 error_exit(err
, __func__
);
102 void qemu_cond_destroy(QemuCond
*cond
)
106 err
= pthread_cond_destroy(&cond
->cond
);
108 error_exit(err
, __func__
);
111 void qemu_cond_signal(QemuCond
*cond
)
115 err
= pthread_cond_signal(&cond
->cond
);
117 error_exit(err
, __func__
);
120 void qemu_cond_broadcast(QemuCond
*cond
)
124 err
= pthread_cond_broadcast(&cond
->cond
);
126 error_exit(err
, __func__
);
129 void qemu_cond_wait(QemuCond
*cond
, QemuMutex
*mutex
)
133 err
= pthread_cond_wait(&cond
->cond
, &mutex
->lock
);
135 error_exit(err
, __func__
);
138 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
142 #if defined(__APPLE__) || defined(__NetBSD__)
143 rc
= pthread_mutex_init(&sem
->lock
, NULL
);
145 error_exit(rc
, __func__
);
147 rc
= pthread_cond_init(&sem
->cond
, NULL
);
149 error_exit(rc
, __func__
);
152 error_exit(EINVAL
, __func__
);
156 rc
= sem_init(&sem
->sem
, 0, init
);
158 error_exit(errno
, __func__
);
163 void qemu_sem_destroy(QemuSemaphore
*sem
)
167 #if defined(__APPLE__) || defined(__NetBSD__)
168 rc
= pthread_cond_destroy(&sem
->cond
);
170 error_exit(rc
, __func__
);
172 rc
= pthread_mutex_destroy(&sem
->lock
);
174 error_exit(rc
, __func__
);
177 rc
= sem_destroy(&sem
->sem
);
179 error_exit(errno
, __func__
);
184 void qemu_sem_post(QemuSemaphore
*sem
)
188 #if defined(__APPLE__) || defined(__NetBSD__)
189 pthread_mutex_lock(&sem
->lock
);
190 if (sem
->count
== UINT_MAX
) {
194 rc
= pthread_cond_signal(&sem
->cond
);
196 pthread_mutex_unlock(&sem
->lock
);
198 error_exit(rc
, __func__
);
201 rc
= sem_post(&sem
->sem
);
203 error_exit(errno
, __func__
);
208 static void compute_abs_deadline(struct timespec
*ts
, int ms
)
211 gettimeofday(&tv
, NULL
);
212 ts
->tv_nsec
= tv
.tv_usec
* 1000 + (ms
% 1000) * 1000000;
213 ts
->tv_sec
= tv
.tv_sec
+ ms
/ 1000;
214 if (ts
->tv_nsec
>= 1000000000) {
216 ts
->tv_nsec
-= 1000000000;
220 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
225 #if defined(__APPLE__) || defined(__NetBSD__)
227 compute_abs_deadline(&ts
, ms
);
228 pthread_mutex_lock(&sem
->lock
);
229 while (sem
->count
== 0) {
230 rc
= pthread_cond_timedwait(&sem
->cond
, &sem
->lock
, &ts
);
231 if (rc
== ETIMEDOUT
) {
235 error_exit(rc
, __func__
);
238 if (rc
!= ETIMEDOUT
) {
241 pthread_mutex_unlock(&sem
->lock
);
242 return (rc
== ETIMEDOUT
? -1 : 0);
245 /* This is cheaper than sem_timedwait. */
247 rc
= sem_trywait(&sem
->sem
);
248 } while (rc
== -1 && errno
== EINTR
);
249 if (rc
== -1 && errno
== EAGAIN
) {
253 compute_abs_deadline(&ts
, ms
);
255 rc
= sem_timedwait(&sem
->sem
, &ts
);
256 } while (rc
== -1 && errno
== EINTR
);
257 if (rc
== -1 && errno
== ETIMEDOUT
) {
262 error_exit(errno
, __func__
);
268 void qemu_sem_wait(QemuSemaphore
*sem
)
272 #if defined(__APPLE__) || defined(__NetBSD__)
273 pthread_mutex_lock(&sem
->lock
);
274 while (sem
->count
== 0) {
275 rc
= pthread_cond_wait(&sem
->cond
, &sem
->lock
);
277 error_exit(rc
, __func__
);
281 pthread_mutex_unlock(&sem
->lock
);
284 rc
= sem_wait(&sem
->sem
);
285 } while (rc
== -1 && errno
== EINTR
);
287 error_exit(errno
, __func__
);
293 #include "qemu/futex.h"
295 static inline void qemu_futex_wake(QemuEvent
*ev
, int n
)
297 pthread_mutex_lock(&ev
->lock
);
299 pthread_cond_signal(&ev
->cond
);
301 pthread_cond_broadcast(&ev
->cond
);
303 pthread_mutex_unlock(&ev
->lock
);
306 static inline void qemu_futex_wait(QemuEvent
*ev
, unsigned val
)
308 pthread_mutex_lock(&ev
->lock
);
309 if (ev
->value
== val
) {
310 pthread_cond_wait(&ev
->cond
, &ev
->lock
);
312 pthread_mutex_unlock(&ev
->lock
);
316 /* Valid transitions:
317 * - free->set, when setting the event
318 * - busy->set, when setting the event, followed by qemu_futex_wake
319 * - set->free, when resetting the event
320 * - free->busy, when waiting
322 * set->busy does not happen (it can be observed from the outside but
323 * it really is set->free->busy).
325 * busy->free provably cannot happen; to enforce it, the set->free transition
326 * is done with an OR, which becomes a no-op if the event has concurrently
327 * transitioned to free or busy.
334 void qemu_event_init(QemuEvent
*ev
, bool init
)
337 pthread_mutex_init(&ev
->lock
, NULL
);
338 pthread_cond_init(&ev
->cond
, NULL
);
341 ev
->value
= (init
? EV_SET
: EV_FREE
);
344 void qemu_event_destroy(QemuEvent
*ev
)
347 pthread_mutex_destroy(&ev
->lock
);
348 pthread_cond_destroy(&ev
->cond
);
352 void qemu_event_set(QemuEvent
*ev
)
354 /* qemu_event_set has release semantics, but because it *loads*
355 * ev->value we need a full memory barrier here.
358 if (atomic_read(&ev
->value
) != EV_SET
) {
359 if (atomic_xchg(&ev
->value
, EV_SET
) == EV_BUSY
) {
360 /* There were waiters, wake them up. */
361 qemu_futex_wake(ev
, INT_MAX
);
366 void qemu_event_reset(QemuEvent
*ev
)
370 value
= atomic_read(&ev
->value
);
372 if (value
== EV_SET
) {
374 * If there was a concurrent reset (or even reset+wait),
375 * do nothing. Otherwise change EV_SET->EV_FREE.
377 atomic_or(&ev
->value
, EV_FREE
);
381 void qemu_event_wait(QemuEvent
*ev
)
385 value
= atomic_read(&ev
->value
);
387 if (value
!= EV_SET
) {
388 if (value
== EV_FREE
) {
390 * Leave the event reset and tell qemu_event_set that there
391 * are waiters. No need to retry, because there cannot be
392 * a concurrent busy->free transition. After the CAS, the
393 * event will be either set or busy.
395 if (atomic_cmpxchg(&ev
->value
, EV_FREE
, EV_BUSY
) == EV_SET
) {
399 qemu_futex_wait(ev
, EV_BUSY
);
403 static pthread_key_t exit_key
;
405 union NotifierThreadData
{
409 QEMU_BUILD_BUG_ON(sizeof(union NotifierThreadData
) != sizeof(void *));
411 void qemu_thread_atexit_add(Notifier
*notifier
)
413 union NotifierThreadData ntd
;
414 ntd
.ptr
= pthread_getspecific(exit_key
);
415 notifier_list_add(&ntd
.list
, notifier
);
416 pthread_setspecific(exit_key
, ntd
.ptr
);
419 void qemu_thread_atexit_remove(Notifier
*notifier
)
421 union NotifierThreadData ntd
;
422 ntd
.ptr
= pthread_getspecific(exit_key
);
423 notifier_remove(notifier
);
424 pthread_setspecific(exit_key
, ntd
.ptr
);
427 static void qemu_thread_atexit_run(void *arg
)
429 union NotifierThreadData ntd
= { .ptr
= arg
};
430 notifier_list_notify(&ntd
.list
, NULL
);
433 static void __attribute__((constructor
)) qemu_thread_atexit_init(void)
435 pthread_key_create(&exit_key
, qemu_thread_atexit_run
);
439 /* Attempt to set the threads name; note that this is for debug, so
440 * we're not going to fail if we can't set it.
442 static void qemu_thread_set_name(QemuThread
*thread
, const char *name
)
444 #ifdef CONFIG_PTHREAD_SETNAME_NP
445 pthread_setname_np(thread
->thread
, name
);
449 void qemu_thread_create(QemuThread
*thread
, const char *name
,
450 void *(*start_routine
)(void*),
453 sigset_t set
, oldset
;
457 err
= pthread_attr_init(&attr
);
459 error_exit(err
, __func__
);
462 /* Leave signal handling to the iothread. */
464 pthread_sigmask(SIG_SETMASK
, &set
, &oldset
);
465 err
= pthread_create(&thread
->thread
, &attr
, start_routine
, arg
);
467 error_exit(err
, __func__
);
470 qemu_thread_set_name(thread
, name
);
473 if (mode
== QEMU_THREAD_DETACHED
) {
474 err
= pthread_detach(thread
->thread
);
476 error_exit(err
, __func__
);
479 pthread_sigmask(SIG_SETMASK
, &oldset
, NULL
);
481 pthread_attr_destroy(&attr
);
484 void qemu_thread_get_self(QemuThread
*thread
)
486 thread
->thread
= pthread_self();
489 bool qemu_thread_is_self(QemuThread
*thread
)
491 return pthread_equal(pthread_self(), thread
->thread
);
494 void qemu_thread_exit(void *retval
)
496 pthread_exit(retval
);
499 void *qemu_thread_join(QemuThread
*thread
)
504 err
= pthread_join(thread
->thread
, &ret
);
506 error_exit(err
, __func__
);