hw/timer: Add SiFive PWM support
[qemu/ar7.git] / util / qemu-thread-posix.c
blob6c5004220d582fc1d1b3210ec8ad2b6ecc2a9ded
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 "qemu-thread-common.h"
18 #include "qemu/tsan.h"
20 static bool name_threads;
22 void qemu_thread_naming(bool enable)
24 name_threads = enable;
26 #ifndef CONFIG_THREAD_SETNAME_BYTHREAD
27 /* This is a debugging option, not fatal */
28 if (enable) {
29 fprintf(stderr, "qemu: thread naming not supported on this host\n");
31 #endif
34 static void error_exit(int err, const char *msg)
36 fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
37 abort();
40 static void compute_abs_deadline(struct timespec *ts, int ms)
42 struct timeval tv;
43 gettimeofday(&tv, NULL);
44 ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
45 ts->tv_sec = tv.tv_sec + ms / 1000;
46 if (ts->tv_nsec >= 1000000000) {
47 ts->tv_sec++;
48 ts->tv_nsec -= 1000000000;
52 void qemu_mutex_init(QemuMutex *mutex)
54 int err;
56 err = pthread_mutex_init(&mutex->lock, NULL);
57 if (err)
58 error_exit(err, __func__);
59 qemu_mutex_post_init(mutex);
62 void qemu_mutex_destroy(QemuMutex *mutex)
64 int err;
66 assert(mutex->initialized);
67 mutex->initialized = false;
68 err = pthread_mutex_destroy(&mutex->lock);
69 if (err)
70 error_exit(err, __func__);
73 void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
75 int err;
77 assert(mutex->initialized);
78 qemu_mutex_pre_lock(mutex, file, line);
79 err = pthread_mutex_lock(&mutex->lock);
80 if (err)
81 error_exit(err, __func__);
82 qemu_mutex_post_lock(mutex, file, line);
85 int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
87 int err;
89 assert(mutex->initialized);
90 err = pthread_mutex_trylock(&mutex->lock);
91 if (err == 0) {
92 qemu_mutex_post_lock(mutex, file, line);
93 return 0;
95 if (err != EBUSY) {
96 error_exit(err, __func__);
98 return -EBUSY;
101 void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
103 int err;
105 assert(mutex->initialized);
106 qemu_mutex_pre_unlock(mutex, file, line);
107 err = pthread_mutex_unlock(&mutex->lock);
108 if (err)
109 error_exit(err, __func__);
112 void qemu_rec_mutex_init(QemuRecMutex *mutex)
114 int err;
115 pthread_mutexattr_t attr;
117 pthread_mutexattr_init(&attr);
118 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
119 err = pthread_mutex_init(&mutex->m.lock, &attr);
120 pthread_mutexattr_destroy(&attr);
121 if (err) {
122 error_exit(err, __func__);
124 mutex->m.initialized = true;
127 void qemu_rec_mutex_destroy(QemuRecMutex *mutex)
129 qemu_mutex_destroy(&mutex->m);
132 void qemu_rec_mutex_lock_impl(QemuRecMutex *mutex, const char *file, int line)
134 qemu_mutex_lock_impl(&mutex->m, file, line);
137 int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line)
139 return qemu_mutex_trylock_impl(&mutex->m, file, line);
142 void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, const char *file, int line)
144 qemu_mutex_unlock_impl(&mutex->m, file, line);
147 void qemu_cond_init(QemuCond *cond)
149 int err;
151 err = pthread_cond_init(&cond->cond, NULL);
152 if (err)
153 error_exit(err, __func__);
154 cond->initialized = true;
157 void qemu_cond_destroy(QemuCond *cond)
159 int err;
161 assert(cond->initialized);
162 cond->initialized = false;
163 err = pthread_cond_destroy(&cond->cond);
164 if (err)
165 error_exit(err, __func__);
168 void qemu_cond_signal(QemuCond *cond)
170 int err;
172 assert(cond->initialized);
173 err = pthread_cond_signal(&cond->cond);
174 if (err)
175 error_exit(err, __func__);
178 void qemu_cond_broadcast(QemuCond *cond)
180 int err;
182 assert(cond->initialized);
183 err = pthread_cond_broadcast(&cond->cond);
184 if (err)
185 error_exit(err, __func__);
188 void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
190 int err;
192 assert(cond->initialized);
193 qemu_mutex_pre_unlock(mutex, file, line);
194 err = pthread_cond_wait(&cond->cond, &mutex->lock);
195 qemu_mutex_post_lock(mutex, file, line);
196 if (err)
197 error_exit(err, __func__);
200 bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms,
201 const char *file, const int line)
203 int err;
204 struct timespec ts;
206 assert(cond->initialized);
207 trace_qemu_mutex_unlock(mutex, file, line);
208 compute_abs_deadline(&ts, ms);
209 err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
210 trace_qemu_mutex_locked(mutex, file, line);
211 if (err && err != ETIMEDOUT) {
212 error_exit(err, __func__);
214 return err != ETIMEDOUT;
217 void qemu_sem_init(QemuSemaphore *sem, int init)
219 int rc;
221 #ifndef CONFIG_SEM_TIMEDWAIT
222 rc = pthread_mutex_init(&sem->lock, NULL);
223 if (rc != 0) {
224 error_exit(rc, __func__);
226 rc = pthread_cond_init(&sem->cond, NULL);
227 if (rc != 0) {
228 error_exit(rc, __func__);
230 if (init < 0) {
231 error_exit(EINVAL, __func__);
233 sem->count = init;
234 #else
235 rc = sem_init(&sem->sem, 0, init);
236 if (rc < 0) {
237 error_exit(errno, __func__);
239 #endif
240 sem->initialized = true;
243 void qemu_sem_destroy(QemuSemaphore *sem)
245 int rc;
247 assert(sem->initialized);
248 sem->initialized = false;
249 #ifndef CONFIG_SEM_TIMEDWAIT
250 rc = pthread_cond_destroy(&sem->cond);
251 if (rc < 0) {
252 error_exit(rc, __func__);
254 rc = pthread_mutex_destroy(&sem->lock);
255 if (rc < 0) {
256 error_exit(rc, __func__);
258 #else
259 rc = sem_destroy(&sem->sem);
260 if (rc < 0) {
261 error_exit(errno, __func__);
263 #endif
266 void qemu_sem_post(QemuSemaphore *sem)
268 int rc;
270 assert(sem->initialized);
271 #ifndef CONFIG_SEM_TIMEDWAIT
272 pthread_mutex_lock(&sem->lock);
273 if (sem->count == UINT_MAX) {
274 rc = EINVAL;
275 } else {
276 sem->count++;
277 rc = pthread_cond_signal(&sem->cond);
279 pthread_mutex_unlock(&sem->lock);
280 if (rc != 0) {
281 error_exit(rc, __func__);
283 #else
284 rc = sem_post(&sem->sem);
285 if (rc < 0) {
286 error_exit(errno, __func__);
288 #endif
291 int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
293 int rc;
294 struct timespec ts;
296 assert(sem->initialized);
297 #ifndef CONFIG_SEM_TIMEDWAIT
298 rc = 0;
299 compute_abs_deadline(&ts, ms);
300 pthread_mutex_lock(&sem->lock);
301 while (sem->count == 0) {
302 rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts);
303 if (rc == ETIMEDOUT) {
304 break;
306 if (rc != 0) {
307 error_exit(rc, __func__);
310 if (rc != ETIMEDOUT) {
311 --sem->count;
313 pthread_mutex_unlock(&sem->lock);
314 return (rc == ETIMEDOUT ? -1 : 0);
315 #else
316 if (ms <= 0) {
317 /* This is cheaper than sem_timedwait. */
318 do {
319 rc = sem_trywait(&sem->sem);
320 } while (rc == -1 && errno == EINTR);
321 if (rc == -1 && errno == EAGAIN) {
322 return -1;
324 } else {
325 compute_abs_deadline(&ts, ms);
326 do {
327 rc = sem_timedwait(&sem->sem, &ts);
328 } while (rc == -1 && errno == EINTR);
329 if (rc == -1 && errno == ETIMEDOUT) {
330 return -1;
333 if (rc < 0) {
334 error_exit(errno, __func__);
336 return 0;
337 #endif
340 void qemu_sem_wait(QemuSemaphore *sem)
342 int rc;
344 assert(sem->initialized);
345 #ifndef CONFIG_SEM_TIMEDWAIT
346 pthread_mutex_lock(&sem->lock);
347 while (sem->count == 0) {
348 rc = pthread_cond_wait(&sem->cond, &sem->lock);
349 if (rc != 0) {
350 error_exit(rc, __func__);
353 --sem->count;
354 pthread_mutex_unlock(&sem->lock);
355 #else
356 do {
357 rc = sem_wait(&sem->sem);
358 } while (rc == -1 && errno == EINTR);
359 if (rc < 0) {
360 error_exit(errno, __func__);
362 #endif
365 #ifdef __linux__
366 #include "qemu/futex.h"
367 #else
368 static inline void qemu_futex_wake(QemuEvent *ev, int n)
370 assert(ev->initialized);
371 pthread_mutex_lock(&ev->lock);
372 if (n == 1) {
373 pthread_cond_signal(&ev->cond);
374 } else {
375 pthread_cond_broadcast(&ev->cond);
377 pthread_mutex_unlock(&ev->lock);
380 static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
382 assert(ev->initialized);
383 pthread_mutex_lock(&ev->lock);
384 if (ev->value == val) {
385 pthread_cond_wait(&ev->cond, &ev->lock);
387 pthread_mutex_unlock(&ev->lock);
389 #endif
391 /* Valid transitions:
392 * - free->set, when setting the event
393 * - busy->set, when setting the event, followed by qemu_futex_wake
394 * - set->free, when resetting the event
395 * - free->busy, when waiting
397 * set->busy does not happen (it can be observed from the outside but
398 * it really is set->free->busy).
400 * busy->free provably cannot happen; to enforce it, the set->free transition
401 * is done with an OR, which becomes a no-op if the event has concurrently
402 * transitioned to free or busy.
405 #define EV_SET 0
406 #define EV_FREE 1
407 #define EV_BUSY -1
409 void qemu_event_init(QemuEvent *ev, bool init)
411 #ifndef __linux__
412 pthread_mutex_init(&ev->lock, NULL);
413 pthread_cond_init(&ev->cond, NULL);
414 #endif
416 ev->value = (init ? EV_SET : EV_FREE);
417 ev->initialized = true;
420 void qemu_event_destroy(QemuEvent *ev)
422 assert(ev->initialized);
423 ev->initialized = false;
424 #ifndef __linux__
425 pthread_mutex_destroy(&ev->lock);
426 pthread_cond_destroy(&ev->cond);
427 #endif
430 void qemu_event_set(QemuEvent *ev)
432 /* qemu_event_set has release semantics, but because it *loads*
433 * ev->value we need a full memory barrier here.
435 assert(ev->initialized);
436 smp_mb();
437 if (qatomic_read(&ev->value) != EV_SET) {
438 if (qatomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
439 /* There were waiters, wake them up. */
440 qemu_futex_wake(ev, INT_MAX);
445 void qemu_event_reset(QemuEvent *ev)
447 unsigned value;
449 assert(ev->initialized);
450 value = qatomic_read(&ev->value);
451 smp_mb_acquire();
452 if (value == EV_SET) {
454 * If there was a concurrent reset (or even reset+wait),
455 * do nothing. Otherwise change EV_SET->EV_FREE.
457 qatomic_or(&ev->value, EV_FREE);
461 void qemu_event_wait(QemuEvent *ev)
463 unsigned value;
465 assert(ev->initialized);
466 value = qatomic_read(&ev->value);
467 smp_mb_acquire();
468 if (value != EV_SET) {
469 if (value == EV_FREE) {
471 * Leave the event reset and tell qemu_event_set that there
472 * are waiters. No need to retry, because there cannot be
473 * a concurrent busy->free transition. After the CAS, the
474 * event will be either set or busy.
476 if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
477 return;
480 qemu_futex_wait(ev, EV_BUSY);
484 static __thread NotifierList thread_exit;
487 * Note that in this implementation you can register a thread-exit
488 * notifier for the main thread, but it will never be called.
489 * This is OK because main thread exit can only happen when the
490 * entire process is exiting, and the API allows notifiers to not
491 * be called on process exit.
493 void qemu_thread_atexit_add(Notifier *notifier)
495 notifier_list_add(&thread_exit, notifier);
498 void qemu_thread_atexit_remove(Notifier *notifier)
500 notifier_remove(notifier);
503 static void qemu_thread_atexit_notify(void *arg)
506 * Called when non-main thread exits (via qemu_thread_exit()
507 * or by returning from its start routine.)
509 notifier_list_notify(&thread_exit, NULL);
512 typedef struct {
513 void *(*start_routine)(void *);
514 void *arg;
515 char *name;
516 } QemuThreadArgs;
518 static void *qemu_thread_start(void *args)
520 QemuThreadArgs *qemu_thread_args = args;
521 void *(*start_routine)(void *) = qemu_thread_args->start_routine;
522 void *arg = qemu_thread_args->arg;
523 void *r;
525 #ifdef CONFIG_THREAD_SETNAME_BYTHREAD
526 /* Attempt to set the threads name; note that this is for debug, so
527 * we're not going to fail if we can't set it.
529 if (name_threads && qemu_thread_args->name) {
530 # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
531 pthread_setname_np(pthread_self(), qemu_thread_args->name);
532 # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
533 pthread_setname_np(qemu_thread_args->name);
534 # endif
536 #endif
537 QEMU_TSAN_ANNOTATE_THREAD_NAME(qemu_thread_args->name);
538 g_free(qemu_thread_args->name);
539 g_free(qemu_thread_args);
542 * GCC 11 with glibc 2.17 on PowerPC reports
544 * qemu-thread-posix.c:540:5: error: ‘__sigsetjmp’ accessing 656 bytes
545 * in a region of size 528 [-Werror=stringop-overflow=]
546 * 540 | pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
547 * | ^~~~~~~~~~~~~~~~~~~~
549 * which is clearly nonsense.
551 #pragma GCC diagnostic push
552 #ifndef __clang__
553 #pragma GCC diagnostic ignored "-Wstringop-overflow"
554 #endif
556 pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
557 r = start_routine(arg);
558 pthread_cleanup_pop(1);
560 #pragma GCC diagnostic pop
562 return r;
565 void qemu_thread_create(QemuThread *thread, const char *name,
566 void *(*start_routine)(void*),
567 void *arg, int mode)
569 sigset_t set, oldset;
570 int err;
571 pthread_attr_t attr;
572 QemuThreadArgs *qemu_thread_args;
574 err = pthread_attr_init(&attr);
575 if (err) {
576 error_exit(err, __func__);
579 if (mode == QEMU_THREAD_DETACHED) {
580 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
583 /* Leave signal handling to the iothread. */
584 sigfillset(&set);
585 /* Blocking the signals can result in undefined behaviour. */
586 sigdelset(&set, SIGSEGV);
587 sigdelset(&set, SIGFPE);
588 sigdelset(&set, SIGILL);
589 /* TODO avoid SIGBUS loss on macOS */
590 pthread_sigmask(SIG_SETMASK, &set, &oldset);
592 qemu_thread_args = g_new0(QemuThreadArgs, 1);
593 qemu_thread_args->name = g_strdup(name);
594 qemu_thread_args->start_routine = start_routine;
595 qemu_thread_args->arg = arg;
597 err = pthread_create(&thread->thread, &attr,
598 qemu_thread_start, qemu_thread_args);
600 if (err)
601 error_exit(err, __func__);
603 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
605 pthread_attr_destroy(&attr);
608 void qemu_thread_get_self(QemuThread *thread)
610 thread->thread = pthread_self();
613 bool qemu_thread_is_self(QemuThread *thread)
615 return pthread_equal(pthread_self(), thread->thread);
618 void qemu_thread_exit(void *retval)
620 pthread_exit(retval);
623 void *qemu_thread_join(QemuThread *thread)
625 int err;
626 void *ret;
628 err = pthread_join(thread->thread, &ret);
629 if (err) {
630 error_exit(err, __func__);
632 return ret;