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