arm/xlnx-versal-virt: use memdev for RAM
[qemu.git] / util / qemu-thread-posix.c
blob838980aaa55b480c7b7303bdcc69d515084b0246
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"
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 static void compute_abs_deadline(struct timespec *ts, int ms)
41 struct timeval tv;
42 gettimeofday(&tv, NULL);
43 ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
44 ts->tv_sec = tv.tv_sec + ms / 1000;
45 if (ts->tv_nsec >= 1000000000) {
46 ts->tv_sec++;
47 ts->tv_nsec -= 1000000000;
51 void qemu_mutex_init(QemuMutex *mutex)
53 int err;
55 err = pthread_mutex_init(&mutex->lock, NULL);
56 if (err)
57 error_exit(err, __func__);
58 qemu_mutex_post_init(mutex);
61 void qemu_mutex_destroy(QemuMutex *mutex)
63 int err;
65 assert(mutex->initialized);
66 mutex->initialized = false;
67 err = pthread_mutex_destroy(&mutex->lock);
68 if (err)
69 error_exit(err, __func__);
72 void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
74 int err;
76 assert(mutex->initialized);
77 qemu_mutex_pre_lock(mutex, file, line);
78 err = pthread_mutex_lock(&mutex->lock);
79 if (err)
80 error_exit(err, __func__);
81 qemu_mutex_post_lock(mutex, file, line);
84 int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
86 int err;
88 assert(mutex->initialized);
89 err = pthread_mutex_trylock(&mutex->lock);
90 if (err == 0) {
91 qemu_mutex_post_lock(mutex, file, line);
92 return 0;
94 if (err != EBUSY) {
95 error_exit(err, __func__);
97 return -EBUSY;
100 void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
102 int err;
104 assert(mutex->initialized);
105 qemu_mutex_pre_unlock(mutex, file, line);
106 err = pthread_mutex_unlock(&mutex->lock);
107 if (err)
108 error_exit(err, __func__);
111 void qemu_rec_mutex_init(QemuRecMutex *mutex)
113 int err;
114 pthread_mutexattr_t attr;
116 pthread_mutexattr_init(&attr);
117 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
118 err = pthread_mutex_init(&mutex->lock, &attr);
119 pthread_mutexattr_destroy(&attr);
120 if (err) {
121 error_exit(err, __func__);
123 mutex->initialized = true;
126 void qemu_cond_init(QemuCond *cond)
128 int err;
130 err = pthread_cond_init(&cond->cond, NULL);
131 if (err)
132 error_exit(err, __func__);
133 cond->initialized = true;
136 void qemu_cond_destroy(QemuCond *cond)
138 int err;
140 assert(cond->initialized);
141 cond->initialized = false;
142 err = pthread_cond_destroy(&cond->cond);
143 if (err)
144 error_exit(err, __func__);
147 void qemu_cond_signal(QemuCond *cond)
149 int err;
151 assert(cond->initialized);
152 err = pthread_cond_signal(&cond->cond);
153 if (err)
154 error_exit(err, __func__);
157 void qemu_cond_broadcast(QemuCond *cond)
159 int err;
161 assert(cond->initialized);
162 err = pthread_cond_broadcast(&cond->cond);
163 if (err)
164 error_exit(err, __func__);
167 void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
169 int err;
171 assert(cond->initialized);
172 qemu_mutex_pre_unlock(mutex, file, line);
173 err = pthread_cond_wait(&cond->cond, &mutex->lock);
174 qemu_mutex_post_lock(mutex, file, line);
175 if (err)
176 error_exit(err, __func__);
179 bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms,
180 const char *file, const int line)
182 int err;
183 struct timespec ts;
185 assert(cond->initialized);
186 trace_qemu_mutex_unlock(mutex, file, line);
187 compute_abs_deadline(&ts, ms);
188 err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
189 trace_qemu_mutex_locked(mutex, file, line);
190 if (err && err != ETIMEDOUT) {
191 error_exit(err, __func__);
193 return err != ETIMEDOUT;
196 void qemu_sem_init(QemuSemaphore *sem, int init)
198 int rc;
200 #ifndef CONFIG_SEM_TIMEDWAIT
201 rc = pthread_mutex_init(&sem->lock, NULL);
202 if (rc != 0) {
203 error_exit(rc, __func__);
205 rc = pthread_cond_init(&sem->cond, NULL);
206 if (rc != 0) {
207 error_exit(rc, __func__);
209 if (init < 0) {
210 error_exit(EINVAL, __func__);
212 sem->count = init;
213 #else
214 rc = sem_init(&sem->sem, 0, init);
215 if (rc < 0) {
216 error_exit(errno, __func__);
218 #endif
219 sem->initialized = true;
222 void qemu_sem_destroy(QemuSemaphore *sem)
224 int rc;
226 assert(sem->initialized);
227 sem->initialized = false;
228 #ifndef CONFIG_SEM_TIMEDWAIT
229 rc = pthread_cond_destroy(&sem->cond);
230 if (rc < 0) {
231 error_exit(rc, __func__);
233 rc = pthread_mutex_destroy(&sem->lock);
234 if (rc < 0) {
235 error_exit(rc, __func__);
237 #else
238 rc = sem_destroy(&sem->sem);
239 if (rc < 0) {
240 error_exit(errno, __func__);
242 #endif
245 void qemu_sem_post(QemuSemaphore *sem)
247 int rc;
249 assert(sem->initialized);
250 #ifndef CONFIG_SEM_TIMEDWAIT
251 pthread_mutex_lock(&sem->lock);
252 if (sem->count == UINT_MAX) {
253 rc = EINVAL;
254 } else {
255 sem->count++;
256 rc = pthread_cond_signal(&sem->cond);
258 pthread_mutex_unlock(&sem->lock);
259 if (rc != 0) {
260 error_exit(rc, __func__);
262 #else
263 rc = sem_post(&sem->sem);
264 if (rc < 0) {
265 error_exit(errno, __func__);
267 #endif
270 int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
272 int rc;
273 struct timespec ts;
275 assert(sem->initialized);
276 #ifndef CONFIG_SEM_TIMEDWAIT
277 rc = 0;
278 compute_abs_deadline(&ts, ms);
279 pthread_mutex_lock(&sem->lock);
280 while (sem->count == 0) {
281 rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts);
282 if (rc == ETIMEDOUT) {
283 break;
285 if (rc != 0) {
286 error_exit(rc, __func__);
289 if (rc != ETIMEDOUT) {
290 --sem->count;
292 pthread_mutex_unlock(&sem->lock);
293 return (rc == ETIMEDOUT ? -1 : 0);
294 #else
295 if (ms <= 0) {
296 /* This is cheaper than sem_timedwait. */
297 do {
298 rc = sem_trywait(&sem->sem);
299 } while (rc == -1 && errno == EINTR);
300 if (rc == -1 && errno == EAGAIN) {
301 return -1;
303 } else {
304 compute_abs_deadline(&ts, ms);
305 do {
306 rc = sem_timedwait(&sem->sem, &ts);
307 } while (rc == -1 && errno == EINTR);
308 if (rc == -1 && errno == ETIMEDOUT) {
309 return -1;
312 if (rc < 0) {
313 error_exit(errno, __func__);
315 return 0;
316 #endif
319 void qemu_sem_wait(QemuSemaphore *sem)
321 int rc;
323 assert(sem->initialized);
324 #ifndef CONFIG_SEM_TIMEDWAIT
325 pthread_mutex_lock(&sem->lock);
326 while (sem->count == 0) {
327 rc = pthread_cond_wait(&sem->cond, &sem->lock);
328 if (rc != 0) {
329 error_exit(rc, __func__);
332 --sem->count;
333 pthread_mutex_unlock(&sem->lock);
334 #else
335 do {
336 rc = sem_wait(&sem->sem);
337 } while (rc == -1 && errno == EINTR);
338 if (rc < 0) {
339 error_exit(errno, __func__);
341 #endif
344 #ifdef __linux__
345 #include "qemu/futex.h"
346 #else
347 static inline void qemu_futex_wake(QemuEvent *ev, int n)
349 assert(ev->initialized);
350 pthread_mutex_lock(&ev->lock);
351 if (n == 1) {
352 pthread_cond_signal(&ev->cond);
353 } else {
354 pthread_cond_broadcast(&ev->cond);
356 pthread_mutex_unlock(&ev->lock);
359 static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
361 assert(ev->initialized);
362 pthread_mutex_lock(&ev->lock);
363 if (ev->value == val) {
364 pthread_cond_wait(&ev->cond, &ev->lock);
366 pthread_mutex_unlock(&ev->lock);
368 #endif
370 /* Valid transitions:
371 * - free->set, when setting the event
372 * - busy->set, when setting the event, followed by qemu_futex_wake
373 * - set->free, when resetting the event
374 * - free->busy, when waiting
376 * set->busy does not happen (it can be observed from the outside but
377 * it really is set->free->busy).
379 * busy->free provably cannot happen; to enforce it, the set->free transition
380 * is done with an OR, which becomes a no-op if the event has concurrently
381 * transitioned to free or busy.
384 #define EV_SET 0
385 #define EV_FREE 1
386 #define EV_BUSY -1
388 void qemu_event_init(QemuEvent *ev, bool init)
390 #ifndef __linux__
391 pthread_mutex_init(&ev->lock, NULL);
392 pthread_cond_init(&ev->cond, NULL);
393 #endif
395 ev->value = (init ? EV_SET : EV_FREE);
396 ev->initialized = true;
399 void qemu_event_destroy(QemuEvent *ev)
401 assert(ev->initialized);
402 ev->initialized = false;
403 #ifndef __linux__
404 pthread_mutex_destroy(&ev->lock);
405 pthread_cond_destroy(&ev->cond);
406 #endif
409 void qemu_event_set(QemuEvent *ev)
411 /* qemu_event_set has release semantics, but because it *loads*
412 * ev->value we need a full memory barrier here.
414 assert(ev->initialized);
415 smp_mb();
416 if (atomic_read(&ev->value) != EV_SET) {
417 if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
418 /* There were waiters, wake them up. */
419 qemu_futex_wake(ev, INT_MAX);
424 void qemu_event_reset(QemuEvent *ev)
426 unsigned value;
428 assert(ev->initialized);
429 value = atomic_read(&ev->value);
430 smp_mb_acquire();
431 if (value == EV_SET) {
433 * If there was a concurrent reset (or even reset+wait),
434 * do nothing. Otherwise change EV_SET->EV_FREE.
436 atomic_or(&ev->value, EV_FREE);
440 void qemu_event_wait(QemuEvent *ev)
442 unsigned value;
444 assert(ev->initialized);
445 value = atomic_read(&ev->value);
446 smp_mb_acquire();
447 if (value != EV_SET) {
448 if (value == EV_FREE) {
450 * Leave the event reset and tell qemu_event_set that there
451 * are waiters. No need to retry, because there cannot be
452 * a concurrent busy->free transition. After the CAS, the
453 * event will be either set or busy.
455 if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
456 return;
459 qemu_futex_wait(ev, EV_BUSY);
463 static __thread NotifierList thread_exit;
466 * Note that in this implementation you can register a thread-exit
467 * notifier for the main thread, but it will never be called.
468 * This is OK because main thread exit can only happen when the
469 * entire process is exiting, and the API allows notifiers to not
470 * be called on process exit.
472 void qemu_thread_atexit_add(Notifier *notifier)
474 notifier_list_add(&thread_exit, notifier);
477 void qemu_thread_atexit_remove(Notifier *notifier)
479 notifier_remove(notifier);
482 static void qemu_thread_atexit_notify(void *arg)
485 * Called when non-main thread exits (via qemu_thread_exit()
486 * or by returning from its start routine.)
488 notifier_list_notify(&thread_exit, NULL);
491 typedef struct {
492 void *(*start_routine)(void *);
493 void *arg;
494 char *name;
495 } QemuThreadArgs;
497 static void *qemu_thread_start(void *args)
499 QemuThreadArgs *qemu_thread_args = args;
500 void *(*start_routine)(void *) = qemu_thread_args->start_routine;
501 void *arg = qemu_thread_args->arg;
502 void *r;
504 #ifdef CONFIG_THREAD_SETNAME_BYTHREAD
505 /* Attempt to set the threads name; note that this is for debug, so
506 * we're not going to fail if we can't set it.
508 if (name_threads && qemu_thread_args->name) {
509 # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
510 pthread_setname_np(pthread_self(), qemu_thread_args->name);
511 # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
512 pthread_setname_np(qemu_thread_args->name);
513 # endif
515 #endif
516 g_free(qemu_thread_args->name);
517 g_free(qemu_thread_args);
518 pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
519 r = start_routine(arg);
520 pthread_cleanup_pop(1);
521 return r;
524 void qemu_thread_create(QemuThread *thread, const char *name,
525 void *(*start_routine)(void*),
526 void *arg, int mode)
528 sigset_t set, oldset;
529 int err;
530 pthread_attr_t attr;
531 QemuThreadArgs *qemu_thread_args;
533 err = pthread_attr_init(&attr);
534 if (err) {
535 error_exit(err, __func__);
538 if (mode == QEMU_THREAD_DETACHED) {
539 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
542 /* Leave signal handling to the iothread. */
543 sigfillset(&set);
544 /* Blocking the signals can result in undefined behaviour. */
545 sigdelset(&set, SIGSEGV);
546 sigdelset(&set, SIGFPE);
547 sigdelset(&set, SIGILL);
548 /* TODO avoid SIGBUS loss on macOS */
549 pthread_sigmask(SIG_SETMASK, &set, &oldset);
551 qemu_thread_args = g_new0(QemuThreadArgs, 1);
552 qemu_thread_args->name = g_strdup(name);
553 qemu_thread_args->start_routine = start_routine;
554 qemu_thread_args->arg = arg;
556 err = pthread_create(&thread->thread, &attr,
557 qemu_thread_start, qemu_thread_args);
559 if (err)
560 error_exit(err, __func__);
562 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
564 pthread_attr_destroy(&attr);
567 void qemu_thread_get_self(QemuThread *thread)
569 thread->thread = pthread_self();
572 bool qemu_thread_is_self(QemuThread *thread)
574 return pthread_equal(pthread_self(), thread->thread);
577 void qemu_thread_exit(void *retval)
579 pthread_exit(retval);
582 void *qemu_thread_join(QemuThread *thread)
584 int err;
585 void *ret;
587 err = pthread_join(thread->thread, &ret);
588 if (err) {
589 error_exit(err, __func__);
591 return ret;