target/hppa: Enable MTTCG
[qemu/ar7.git] / util / qemu-thread-posix.c
blobb789cf32e954cdcc7a290b0cdfa86deb1ee49f66
1 /*
2 * Wrappers around mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2009
6 * Author:
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 "trace.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 */
27 if (enable) {
28 fprintf(stderr, "qemu: thread naming not supported on this host\n");
30 #endif
33 static void error_exit(int err, const char *msg)
35 fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
36 abort();
39 void qemu_mutex_init(QemuMutex *mutex)
41 int err;
43 err = pthread_mutex_init(&mutex->lock, NULL);
44 if (err)
45 error_exit(err, __func__);
46 mutex->initialized = true;
49 void qemu_mutex_destroy(QemuMutex *mutex)
51 int err;
53 assert(mutex->initialized);
54 mutex->initialized = false;
55 err = pthread_mutex_destroy(&mutex->lock);
56 if (err)
57 error_exit(err, __func__);
60 void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
62 int err;
64 assert(mutex->initialized);
65 trace_qemu_mutex_lock(mutex, file, line);
67 err = pthread_mutex_lock(&mutex->lock);
68 if (err)
69 error_exit(err, __func__);
71 trace_qemu_mutex_locked(mutex, file, line);
74 int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
76 int err;
78 assert(mutex->initialized);
79 err = pthread_mutex_trylock(&mutex->lock);
80 if (err == 0) {
81 trace_qemu_mutex_locked(mutex, file, line);
82 return 0;
84 if (err != EBUSY) {
85 error_exit(err, __func__);
87 return -EBUSY;
90 void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
92 int err;
94 assert(mutex->initialized);
95 err = pthread_mutex_unlock(&mutex->lock);
96 if (err)
97 error_exit(err, __func__);
99 trace_qemu_mutex_unlock(mutex, file, line);
102 void qemu_rec_mutex_init(QemuRecMutex *mutex)
104 int err;
105 pthread_mutexattr_t attr;
107 pthread_mutexattr_init(&attr);
108 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
109 err = pthread_mutex_init(&mutex->lock, &attr);
110 pthread_mutexattr_destroy(&attr);
111 if (err) {
112 error_exit(err, __func__);
114 mutex->initialized = true;
117 void qemu_cond_init(QemuCond *cond)
119 int err;
121 err = pthread_cond_init(&cond->cond, NULL);
122 if (err)
123 error_exit(err, __func__);
124 cond->initialized = true;
127 void qemu_cond_destroy(QemuCond *cond)
129 int err;
131 assert(cond->initialized);
132 cond->initialized = false;
133 err = pthread_cond_destroy(&cond->cond);
134 if (err)
135 error_exit(err, __func__);
138 void qemu_cond_signal(QemuCond *cond)
140 int err;
142 assert(cond->initialized);
143 err = pthread_cond_signal(&cond->cond);
144 if (err)
145 error_exit(err, __func__);
148 void qemu_cond_broadcast(QemuCond *cond)
150 int err;
152 assert(cond->initialized);
153 err = pthread_cond_broadcast(&cond->cond);
154 if (err)
155 error_exit(err, __func__);
158 void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
160 int err;
162 assert(cond->initialized);
163 trace_qemu_mutex_unlock(mutex, file, line);
164 err = pthread_cond_wait(&cond->cond, &mutex->lock);
165 trace_qemu_mutex_locked(mutex, file, line);
166 if (err)
167 error_exit(err, __func__);
170 void qemu_sem_init(QemuSemaphore *sem, int init)
172 int rc;
174 #ifndef CONFIG_SEM_TIMEDWAIT
175 rc = pthread_mutex_init(&sem->lock, NULL);
176 if (rc != 0) {
177 error_exit(rc, __func__);
179 rc = pthread_cond_init(&sem->cond, NULL);
180 if (rc != 0) {
181 error_exit(rc, __func__);
183 if (init < 0) {
184 error_exit(EINVAL, __func__);
186 sem->count = init;
187 #else
188 rc = sem_init(&sem->sem, 0, init);
189 if (rc < 0) {
190 error_exit(errno, __func__);
192 #endif
193 sem->initialized = true;
196 void qemu_sem_destroy(QemuSemaphore *sem)
198 int rc;
200 assert(sem->initialized);
201 sem->initialized = false;
202 #ifndef CONFIG_SEM_TIMEDWAIT
203 rc = pthread_cond_destroy(&sem->cond);
204 if (rc < 0) {
205 error_exit(rc, __func__);
207 rc = pthread_mutex_destroy(&sem->lock);
208 if (rc < 0) {
209 error_exit(rc, __func__);
211 #else
212 rc = sem_destroy(&sem->sem);
213 if (rc < 0) {
214 error_exit(errno, __func__);
216 #endif
219 void qemu_sem_post(QemuSemaphore *sem)
221 int rc;
223 assert(sem->initialized);
224 #ifndef CONFIG_SEM_TIMEDWAIT
225 pthread_mutex_lock(&sem->lock);
226 if (sem->count == UINT_MAX) {
227 rc = EINVAL;
228 } else {
229 sem->count++;
230 rc = pthread_cond_signal(&sem->cond);
232 pthread_mutex_unlock(&sem->lock);
233 if (rc != 0) {
234 error_exit(rc, __func__);
236 #else
237 rc = sem_post(&sem->sem);
238 if (rc < 0) {
239 error_exit(errno, __func__);
241 #endif
244 static void compute_abs_deadline(struct timespec *ts, int ms)
246 struct timeval tv;
247 gettimeofday(&tv, NULL);
248 ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
249 ts->tv_sec = tv.tv_sec + ms / 1000;
250 if (ts->tv_nsec >= 1000000000) {
251 ts->tv_sec++;
252 ts->tv_nsec -= 1000000000;
256 int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
258 int rc;
259 struct timespec ts;
261 assert(sem->initialized);
262 #ifndef CONFIG_SEM_TIMEDWAIT
263 rc = 0;
264 compute_abs_deadline(&ts, ms);
265 pthread_mutex_lock(&sem->lock);
266 while (sem->count == 0) {
267 rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts);
268 if (rc == ETIMEDOUT) {
269 break;
271 if (rc != 0) {
272 error_exit(rc, __func__);
275 if (rc != ETIMEDOUT) {
276 --sem->count;
278 pthread_mutex_unlock(&sem->lock);
279 return (rc == ETIMEDOUT ? -1 : 0);
280 #else
281 if (ms <= 0) {
282 /* This is cheaper than sem_timedwait. */
283 do {
284 rc = sem_trywait(&sem->sem);
285 } while (rc == -1 && errno == EINTR);
286 if (rc == -1 && errno == EAGAIN) {
287 return -1;
289 } else {
290 compute_abs_deadline(&ts, ms);
291 do {
292 rc = sem_timedwait(&sem->sem, &ts);
293 } while (rc == -1 && errno == EINTR);
294 if (rc == -1 && errno == ETIMEDOUT) {
295 return -1;
298 if (rc < 0) {
299 error_exit(errno, __func__);
301 return 0;
302 #endif
305 void qemu_sem_wait(QemuSemaphore *sem)
307 int rc;
309 assert(sem->initialized);
310 #ifndef CONFIG_SEM_TIMEDWAIT
311 pthread_mutex_lock(&sem->lock);
312 while (sem->count == 0) {
313 rc = pthread_cond_wait(&sem->cond, &sem->lock);
314 if (rc != 0) {
315 error_exit(rc, __func__);
318 --sem->count;
319 pthread_mutex_unlock(&sem->lock);
320 #else
321 do {
322 rc = sem_wait(&sem->sem);
323 } while (rc == -1 && errno == EINTR);
324 if (rc < 0) {
325 error_exit(errno, __func__);
327 #endif
330 #ifdef __linux__
331 #include "qemu/futex.h"
332 #else
333 static inline void qemu_futex_wake(QemuEvent *ev, int n)
335 assert(ev->initialized);
336 pthread_mutex_lock(&ev->lock);
337 if (n == 1) {
338 pthread_cond_signal(&ev->cond);
339 } else {
340 pthread_cond_broadcast(&ev->cond);
342 pthread_mutex_unlock(&ev->lock);
345 static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
347 assert(ev->initialized);
348 pthread_mutex_lock(&ev->lock);
349 if (ev->value == val) {
350 pthread_cond_wait(&ev->cond, &ev->lock);
352 pthread_mutex_unlock(&ev->lock);
354 #endif
356 /* Valid transitions:
357 * - free->set, when setting the event
358 * - busy->set, when setting the event, followed by qemu_futex_wake
359 * - set->free, when resetting the event
360 * - free->busy, when waiting
362 * set->busy does not happen (it can be observed from the outside but
363 * it really is set->free->busy).
365 * busy->free provably cannot happen; to enforce it, the set->free transition
366 * is done with an OR, which becomes a no-op if the event has concurrently
367 * transitioned to free or busy.
370 #define EV_SET 0
371 #define EV_FREE 1
372 #define EV_BUSY -1
374 void qemu_event_init(QemuEvent *ev, bool init)
376 #ifndef __linux__
377 pthread_mutex_init(&ev->lock, NULL);
378 pthread_cond_init(&ev->cond, NULL);
379 #endif
381 ev->value = (init ? EV_SET : EV_FREE);
382 ev->initialized = true;
385 void qemu_event_destroy(QemuEvent *ev)
387 assert(ev->initialized);
388 ev->initialized = false;
389 #ifndef __linux__
390 pthread_mutex_destroy(&ev->lock);
391 pthread_cond_destroy(&ev->cond);
392 #endif
395 void qemu_event_set(QemuEvent *ev)
397 /* qemu_event_set has release semantics, but because it *loads*
398 * ev->value we need a full memory barrier here.
400 assert(ev->initialized);
401 smp_mb();
402 if (atomic_read(&ev->value) != EV_SET) {
403 if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
404 /* There were waiters, wake them up. */
405 qemu_futex_wake(ev, INT_MAX);
410 void qemu_event_reset(QemuEvent *ev)
412 unsigned value;
414 assert(ev->initialized);
415 value = atomic_read(&ev->value);
416 smp_mb_acquire();
417 if (value == EV_SET) {
419 * If there was a concurrent reset (or even reset+wait),
420 * do nothing. Otherwise change EV_SET->EV_FREE.
422 atomic_or(&ev->value, EV_FREE);
426 void qemu_event_wait(QemuEvent *ev)
428 unsigned value;
430 assert(ev->initialized);
431 value = atomic_read(&ev->value);
432 smp_mb_acquire();
433 if (value != EV_SET) {
434 if (value == EV_FREE) {
436 * Leave the event reset and tell qemu_event_set that there
437 * are waiters. No need to retry, because there cannot be
438 * a concurrent busy->free transition. After the CAS, the
439 * event will be either set or busy.
441 if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
442 return;
445 qemu_futex_wait(ev, EV_BUSY);
449 static pthread_key_t exit_key;
451 union NotifierThreadData {
452 void *ptr;
453 NotifierList list;
455 QEMU_BUILD_BUG_ON(sizeof(union NotifierThreadData) != sizeof(void *));
457 void qemu_thread_atexit_add(Notifier *notifier)
459 union NotifierThreadData ntd;
460 ntd.ptr = pthread_getspecific(exit_key);
461 notifier_list_add(&ntd.list, notifier);
462 pthread_setspecific(exit_key, ntd.ptr);
465 void qemu_thread_atexit_remove(Notifier *notifier)
467 union NotifierThreadData ntd;
468 ntd.ptr = pthread_getspecific(exit_key);
469 notifier_remove(notifier);
470 pthread_setspecific(exit_key, ntd.ptr);
473 static void qemu_thread_atexit_run(void *arg)
475 union NotifierThreadData ntd = { .ptr = arg };
476 notifier_list_notify(&ntd.list, NULL);
479 static void __attribute__((constructor)) qemu_thread_atexit_init(void)
481 pthread_key_create(&exit_key, qemu_thread_atexit_run);
485 #ifdef CONFIG_PTHREAD_SETNAME_NP
486 typedef struct {
487 void *(*start_routine)(void *);
488 void *arg;
489 char *name;
490 } QemuThreadArgs;
492 static void *qemu_thread_start(void *args)
494 QemuThreadArgs *qemu_thread_args = args;
495 void *(*start_routine)(void *) = qemu_thread_args->start_routine;
496 void *arg = qemu_thread_args->arg;
498 /* Attempt to set the threads name; note that this is for debug, so
499 * we're not going to fail if we can't set it.
501 pthread_setname_np(pthread_self(), qemu_thread_args->name);
502 g_free(qemu_thread_args->name);
503 g_free(qemu_thread_args);
504 return start_routine(arg);
506 #endif
509 void qemu_thread_create(QemuThread *thread, const char *name,
510 void *(*start_routine)(void*),
511 void *arg, int mode)
513 sigset_t set, oldset;
514 int err;
515 pthread_attr_t attr;
517 err = pthread_attr_init(&attr);
518 if (err) {
519 error_exit(err, __func__);
522 if (mode == QEMU_THREAD_DETACHED) {
523 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
526 /* Leave signal handling to the iothread. */
527 sigfillset(&set);
528 pthread_sigmask(SIG_SETMASK, &set, &oldset);
530 #ifdef CONFIG_PTHREAD_SETNAME_NP
531 if (name_threads) {
532 QemuThreadArgs *qemu_thread_args;
533 qemu_thread_args = g_new0(QemuThreadArgs, 1);
534 qemu_thread_args->name = g_strdup(name);
535 qemu_thread_args->start_routine = start_routine;
536 qemu_thread_args->arg = arg;
538 err = pthread_create(&thread->thread, &attr,
539 qemu_thread_start, qemu_thread_args);
540 } else
541 #endif
543 err = pthread_create(&thread->thread, &attr,
544 start_routine, arg);
547 if (err)
548 error_exit(err, __func__);
550 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
552 pthread_attr_destroy(&attr);
555 void qemu_thread_get_self(QemuThread *thread)
557 thread->thread = pthread_self();
560 bool qemu_thread_is_self(QemuThread *thread)
562 return pthread_equal(pthread_self(), thread->thread);
565 void qemu_thread_exit(void *retval)
567 pthread_exit(retval);
570 void *qemu_thread_join(QemuThread *thread)
572 int err;
573 void *ret;
575 err = pthread_join(thread->thread, &ret);
576 if (err) {
577 error_exit(err, __func__);
579 return ret;