]>
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 <[email protected]> | |
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 | */ | |
13 | #include <stdlib.h> | |
14 | #include <stdio.h> | |
15 | #include <errno.h> | |
16 | #include <time.h> | |
17 | #include <signal.h> | |
18 | #include <stdint.h> | |
19 | #include <string.h> | |
38b14db3 PB |
20 | #include <limits.h> |
21 | #include <unistd.h> | |
22 | #include <sys/time.h> | |
c7c4d063 PB |
23 | #ifdef __linux__ |
24 | #include <sys/syscall.h> | |
25 | #include <linux/futex.h> | |
26 | #endif | |
1de7afc9 | 27 | #include "qemu/thread.h" |
c7c4d063 | 28 | #include "qemu/atomic.h" |
ef57137f | 29 | #include "qemu/notify.h" |
e5d355d1 | 30 | |
8f480de0 DDAG |
31 | static bool name_threads; |
32 | ||
33 | void qemu_thread_naming(bool enable) | |
34 | { | |
35 | name_threads = enable; | |
5c312079 DDAG |
36 | |
37 | #ifndef CONFIG_THREAD_SETNAME_BYTHREAD | |
38 | /* This is a debugging option, not fatal */ | |
39 | if (enable) { | |
40 | fprintf(stderr, "qemu: thread naming not supported on this host\n"); | |
41 | } | |
42 | #endif | |
8f480de0 DDAG |
43 | } |
44 | ||
e5d355d1 AL |
45 | static void error_exit(int err, const char *msg) |
46 | { | |
47 | fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err)); | |
53380ac3 | 48 | abort(); |
e5d355d1 AL |
49 | } |
50 | ||
51 | void qemu_mutex_init(QemuMutex *mutex) | |
52 | { | |
53 | int err; | |
54 | ||
24fa9049 | 55 | err = pthread_mutex_init(&mutex->lock, NULL); |
e5d355d1 AL |
56 | if (err) |
57 | error_exit(err, __func__); | |
58 | } | |
59 | ||
313b1d69 CC |
60 | void qemu_mutex_destroy(QemuMutex *mutex) |
61 | { | |
62 | int err; | |
63 | ||
64 | err = pthread_mutex_destroy(&mutex->lock); | |
65 | if (err) | |
66 | error_exit(err, __func__); | |
67 | } | |
68 | ||
e5d355d1 AL |
69 | void qemu_mutex_lock(QemuMutex *mutex) |
70 | { | |
71 | int err; | |
72 | ||
73 | err = pthread_mutex_lock(&mutex->lock); | |
74 | if (err) | |
75 | error_exit(err, __func__); | |
76 | } | |
77 | ||
78 | int qemu_mutex_trylock(QemuMutex *mutex) | |
79 | { | |
80 | return pthread_mutex_trylock(&mutex->lock); | |
81 | } | |
82 | ||
e5d355d1 AL |
83 | void qemu_mutex_unlock(QemuMutex *mutex) |
84 | { | |
85 | int err; | |
86 | ||
87 | err = pthread_mutex_unlock(&mutex->lock); | |
88 | if (err) | |
89 | error_exit(err, __func__); | |
90 | } | |
91 | ||
92 | void qemu_cond_init(QemuCond *cond) | |
93 | { | |
94 | int err; | |
95 | ||
96 | err = pthread_cond_init(&cond->cond, NULL); | |
97 | if (err) | |
98 | error_exit(err, __func__); | |
99 | } | |
100 | ||
313b1d69 CC |
101 | void qemu_cond_destroy(QemuCond *cond) |
102 | { | |
103 | int err; | |
104 | ||
105 | err = pthread_cond_destroy(&cond->cond); | |
106 | if (err) | |
107 | error_exit(err, __func__); | |
108 | } | |
109 | ||
e5d355d1 AL |
110 | void qemu_cond_signal(QemuCond *cond) |
111 | { | |
112 | int err; | |
113 | ||
114 | err = pthread_cond_signal(&cond->cond); | |
115 | if (err) | |
116 | error_exit(err, __func__); | |
117 | } | |
118 | ||
119 | void qemu_cond_broadcast(QemuCond *cond) | |
120 | { | |
121 | int err; | |
122 | ||
123 | err = pthread_cond_broadcast(&cond->cond); | |
124 | if (err) | |
125 | error_exit(err, __func__); | |
126 | } | |
127 | ||
128 | void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) | |
129 | { | |
130 | int err; | |
131 | ||
132 | err = pthread_cond_wait(&cond->cond, &mutex->lock); | |
133 | if (err) | |
134 | error_exit(err, __func__); | |
135 | } | |
136 | ||
38b14db3 PB |
137 | void qemu_sem_init(QemuSemaphore *sem, int init) |
138 | { | |
139 | int rc; | |
140 | ||
927fa909 | 141 | #if defined(__APPLE__) || defined(__NetBSD__) |
c166cb72 PB |
142 | rc = pthread_mutex_init(&sem->lock, NULL); |
143 | if (rc != 0) { | |
144 | error_exit(rc, __func__); | |
145 | } | |
146 | rc = pthread_cond_init(&sem->cond, NULL); | |
147 | if (rc != 0) { | |
148 | error_exit(rc, __func__); | |
149 | } | |
150 | if (init < 0) { | |
151 | error_exit(EINVAL, __func__); | |
152 | } | |
153 | sem->count = init; | |
154 | #else | |
38b14db3 PB |
155 | rc = sem_init(&sem->sem, 0, init); |
156 | if (rc < 0) { | |
157 | error_exit(errno, __func__); | |
158 | } | |
c166cb72 | 159 | #endif |
38b14db3 PB |
160 | } |
161 | ||
162 | void qemu_sem_destroy(QemuSemaphore *sem) | |
163 | { | |
164 | int rc; | |
165 | ||
927fa909 | 166 | #if defined(__APPLE__) || defined(__NetBSD__) |
c166cb72 PB |
167 | rc = pthread_cond_destroy(&sem->cond); |
168 | if (rc < 0) { | |
169 | error_exit(rc, __func__); | |
170 | } | |
171 | rc = pthread_mutex_destroy(&sem->lock); | |
172 | if (rc < 0) { | |
173 | error_exit(rc, __func__); | |
174 | } | |
175 | #else | |
38b14db3 PB |
176 | rc = sem_destroy(&sem->sem); |
177 | if (rc < 0) { | |
178 | error_exit(errno, __func__); | |
179 | } | |
c166cb72 | 180 | #endif |
38b14db3 PB |
181 | } |
182 | ||
183 | void qemu_sem_post(QemuSemaphore *sem) | |
184 | { | |
185 | int rc; | |
186 | ||
927fa909 | 187 | #if defined(__APPLE__) || defined(__NetBSD__) |
c166cb72 | 188 | pthread_mutex_lock(&sem->lock); |
79761c66 | 189 | if (sem->count == UINT_MAX) { |
c166cb72 | 190 | rc = EINVAL; |
c166cb72 | 191 | } else { |
79761c66 IT |
192 | sem->count++; |
193 | rc = pthread_cond_signal(&sem->cond); | |
c166cb72 PB |
194 | } |
195 | pthread_mutex_unlock(&sem->lock); | |
196 | if (rc != 0) { | |
197 | error_exit(rc, __func__); | |
198 | } | |
199 | #else | |
38b14db3 PB |
200 | rc = sem_post(&sem->sem); |
201 | if (rc < 0) { | |
202 | error_exit(errno, __func__); | |
203 | } | |
c166cb72 PB |
204 | #endif |
205 | } | |
206 | ||
207 | static void compute_abs_deadline(struct timespec *ts, int ms) | |
208 | { | |
209 | struct timeval tv; | |
210 | gettimeofday(&tv, NULL); | |
211 | ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000; | |
212 | ts->tv_sec = tv.tv_sec + ms / 1000; | |
213 | if (ts->tv_nsec >= 1000000000) { | |
214 | ts->tv_sec++; | |
215 | ts->tv_nsec -= 1000000000; | |
216 | } | |
38b14db3 PB |
217 | } |
218 | ||
219 | int qemu_sem_timedwait(QemuSemaphore *sem, int ms) | |
220 | { | |
221 | int rc; | |
c166cb72 PB |
222 | struct timespec ts; |
223 | ||
927fa909 | 224 | #if defined(__APPLE__) || defined(__NetBSD__) |
79761c66 | 225 | rc = 0; |
c166cb72 PB |
226 | compute_abs_deadline(&ts, ms); |
227 | pthread_mutex_lock(&sem->lock); | |
79761c66 | 228 | while (sem->count == 0) { |
c166cb72 PB |
229 | rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts); |
230 | if (rc == ETIMEDOUT) { | |
231 | break; | |
232 | } | |
233 | if (rc != 0) { | |
234 | error_exit(rc, __func__); | |
235 | } | |
236 | } | |
79761c66 IT |
237 | if (rc != ETIMEDOUT) { |
238 | --sem->count; | |
239 | } | |
c166cb72 PB |
240 | pthread_mutex_unlock(&sem->lock); |
241 | return (rc == ETIMEDOUT ? -1 : 0); | |
242 | #else | |
38b14db3 PB |
243 | if (ms <= 0) { |
244 | /* This is cheaper than sem_timedwait. */ | |
245 | do { | |
246 | rc = sem_trywait(&sem->sem); | |
247 | } while (rc == -1 && errno == EINTR); | |
248 | if (rc == -1 && errno == EAGAIN) { | |
249 | return -1; | |
250 | } | |
251 | } else { | |
c166cb72 | 252 | compute_abs_deadline(&ts, ms); |
38b14db3 PB |
253 | do { |
254 | rc = sem_timedwait(&sem->sem, &ts); | |
255 | } while (rc == -1 && errno == EINTR); | |
256 | if (rc == -1 && errno == ETIMEDOUT) { | |
257 | return -1; | |
258 | } | |
259 | } | |
260 | if (rc < 0) { | |
261 | error_exit(errno, __func__); | |
262 | } | |
263 | return 0; | |
c166cb72 | 264 | #endif |
38b14db3 PB |
265 | } |
266 | ||
267 | void qemu_sem_wait(QemuSemaphore *sem) | |
268 | { | |
79761c66 IT |
269 | int rc; |
270 | ||
927fa909 | 271 | #if defined(__APPLE__) || defined(__NetBSD__) |
c166cb72 | 272 | pthread_mutex_lock(&sem->lock); |
79761c66 IT |
273 | while (sem->count == 0) { |
274 | rc = pthread_cond_wait(&sem->cond, &sem->lock); | |
275 | if (rc != 0) { | |
276 | error_exit(rc, __func__); | |
277 | } | |
c166cb72 | 278 | } |
79761c66 | 279 | --sem->count; |
c166cb72 PB |
280 | pthread_mutex_unlock(&sem->lock); |
281 | #else | |
38b14db3 PB |
282 | do { |
283 | rc = sem_wait(&sem->sem); | |
284 | } while (rc == -1 && errno == EINTR); | |
285 | if (rc < 0) { | |
286 | error_exit(errno, __func__); | |
287 | } | |
c166cb72 | 288 | #endif |
38b14db3 PB |
289 | } |
290 | ||
c7c4d063 PB |
291 | #ifdef __linux__ |
292 | #define futex(...) syscall(__NR_futex, __VA_ARGS__) | |
293 | ||
294 | static inline void futex_wake(QemuEvent *ev, int n) | |
295 | { | |
296 | futex(ev, FUTEX_WAKE, n, NULL, NULL, 0); | |
297 | } | |
298 | ||
299 | static inline void futex_wait(QemuEvent *ev, unsigned val) | |
300 | { | |
16ef9d02 EC |
301 | while (futex(ev, FUTEX_WAIT, (int) val, NULL, NULL, 0)) { |
302 | switch (errno) { | |
303 | case EWOULDBLOCK: | |
304 | return; | |
305 | case EINTR: | |
306 | break; /* get out of switch and retry */ | |
307 | default: | |
308 | abort(); | |
309 | } | |
310 | } | |
c7c4d063 PB |
311 | } |
312 | #else | |
313 | static inline void futex_wake(QemuEvent *ev, int n) | |
314 | { | |
158ef8cb | 315 | pthread_mutex_lock(&ev->lock); |
c7c4d063 PB |
316 | if (n == 1) { |
317 | pthread_cond_signal(&ev->cond); | |
318 | } else { | |
319 | pthread_cond_broadcast(&ev->cond); | |
320 | } | |
158ef8cb | 321 | pthread_mutex_unlock(&ev->lock); |
c7c4d063 PB |
322 | } |
323 | ||
324 | static inline void futex_wait(QemuEvent *ev, unsigned val) | |
325 | { | |
326 | pthread_mutex_lock(&ev->lock); | |
327 | if (ev->value == val) { | |
328 | pthread_cond_wait(&ev->cond, &ev->lock); | |
329 | } | |
330 | pthread_mutex_unlock(&ev->lock); | |
331 | } | |
332 | #endif | |
333 | ||
334 | /* Valid transitions: | |
335 | * - free->set, when setting the event | |
336 | * - busy->set, when setting the event, followed by futex_wake | |
337 | * - set->free, when resetting the event | |
338 | * - free->busy, when waiting | |
339 | * | |
340 | * set->busy does not happen (it can be observed from the outside but | |
341 | * it really is set->free->busy). | |
342 | * | |
343 | * busy->free provably cannot happen; to enforce it, the set->free transition | |
344 | * is done with an OR, which becomes a no-op if the event has concurrently | |
345 | * transitioned to free or busy. | |
346 | */ | |
347 | ||
348 | #define EV_SET 0 | |
349 | #define EV_FREE 1 | |
350 | #define EV_BUSY -1 | |
351 | ||
352 | void qemu_event_init(QemuEvent *ev, bool init) | |
353 | { | |
354 | #ifndef __linux__ | |
355 | pthread_mutex_init(&ev->lock, NULL); | |
356 | pthread_cond_init(&ev->cond, NULL); | |
357 | #endif | |
358 | ||
359 | ev->value = (init ? EV_SET : EV_FREE); | |
360 | } | |
361 | ||
362 | void qemu_event_destroy(QemuEvent *ev) | |
363 | { | |
364 | #ifndef __linux__ | |
365 | pthread_mutex_destroy(&ev->lock); | |
366 | pthread_cond_destroy(&ev->cond); | |
367 | #endif | |
368 | } | |
369 | ||
370 | void qemu_event_set(QemuEvent *ev) | |
371 | { | |
372 | if (atomic_mb_read(&ev->value) != EV_SET) { | |
373 | if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) { | |
374 | /* There were waiters, wake them up. */ | |
375 | futex_wake(ev, INT_MAX); | |
376 | } | |
377 | } | |
378 | } | |
379 | ||
380 | void qemu_event_reset(QemuEvent *ev) | |
381 | { | |
382 | if (atomic_mb_read(&ev->value) == EV_SET) { | |
383 | /* | |
384 | * If there was a concurrent reset (or even reset+wait), | |
385 | * do nothing. Otherwise change EV_SET->EV_FREE. | |
386 | */ | |
387 | atomic_or(&ev->value, EV_FREE); | |
388 | } | |
389 | } | |
390 | ||
391 | void qemu_event_wait(QemuEvent *ev) | |
392 | { | |
393 | unsigned value; | |
394 | ||
395 | value = atomic_mb_read(&ev->value); | |
396 | if (value != EV_SET) { | |
397 | if (value == EV_FREE) { | |
398 | /* | |
399 | * Leave the event reset and tell qemu_event_set that there | |
400 | * are waiters. No need to retry, because there cannot be | |
67cc32eb | 401 | * a concurrent busy->free transition. After the CAS, the |
c7c4d063 PB |
402 | * event will be either set or busy. |
403 | */ | |
404 | if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) { | |
405 | return; | |
406 | } | |
407 | } | |
408 | futex_wait(ev, EV_BUSY); | |
409 | } | |
410 | } | |
411 | ||
ef57137f PB |
412 | static pthread_key_t exit_key; |
413 | ||
414 | union NotifierThreadData { | |
415 | void *ptr; | |
416 | NotifierList list; | |
417 | }; | |
418 | QEMU_BUILD_BUG_ON(sizeof(union NotifierThreadData) != sizeof(void *)); | |
419 | ||
420 | void qemu_thread_atexit_add(Notifier *notifier) | |
421 | { | |
422 | union NotifierThreadData ntd; | |
423 | ntd.ptr = pthread_getspecific(exit_key); | |
424 | notifier_list_add(&ntd.list, notifier); | |
425 | pthread_setspecific(exit_key, ntd.ptr); | |
426 | } | |
427 | ||
428 | void qemu_thread_atexit_remove(Notifier *notifier) | |
429 | { | |
430 | union NotifierThreadData ntd; | |
431 | ntd.ptr = pthread_getspecific(exit_key); | |
432 | notifier_remove(notifier); | |
433 | pthread_setspecific(exit_key, ntd.ptr); | |
434 | } | |
435 | ||
436 | static void qemu_thread_atexit_run(void *arg) | |
437 | { | |
438 | union NotifierThreadData ntd = { .ptr = arg }; | |
439 | notifier_list_notify(&ntd.list, NULL); | |
440 | } | |
441 | ||
442 | static void __attribute__((constructor)) qemu_thread_atexit_init(void) | |
443 | { | |
444 | pthread_key_create(&exit_key, qemu_thread_atexit_run); | |
445 | } | |
446 | ||
447 | ||
5c312079 DDAG |
448 | /* Attempt to set the threads name; note that this is for debug, so |
449 | * we're not going to fail if we can't set it. | |
450 | */ | |
451 | static void qemu_thread_set_name(QemuThread *thread, const char *name) | |
452 | { | |
453 | #ifdef CONFIG_PTHREAD_SETNAME_NP | |
454 | pthread_setname_np(thread->thread, name); | |
455 | #endif | |
456 | } | |
457 | ||
4900116e | 458 | void qemu_thread_create(QemuThread *thread, const char *name, |
e5d355d1 | 459 | void *(*start_routine)(void*), |
cf218714 | 460 | void *arg, int mode) |
e5d355d1 | 461 | { |
cf218714 | 462 | sigset_t set, oldset; |
e5d355d1 | 463 | int err; |
8763046b | 464 | pthread_attr_t attr; |
e5d355d1 | 465 | |
8763046b JK |
466 | err = pthread_attr_init(&attr); |
467 | if (err) { | |
468 | error_exit(err, __func__); | |
469 | } | |
470 | if (mode == QEMU_THREAD_DETACHED) { | |
471 | err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | |
472 | if (err) { | |
473 | error_exit(err, __func__); | |
474 | } | |
475 | } | |
55541c8a | 476 | |
cf218714 | 477 | /* Leave signal handling to the iothread. */ |
55541c8a PB |
478 | sigfillset(&set); |
479 | pthread_sigmask(SIG_SETMASK, &set, &oldset); | |
8763046b | 480 | err = pthread_create(&thread->thread, &attr, start_routine, arg); |
e5d355d1 AL |
481 | if (err) |
482 | error_exit(err, __func__); | |
55541c8a | 483 | |
4900116e | 484 | if (name_threads) { |
5c312079 | 485 | qemu_thread_set_name(thread, name); |
4900116e | 486 | } |
4900116e | 487 | |
55541c8a | 488 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); |
8763046b JK |
489 | |
490 | pthread_attr_destroy(&attr); | |
e5d355d1 AL |
491 | } |
492 | ||
b7680cb6 | 493 | void qemu_thread_get_self(QemuThread *thread) |
e5d355d1 AL |
494 | { |
495 | thread->thread = pthread_self(); | |
496 | } | |
497 | ||
2d797b65 | 498 | bool qemu_thread_is_self(QemuThread *thread) |
e5d355d1 | 499 | { |
b7680cb6 | 500 | return pthread_equal(pthread_self(), thread->thread); |
e5d355d1 AL |
501 | } |
502 | ||
313b1d69 CC |
503 | void qemu_thread_exit(void *retval) |
504 | { | |
505 | pthread_exit(retval); | |
506 | } | |
8763046b JK |
507 | |
508 | void *qemu_thread_join(QemuThread *thread) | |
509 | { | |
510 | int err; | |
511 | void *ret; | |
512 | ||
513 | err = pthread_join(thread->thread, &ret); | |
514 | if (err) { | |
515 | error_exit(err, __func__); | |
516 | } | |
517 | return ret; | |
518 | } |