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