qemu-thread-posix.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. */
  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"
  18. static bool name_threads;
  19. void qemu_thread_naming(bool enable)
  20. {
  21. name_threads = enable;
  22. #ifndef CONFIG_THREAD_SETNAME_BYTHREAD
  23. /* This is a debugging option, not fatal */
  24. if (enable) {
  25. fprintf(stderr, "qemu: thread naming not supported on this host\n");
  26. }
  27. #endif
  28. }
  29. static void error_exit(int err, const char *msg)
  30. {
  31. fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
  32. abort();
  33. }
  34. static void compute_abs_deadline(struct timespec *ts, int ms)
  35. {
  36. struct timeval tv;
  37. gettimeofday(&tv, NULL);
  38. ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
  39. ts->tv_sec = tv.tv_sec + ms / 1000;
  40. if (ts->tv_nsec >= 1000000000) {
  41. ts->tv_sec++;
  42. ts->tv_nsec -= 1000000000;
  43. }
  44. }
  45. void qemu_mutex_init(QemuMutex *mutex)
  46. {
  47. int err;
  48. err = pthread_mutex_init(&mutex->lock, NULL);
  49. if (err)
  50. error_exit(err, __func__);
  51. qemu_mutex_post_init(mutex);
  52. }
  53. void qemu_mutex_destroy(QemuMutex *mutex)
  54. {
  55. int err;
  56. assert(mutex->initialized);
  57. mutex->initialized = false;
  58. err = pthread_mutex_destroy(&mutex->lock);
  59. if (err)
  60. error_exit(err, __func__);
  61. }
  62. void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
  63. {
  64. int err;
  65. assert(mutex->initialized);
  66. qemu_mutex_pre_lock(mutex, file, line);
  67. err = pthread_mutex_lock(&mutex->lock);
  68. if (err)
  69. error_exit(err, __func__);
  70. qemu_mutex_post_lock(mutex, file, line);
  71. }
  72. int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
  73. {
  74. int err;
  75. assert(mutex->initialized);
  76. err = pthread_mutex_trylock(&mutex->lock);
  77. if (err == 0) {
  78. qemu_mutex_post_lock(mutex, file, line);
  79. return 0;
  80. }
  81. if (err != EBUSY) {
  82. error_exit(err, __func__);
  83. }
  84. return -EBUSY;
  85. }
  86. void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
  87. {
  88. int err;
  89. assert(mutex->initialized);
  90. qemu_mutex_pre_unlock(mutex, file, line);
  91. err = pthread_mutex_unlock(&mutex->lock);
  92. if (err)
  93. error_exit(err, __func__);
  94. }
  95. void qemu_rec_mutex_init(QemuRecMutex *mutex)
  96. {
  97. int err;
  98. pthread_mutexattr_t attr;
  99. pthread_mutexattr_init(&attr);
  100. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  101. err = pthread_mutex_init(&mutex->lock, &attr);
  102. pthread_mutexattr_destroy(&attr);
  103. if (err) {
  104. error_exit(err, __func__);
  105. }
  106. mutex->initialized = true;
  107. }
  108. void qemu_cond_init(QemuCond *cond)
  109. {
  110. int err;
  111. err = pthread_cond_init(&cond->cond, NULL);
  112. if (err)
  113. error_exit(err, __func__);
  114. cond->initialized = true;
  115. }
  116. void qemu_cond_destroy(QemuCond *cond)
  117. {
  118. int err;
  119. assert(cond->initialized);
  120. cond->initialized = false;
  121. err = pthread_cond_destroy(&cond->cond);
  122. if (err)
  123. error_exit(err, __func__);
  124. }
  125. void qemu_cond_signal(QemuCond *cond)
  126. {
  127. int err;
  128. assert(cond->initialized);
  129. err = pthread_cond_signal(&cond->cond);
  130. if (err)
  131. error_exit(err, __func__);
  132. }
  133. void qemu_cond_broadcast(QemuCond *cond)
  134. {
  135. int err;
  136. assert(cond->initialized);
  137. err = pthread_cond_broadcast(&cond->cond);
  138. if (err)
  139. error_exit(err, __func__);
  140. }
  141. void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
  142. {
  143. int err;
  144. assert(cond->initialized);
  145. qemu_mutex_pre_unlock(mutex, file, line);
  146. err = pthread_cond_wait(&cond->cond, &mutex->lock);
  147. qemu_mutex_post_lock(mutex, file, line);
  148. if (err)
  149. error_exit(err, __func__);
  150. }
  151. bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms,
  152. const char *file, const int line)
  153. {
  154. int err;
  155. struct timespec ts;
  156. assert(cond->initialized);
  157. trace_qemu_mutex_unlock(mutex, file, line);
  158. compute_abs_deadline(&ts, ms);
  159. err = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
  160. trace_qemu_mutex_locked(mutex, file, line);
  161. if (err && err != ETIMEDOUT) {
  162. error_exit(err, __func__);
  163. }
  164. return err != ETIMEDOUT;
  165. }
  166. void qemu_sem_init(QemuSemaphore *sem, int init)
  167. {
  168. int rc;
  169. #ifndef CONFIG_SEM_TIMEDWAIT
  170. rc = pthread_mutex_init(&sem->lock, NULL);
  171. if (rc != 0) {
  172. error_exit(rc, __func__);
  173. }
  174. rc = pthread_cond_init(&sem->cond, NULL);
  175. if (rc != 0) {
  176. error_exit(rc, __func__);
  177. }
  178. if (init < 0) {
  179. error_exit(EINVAL, __func__);
  180. }
  181. sem->count = init;
  182. #else
  183. rc = sem_init(&sem->sem, 0, init);
  184. if (rc < 0) {
  185. error_exit(errno, __func__);
  186. }
  187. #endif
  188. sem->initialized = true;
  189. }
  190. void qemu_sem_destroy(QemuSemaphore *sem)
  191. {
  192. int rc;
  193. assert(sem->initialized);
  194. sem->initialized = false;
  195. #ifndef CONFIG_SEM_TIMEDWAIT
  196. rc = pthread_cond_destroy(&sem->cond);
  197. if (rc < 0) {
  198. error_exit(rc, __func__);
  199. }
  200. rc = pthread_mutex_destroy(&sem->lock);
  201. if (rc < 0) {
  202. error_exit(rc, __func__);
  203. }
  204. #else
  205. rc = sem_destroy(&sem->sem);
  206. if (rc < 0) {
  207. error_exit(errno, __func__);
  208. }
  209. #endif
  210. }
  211. void qemu_sem_post(QemuSemaphore *sem)
  212. {
  213. int rc;
  214. assert(sem->initialized);
  215. #ifndef CONFIG_SEM_TIMEDWAIT
  216. pthread_mutex_lock(&sem->lock);
  217. if (sem->count == UINT_MAX) {
  218. rc = EINVAL;
  219. } else {
  220. sem->count++;
  221. rc = pthread_cond_signal(&sem->cond);
  222. }
  223. pthread_mutex_unlock(&sem->lock);
  224. if (rc != 0) {
  225. error_exit(rc, __func__);
  226. }
  227. #else
  228. rc = sem_post(&sem->sem);
  229. if (rc < 0) {
  230. error_exit(errno, __func__);
  231. }
  232. #endif
  233. }
  234. int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
  235. {
  236. int rc;
  237. struct timespec ts;
  238. assert(sem->initialized);
  239. #ifndef CONFIG_SEM_TIMEDWAIT
  240. rc = 0;
  241. compute_abs_deadline(&ts, ms);
  242. pthread_mutex_lock(&sem->lock);
  243. while (sem->count == 0) {
  244. rc = pthread_cond_timedwait(&sem->cond, &sem->lock, &ts);
  245. if (rc == ETIMEDOUT) {
  246. break;
  247. }
  248. if (rc != 0) {
  249. error_exit(rc, __func__);
  250. }
  251. }
  252. if (rc != ETIMEDOUT) {
  253. --sem->count;
  254. }
  255. pthread_mutex_unlock(&sem->lock);
  256. return (rc == ETIMEDOUT ? -1 : 0);
  257. #else
  258. if (ms <= 0) {
  259. /* This is cheaper than sem_timedwait. */
  260. do {
  261. rc = sem_trywait(&sem->sem);
  262. } while (rc == -1 && errno == EINTR);
  263. if (rc == -1 && errno == EAGAIN) {
  264. return -1;
  265. }
  266. } else {
  267. compute_abs_deadline(&ts, ms);
  268. do {
  269. rc = sem_timedwait(&sem->sem, &ts);
  270. } while (rc == -1 && errno == EINTR);
  271. if (rc == -1 && errno == ETIMEDOUT) {
  272. return -1;
  273. }
  274. }
  275. if (rc < 0) {
  276. error_exit(errno, __func__);
  277. }
  278. return 0;
  279. #endif
  280. }
  281. void qemu_sem_wait(QemuSemaphore *sem)
  282. {
  283. int rc;
  284. assert(sem->initialized);
  285. #ifndef CONFIG_SEM_TIMEDWAIT
  286. pthread_mutex_lock(&sem->lock);
  287. while (sem->count == 0) {
  288. rc = pthread_cond_wait(&sem->cond, &sem->lock);
  289. if (rc != 0) {
  290. error_exit(rc, __func__);
  291. }
  292. }
  293. --sem->count;
  294. pthread_mutex_unlock(&sem->lock);
  295. #else
  296. do {
  297. rc = sem_wait(&sem->sem);
  298. } while (rc == -1 && errno == EINTR);
  299. if (rc < 0) {
  300. error_exit(errno, __func__);
  301. }
  302. #endif
  303. }
  304. #ifdef __linux__
  305. #include "qemu/futex.h"
  306. #else
  307. static inline void qemu_futex_wake(QemuEvent *ev, int n)
  308. {
  309. assert(ev->initialized);
  310. pthread_mutex_lock(&ev->lock);
  311. if (n == 1) {
  312. pthread_cond_signal(&ev->cond);
  313. } else {
  314. pthread_cond_broadcast(&ev->cond);
  315. }
  316. pthread_mutex_unlock(&ev->lock);
  317. }
  318. static inline void qemu_futex_wait(QemuEvent *ev, unsigned val)
  319. {
  320. assert(ev->initialized);
  321. pthread_mutex_lock(&ev->lock);
  322. if (ev->value == val) {
  323. pthread_cond_wait(&ev->cond, &ev->lock);
  324. }
  325. pthread_mutex_unlock(&ev->lock);
  326. }
  327. #endif
  328. /* Valid transitions:
  329. * - free->set, when setting the event
  330. * - busy->set, when setting the event, followed by qemu_futex_wake
  331. * - set->free, when resetting the event
  332. * - free->busy, when waiting
  333. *
  334. * set->busy does not happen (it can be observed from the outside but
  335. * it really is set->free->busy).
  336. *
  337. * busy->free provably cannot happen; to enforce it, the set->free transition
  338. * is done with an OR, which becomes a no-op if the event has concurrently
  339. * transitioned to free or busy.
  340. */
  341. #define EV_SET 0
  342. #define EV_FREE 1
  343. #define EV_BUSY -1
  344. void qemu_event_init(QemuEvent *ev, bool init)
  345. {
  346. #ifndef __linux__
  347. pthread_mutex_init(&ev->lock, NULL);
  348. pthread_cond_init(&ev->cond, NULL);
  349. #endif
  350. ev->value = (init ? EV_SET : EV_FREE);
  351. ev->initialized = true;
  352. }
  353. void qemu_event_destroy(QemuEvent *ev)
  354. {
  355. assert(ev->initialized);
  356. ev->initialized = false;
  357. #ifndef __linux__
  358. pthread_mutex_destroy(&ev->lock);
  359. pthread_cond_destroy(&ev->cond);
  360. #endif
  361. }
  362. void qemu_event_set(QemuEvent *ev)
  363. {
  364. /* qemu_event_set has release semantics, but because it *loads*
  365. * ev->value we need a full memory barrier here.
  366. */
  367. assert(ev->initialized);
  368. smp_mb();
  369. if (atomic_read(&ev->value) != EV_SET) {
  370. if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
  371. /* There were waiters, wake them up. */
  372. qemu_futex_wake(ev, INT_MAX);
  373. }
  374. }
  375. }
  376. void qemu_event_reset(QemuEvent *ev)
  377. {
  378. unsigned value;
  379. assert(ev->initialized);
  380. value = atomic_read(&ev->value);
  381. smp_mb_acquire();
  382. if (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. void qemu_event_wait(QemuEvent *ev)
  391. {
  392. unsigned value;
  393. assert(ev->initialized);
  394. value = atomic_read(&ev->value);
  395. smp_mb_acquire();
  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
  401. * a concurrent busy->free transition. After the CAS, the
  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. qemu_futex_wait(ev, EV_BUSY);
  409. }
  410. }
  411. static __thread NotifierList thread_exit;
  412. /*
  413. * Note that in this implementation you can register a thread-exit
  414. * notifier for the main thread, but it will never be called.
  415. * This is OK because main thread exit can only happen when the
  416. * entire process is exiting, and the API allows notifiers to not
  417. * be called on process exit.
  418. */
  419. void qemu_thread_atexit_add(Notifier *notifier)
  420. {
  421. notifier_list_add(&thread_exit, notifier);
  422. }
  423. void qemu_thread_atexit_remove(Notifier *notifier)
  424. {
  425. notifier_remove(notifier);
  426. }
  427. static void qemu_thread_atexit_notify(void *arg)
  428. {
  429. /*
  430. * Called when non-main thread exits (via qemu_thread_exit()
  431. * or by returning from its start routine.)
  432. */
  433. notifier_list_notify(&thread_exit, NULL);
  434. }
  435. typedef struct {
  436. void *(*start_routine)(void *);
  437. void *arg;
  438. char *name;
  439. } QemuThreadArgs;
  440. static void *qemu_thread_start(void *args)
  441. {
  442. QemuThreadArgs *qemu_thread_args = args;
  443. void *(*start_routine)(void *) = qemu_thread_args->start_routine;
  444. void *arg = qemu_thread_args->arg;
  445. void *r;
  446. #ifdef CONFIG_THREAD_SETNAME_BYTHREAD
  447. /* Attempt to set the threads name; note that this is for debug, so
  448. * we're not going to fail if we can't set it.
  449. */
  450. if (name_threads && qemu_thread_args->name) {
  451. # if defined(CONFIG_PTHREAD_SETNAME_NP_W_TID)
  452. pthread_setname_np(pthread_self(), qemu_thread_args->name);
  453. # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
  454. pthread_setname_np(qemu_thread_args->name);
  455. # endif
  456. }
  457. #endif
  458. g_free(qemu_thread_args->name);
  459. g_free(qemu_thread_args);
  460. pthread_cleanup_push(qemu_thread_atexit_notify, NULL);
  461. r = start_routine(arg);
  462. pthread_cleanup_pop(1);
  463. return r;
  464. }
  465. void qemu_thread_create(QemuThread *thread, const char *name,
  466. void *(*start_routine)(void*),
  467. void *arg, int mode)
  468. {
  469. sigset_t set, oldset;
  470. int err;
  471. pthread_attr_t attr;
  472. QemuThreadArgs *qemu_thread_args;
  473. err = pthread_attr_init(&attr);
  474. if (err) {
  475. error_exit(err, __func__);
  476. }
  477. if (mode == QEMU_THREAD_DETACHED) {
  478. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  479. }
  480. /* Leave signal handling to the iothread. */
  481. sigfillset(&set);
  482. /* Blocking the signals can result in undefined behaviour. */
  483. sigdelset(&set, SIGSEGV);
  484. sigdelset(&set, SIGFPE);
  485. sigdelset(&set, SIGILL);
  486. /* TODO avoid SIGBUS loss on macOS */
  487. pthread_sigmask(SIG_SETMASK, &set, &oldset);
  488. qemu_thread_args = g_new0(QemuThreadArgs, 1);
  489. qemu_thread_args->name = g_strdup(name);
  490. qemu_thread_args->start_routine = start_routine;
  491. qemu_thread_args->arg = arg;
  492. err = pthread_create(&thread->thread, &attr,
  493. qemu_thread_start, qemu_thread_args);
  494. if (err)
  495. error_exit(err, __func__);
  496. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  497. pthread_attr_destroy(&attr);
  498. }
  499. void qemu_thread_get_self(QemuThread *thread)
  500. {
  501. thread->thread = pthread_self();
  502. }
  503. bool qemu_thread_is_self(QemuThread *thread)
  504. {
  505. return pthread_equal(pthread_self(), thread->thread);
  506. }
  507. void qemu_thread_exit(void *retval)
  508. {
  509. pthread_exit(retval);
  510. }
  511. void *qemu_thread_join(QemuThread *thread)
  512. {
  513. int err;
  514. void *ret;
  515. err = pthread_join(thread->thread, &ret);
  516. if (err) {
  517. error_exit(err, __func__);
  518. }
  519. return ret;
  520. }