]> git.proxmox.com Git - mirror_qemu.git/blame - util/qemu-thread-posix.c
tests/qtest: libqtest: Correct the timeout unit of blocking receive calls for win32
[mirror_qemu.git] / util / qemu-thread-posix.c
CommitLineData
e5d355d1
AL
1/*
2 * Wrappers around mutex/cond/thread functions
3 *
4 * Copyright Red Hat, Inc. 2009
5 *
6 * Author:
7 * Marcelo Tosatti <mtosatti@redhat.com>
8 *
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.
11 *
12 */
aafd7584 13#include "qemu/osdep.h"
1de7afc9 14#include "qemu/thread.h"
c7c4d063 15#include "qemu/atomic.h"
ef57137f 16#include "qemu/notify.h"
f1aff7aa 17#include "qemu-thread-common.h"
ce9f0e5b 18#include "qemu/tsan.h"
e5d355d1 19
8f480de0
DDAG
20static bool name_threads;
21
22void qemu_thread_naming(bool enable)
23{
24 name_threads = enable;
5c312079 25
10f6b231
PB
26#if !defined CONFIG_PTHREAD_SETNAME_NP_W_TID && \
27 !defined CONFIG_PTHREAD_SETNAME_NP_WO_TID
5c312079
DDAG
28 /* This is a debugging option, not fatal */
29 if (enable) {
30 fprintf(stderr, "qemu: thread naming not supported on this host\n");
31 }
32#endif
8f480de0
DDAG
33}
34
e5d355d1
AL
35static void error_exit(int err, const char *msg)
36{
37 fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
53380ac3 38 abort();
e5d355d1
AL
39}
40
657ac98b
LM
41static inline clockid_t qemu_timedwait_clockid(void)
42{
43#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
44 return CLOCK_MONOTONIC;
45#else
46 return CLOCK_REALTIME;
47#endif
48}
49
3dcc9c6e
YK
50static void compute_abs_deadline(struct timespec *ts, int ms)
51{
657ac98b
LM
52 clock_gettime(qemu_timedwait_clockid(), ts);
53 ts->tv_nsec += (ms % 1000) * 1000000;
54 ts->tv_sec += ms / 1000;
3dcc9c6e
YK
55 if (ts->tv_nsec >= 1000000000) {
56 ts->tv_sec++;
57 ts->tv_nsec -= 1000000000;
58 }
59}
60
e5d355d1
AL
61void qemu_mutex_init(QemuMutex *mutex)
62{
63 int err;
64
24fa9049 65 err = pthread_mutex_init(&mutex->lock, NULL);
e5d355d1
AL
66 if (err)
67 error_exit(err, __func__);
f1aff7aa 68 qemu_mutex_post_init(mutex);
e5d355d1
AL
69}
70
313b1d69
CC
71void qemu_mutex_destroy(QemuMutex *mutex)
72{
73 int err;
74
c096358e
FZ
75 assert(mutex->initialized);
76 mutex->initialized = false;
313b1d69
CC
77 err = pthread_mutex_destroy(&mutex->lock);
78 if (err)
79 error_exit(err, __func__);
80}
81
6c27a0de 82void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
e5d355d1
AL
83{
84 int err;
85
c096358e 86 assert(mutex->initialized);
f1aff7aa 87 qemu_mutex_pre_lock(mutex, file, line);
e5d355d1
AL
88 err = pthread_mutex_lock(&mutex->lock);
89 if (err)
90 error_exit(err, __func__);
f1aff7aa 91 qemu_mutex_post_lock(mutex, file, line);
e5d355d1
AL
92}
93
6c27a0de 94int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
e5d355d1 95{
31f5a726
JRZ
96 int err;
97
c096358e 98 assert(mutex->initialized);
31f5a726
JRZ
99 err = pthread_mutex_trylock(&mutex->lock);
100 if (err == 0) {
f1aff7aa 101 qemu_mutex_post_lock(mutex, file, line);
31f5a726
JRZ
102 return 0;
103 }
104 if (err != EBUSY) {
105 error_exit(err, __func__);
106 }
107 return -EBUSY;
e5d355d1
AL
108}
109
6c27a0de 110void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
e5d355d1
AL
111{
112 int err;
113
c096358e 114 assert(mutex->initialized);
f1aff7aa 115 qemu_mutex_pre_unlock(mutex, file, line);
e5d355d1
AL
116 err = pthread_mutex_unlock(&mutex->lock);
117 if (err)
118 error_exit(err, __func__);
119}
120
feadec63
PB
121void qemu_rec_mutex_init(QemuRecMutex *mutex)
122{
123 int err;
124 pthread_mutexattr_t attr;
125
126 pthread_mutexattr_init(&attr);
127 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
6c98635e 128 err = pthread_mutex_init(&mutex->m.lock, &attr);
feadec63
PB
129 pthread_mutexattr_destroy(&attr);
130 if (err) {
131 error_exit(err, __func__);
132 }
6c98635e 133 mutex->m.initialized = true;
feadec63
PB
134}
135
4b193bb7
RH
136void qemu_rec_mutex_destroy(QemuRecMutex *mutex)
137{
6c98635e 138 qemu_mutex_destroy(&mutex->m);
4b193bb7
RH
139}
140
141void qemu_rec_mutex_lock_impl(QemuRecMutex *mutex, const char *file, int line)
142{
6c98635e 143 qemu_mutex_lock_impl(&mutex->m, file, line);
4b193bb7
RH
144}
145
146int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line)
147{
6c98635e 148 return qemu_mutex_trylock_impl(&mutex->m, file, line);
4b193bb7
RH
149}
150
9c75bae7 151void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, const char *file, int line)
4b193bb7 152{
6c98635e 153 qemu_mutex_unlock_impl(&mutex->m, file, line);
4b193bb7
RH
154}
155
e5d355d1
AL
156void qemu_cond_init(QemuCond *cond)
157{
657ac98b 158 pthread_condattr_t attr;
e5d355d1
AL
159 int err;
160
657ac98b
LM
161 err = pthread_condattr_init(&attr);
162 if (err) {
163 error_exit(err, __func__);
164 }
165#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
166 err = pthread_condattr_setclock(&attr, qemu_timedwait_clockid());
167 if (err) {
e5d355d1 168 error_exit(err, __func__);
657ac98b
LM
169 }
170#endif
171 err = pthread_cond_init(&cond->cond, &attr);
172 if (err) {
173 error_exit(err, __func__);
174 }
175 err = pthread_condattr_destroy(&attr);
176 if (err) {
177 error_exit(err, __func__);
178 }
c096358e 179 cond->initialized = true;
e5d355d1
AL
180}
181
313b1d69
CC
182void qemu_cond_destroy(QemuCond *cond)
183{
184 int err;
185
c096358e
FZ
186 assert(cond->initialized);
187 cond->initialized = false;
313b1d69
CC
188 err = pthread_cond_destroy(&cond->cond);
189 if (err)
190 error_exit(err, __func__);
191}
192
e5d355d1
AL
193void qemu_cond_signal(QemuCond *cond)
194{
195 int err;
196
c096358e 197 assert(cond->initialized);
e5d355d1
AL
198 err = pthread_cond_signal(&cond->cond);
199 if (err)
200 error_exit(err, __func__);
201}
202
203void qemu_cond_broadcast(QemuCond *cond)
204{
205 int err;
206
c096358e 207 assert(cond->initialized);
e5d355d1
AL
208 err = pthread_cond_broadcast(&cond->cond);
209 if (err)
210 error_exit(err, __func__);
211}
212
6c27a0de 213void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
e5d355d1
AL
214{
215 int err;
216
c096358e 217 assert(cond->initialized);
f1aff7aa 218 qemu_mutex_pre_unlock(mutex, file, line);
e5d355d1 219 err = pthread_cond_wait(&cond->cond, &mutex->lock);
f1aff7aa 220 qemu_mutex_post_lock(mutex, file, line);
e5d355d1
AL
221 if (err)
222 error_exit(err, __func__);
223}
224
a0d45db8
LM
225static bool
226qemu_cond_timedwait_ts(QemuCond *cond, QemuMutex *mutex, struct timespec *ts,
227 const char *file, const int line)
3dcc9c6e
YK
228{
229 int err;
3dcc9c6e
YK
230
231 assert(cond->initialized);
232 trace_qemu_mutex_unlock(mutex, file, line);
a0d45db8 233 err = pthread_cond_timedwait(&cond->cond, &mutex->lock, ts);
3dcc9c6e
YK
234 trace_qemu_mutex_locked(mutex, file, line);
235 if (err && err != ETIMEDOUT) {
236 error_exit(err, __func__);
237 }
238 return err != ETIMEDOUT;
239}
240
a0d45db8
LM
241bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms,
242 const char *file, const int line)
243{
244 struct timespec ts;
245
246 compute_abs_deadline(&ts, ms);
247 return qemu_cond_timedwait_ts(cond, mutex, &ts, file, line);
248}
249
38b14db3
PB
250void qemu_sem_init(QemuSemaphore *sem, int init)
251{
a0d45db8
LM
252 qemu_mutex_init(&sem->mutex);
253 qemu_cond_init(&sem->cond);
38b14db3 254
c166cb72
PB
255 if (init < 0) {
256 error_exit(EINVAL, __func__);
257 }
258 sem->count = init;
38b14db3
PB
259}
260
261void qemu_sem_destroy(QemuSemaphore *sem)
262{
a0d45db8
LM
263 qemu_cond_destroy(&sem->cond);
264 qemu_mutex_destroy(&sem->mutex);
38b14db3
PB
265}
266
267void qemu_sem_post(QemuSemaphore *sem)
268{
a0d45db8 269 qemu_mutex_lock(&sem->mutex);
79761c66 270 if (sem->count == UINT_MAX) {
a0d45db8 271 error_exit(EINVAL, __func__);
c166cb72 272 } else {
79761c66 273 sem->count++;
a0d45db8 274 qemu_cond_signal(&sem->cond);
c166cb72 275 }
a0d45db8 276 qemu_mutex_unlock(&sem->mutex);
c166cb72
PB
277}
278
38b14db3
PB
279int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
280{
a0d45db8 281 bool rc = true;
c166cb72
PB
282 struct timespec ts;
283
c166cb72 284 compute_abs_deadline(&ts, ms);
a0d45db8 285 qemu_mutex_lock(&sem->mutex);
79761c66 286 while (sem->count == 0) {
8ab30264
PB
287 if (ms == 0) {
288 rc = false;
289 } else {
290 rc = qemu_cond_timedwait_ts(&sem->cond, &sem->mutex, &ts,
291 __FILE__, __LINE__);
292 }
a0d45db8 293 if (!rc) { /* timeout */
c166cb72
PB
294 break;
295 }
c166cb72 296 }
a0d45db8 297 if (rc) {
79761c66
IT
298 --sem->count;
299 }
a0d45db8
LM
300 qemu_mutex_unlock(&sem->mutex);
301 return (rc ? 0 : -1);
38b14db3
PB
302}
303
304void qemu_sem_wait(QemuSemaphore *sem)
305{
a0d45db8 306 qemu_mutex_lock(&sem->mutex);
79761c66 307 while (sem->count == 0) {
a0d45db8 308 qemu_cond_wait(&sem->cond, &sem->mutex);
c166cb72 309 }
79761c66 310 --sem->count;
a0d45db8 311 qemu_mutex_unlock(&sem->mutex);
38b14db3
PB
312}
313
c7c4d063 314#ifdef __linux__
fbcc3e50 315#include "qemu/futex.h"
c7c4d063 316#else
fbcc3e50 317static inline void qemu_futex_wake(QemuEvent *ev, int n)
c7c4d063 318{
c096358e 319 assert(ev->initialized);
158ef8cb 320 pthread_mutex_lock(&ev->lock);
c7c4d063
PB
321 if (n == 1) {
322 pthread_cond_signal(&ev->cond);
323 } else {
324 pthread_cond_broadcast(&ev->cond);
325 }
158ef8cb 326 pthread_mutex_unlock(&ev->lock);
c7c4d063
PB
327}
328
fbcc3e50 329static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
c7c4d063 330{
c096358e 331 assert(ev->initialized);
c7c4d063
PB
332 pthread_mutex_lock(&ev->lock);
333 if (ev->value == val) {
334 pthread_cond_wait(&ev->cond, &ev->lock);
335 }
336 pthread_mutex_unlock(&ev->lock);
337}
338#endif
339
340/* Valid transitions:
341 * - free->set, when setting the event
fbcc3e50 342 * - busy->set, when setting the event, followed by qemu_futex_wake
c7c4d063
PB
343 * - set->free, when resetting the event
344 * - free->busy, when waiting
345 *
346 * set->busy does not happen (it can be observed from the outside but
347 * it really is set->free->busy).
348 *
349 * busy->free provably cannot happen; to enforce it, the set->free transition
350 * is done with an OR, which becomes a no-op if the event has concurrently
351 * transitioned to free or busy.
352 */
353
354#define EV_SET 0
355#define EV_FREE 1
356#define EV_BUSY -1
357
358void qemu_event_init(QemuEvent *ev, bool init)
359{
360#ifndef __linux__
361 pthread_mutex_init(&ev->lock, NULL);
362 pthread_cond_init(&ev->cond, NULL);
363#endif
364
365 ev->value = (init ? EV_SET : EV_FREE);
c096358e 366 ev->initialized = true;
c7c4d063
PB
367}
368
369void qemu_event_destroy(QemuEvent *ev)
370{
c096358e
FZ
371 assert(ev->initialized);
372 ev->initialized = false;
c7c4d063
PB
373#ifndef __linux__
374 pthread_mutex_destroy(&ev->lock);
375 pthread_cond_destroy(&ev->cond);
376#endif
377}
378
379void qemu_event_set(QemuEvent *ev)
380{
374293ca
PB
381 /* qemu_event_set has release semantics, but because it *loads*
382 * ev->value we need a full memory barrier here.
383 */
c096358e 384 assert(ev->initialized);
374293ca 385 smp_mb();
d73415a3
SH
386 if (qatomic_read(&ev->value) != EV_SET) {
387 if (qatomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
c7c4d063 388 /* There were waiters, wake them up. */
fbcc3e50 389 qemu_futex_wake(ev, INT_MAX);
c7c4d063
PB
390 }
391 }
392}
393
394void qemu_event_reset(QemuEvent *ev)
395{
374293ca
PB
396 unsigned value;
397
c096358e 398 assert(ev->initialized);
d73415a3 399 value = qatomic_read(&ev->value);
374293ca
PB
400 smp_mb_acquire();
401 if (value == EV_SET) {
c7c4d063
PB
402 /*
403 * If there was a concurrent reset (or even reset+wait),
404 * do nothing. Otherwise change EV_SET->EV_FREE.
405 */
d73415a3 406 qatomic_or(&ev->value, EV_FREE);
c7c4d063
PB
407 }
408}
409
410void qemu_event_wait(QemuEvent *ev)
411{
412 unsigned value;
413
c096358e 414 assert(ev->initialized);
d73415a3 415 value = qatomic_read(&ev->value);
374293ca 416 smp_mb_acquire();
c7c4d063
PB
417 if (value != EV_SET) {
418 if (value == EV_FREE) {
419 /*
420 * Leave the event reset and tell qemu_event_set that there
421 * are waiters. No need to retry, because there cannot be
67cc32eb 422 * a concurrent busy->free transition. After the CAS, the
c7c4d063
PB
423 * event will be either set or busy.
424 */
d73415a3 425 if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
c7c4d063
PB
426 return;
427 }
428 }
fbcc3e50 429 qemu_futex_wait(ev, EV_BUSY);
c7c4d063
PB
430 }
431}
432
a458774a 433static __thread NotifierList thread_exit;
ef57137f 434
a458774a
PM
435/*
436 * Note that in this implementation you can register a thread-exit
437 * notifier for the main thread, but it will never be called.
438 * This is OK because main thread exit can only happen when the
439 * entire process is exiting, and the API allows notifiers to not
440 * be called on process exit.
441 */
ef57137f
PB
442void qemu_thread_atexit_add(Notifier *notifier)
443{
a458774a 444 notifier_list_add(&thread_exit, notifier);
ef57137f
PB
445}
446
447void qemu_thread_atexit_remove(Notifier *notifier)
448{
ef57137f 449 notifier_remove(notifier);
ef57137f
PB
450}
451
a458774a 452static void qemu_thread_atexit_notify(void *arg)
ef57137f 453{
a458774a
PM
454 /*
455 * Called when non-main thread exits (via qemu_thread_exit()
456 * or by returning from its start routine.)
457 */
458 notifier_list_notify(&thread_exit, NULL);
ef57137f
PB
459}
460
68a93982 461typedef struct {
462 void *(*start_routine)(void *);
463 void *arg;
464 char *name;
465} QemuThreadArgs;
466
467static void *qemu_thread_start(void *args)
468{
469 QemuThreadArgs *qemu_thread_args = args;
470 void *(*start_routine)(void *) = qemu_thread_args->start_routine;
471 void *arg = qemu_thread_args->arg;
a458774a 472 void *r;
68a93982 473
474 /* Attempt to set the threads name; note that this is for debug, so
475 * we're not going to fail if we can't set it.
476 */
d820fa5b 477 if (name_threads && qemu_thread_args->name) {
479a5747 478# if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
d820fa5b 479 pthread_setname_np(pthread_self(), qemu_thread_args->name);
479a5747
RB
480# elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
481 pthread_setname_np(qemu_thread_args->name);
482# endif
d820fa5b 483 }
ce9f0e5b 484 QEMU_TSAN_ANNOTATE_THREAD_NAME(qemu_thread_args->name);
68a93982 485 g_free(qemu_thread_args->name);
486 g_free(qemu_thread_args);
37daf1ba
RH
487
488 /*
489 * GCC 11 with glibc 2.17 on PowerPC reports
490 *
491 * qemu-thread-posix.c:540:5: error: ‘__sigsetjmp’ accessing 656 bytes
492 * in a region of size 528 [-Werror=stringop-overflow=]
493 * 540 | pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
494 * | ^~~~~~~~~~~~~~~~~~~~
495 *
496 * which is clearly nonsense.
497 */
498#pragma GCC diagnostic push
499#ifndef __clang__
500#pragma GCC diagnostic ignored "-Wstringop-overflow"
501#endif
502
a458774a
PM
503 pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
504 r = start_routine(arg);
505 pthread_cleanup_pop(1);
37daf1ba
RH
506
507#pragma GCC diagnostic pop
508
a458774a 509 return r;
5c312079
DDAG
510}
511
4900116e 512void qemu_thread_create(QemuThread *thread, const char *name,
e5d355d1 513 void *(*start_routine)(void*),
cf218714 514 void *arg, int mode)
e5d355d1 515{
cf218714 516 sigset_t set, oldset;
e5d355d1 517 int err;
8763046b 518 pthread_attr_t attr;
d820fa5b 519 QemuThreadArgs *qemu_thread_args;
e5d355d1 520
8763046b
JK
521 err = pthread_attr_init(&attr);
522 if (err) {
523 error_exit(err, __func__);
524 }
55541c8a 525
68a93982 526 if (mode == QEMU_THREAD_DETACHED) {
527 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
528 }
529
cf218714 530 /* Leave signal handling to the iothread. */
55541c8a 531 sigfillset(&set);
21a43af0
RB
532 /* Blocking the signals can result in undefined behaviour. */
533 sigdelset(&set, SIGSEGV);
534 sigdelset(&set, SIGFPE);
535 sigdelset(&set, SIGILL);
536 /* TODO avoid SIGBUS loss on macOS */
55541c8a 537 pthread_sigmask(SIG_SETMASK, &set, &oldset);
55541c8a 538
d820fa5b
PX
539 qemu_thread_args = g_new0(QemuThreadArgs, 1);
540 qemu_thread_args->name = g_strdup(name);
541 qemu_thread_args->start_routine = start_routine;
542 qemu_thread_args->arg = arg;
543
544 err = pthread_create(&thread->thread, &attr,
545 qemu_thread_start, qemu_thread_args);
4900116e 546
68a93982 547 if (err)
548 error_exit(err, __func__);
549
55541c8a 550 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
8763046b
JK
551
552 pthread_attr_destroy(&attr);
e5d355d1
AL
553}
554
b7680cb6 555void qemu_thread_get_self(QemuThread *thread)
e5d355d1
AL
556{
557 thread->thread = pthread_self();
558}
559
2d797b65 560bool qemu_thread_is_self(QemuThread *thread)
e5d355d1 561{
b7680cb6 562 return pthread_equal(pthread_self(), thread->thread);
e5d355d1
AL
563}
564
313b1d69
CC
565void qemu_thread_exit(void *retval)
566{
567 pthread_exit(retval);
568}
8763046b
JK
569
570void *qemu_thread_join(QemuThread *thread)
571{
572 int err;
573 void *ret;
574
575 err = pthread_join(thread->thread, &ret);
576 if (err) {
577 error_exit(err, __func__);
578 }
579 return ret;
580}