qemu-thread-posix.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <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>
  20. #include "qemu-thread.h"
  21. static void error_exit(int err, const char *msg)
  22. {
  23. fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
  24. abort();
  25. }
  26. void qemu_mutex_init(QemuMutex *mutex)
  27. {
  28. int err;
  29. pthread_mutexattr_t mutexattr;
  30. pthread_mutexattr_init(&mutexattr);
  31. pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK);
  32. err = pthread_mutex_init(&mutex->lock, &mutexattr);
  33. pthread_mutexattr_destroy(&mutexattr);
  34. if (err)
  35. error_exit(err, __func__);
  36. }
  37. void qemu_mutex_destroy(QemuMutex *mutex)
  38. {
  39. int err;
  40. err = pthread_mutex_destroy(&mutex->lock);
  41. if (err)
  42. error_exit(err, __func__);
  43. }
  44. void qemu_mutex_lock(QemuMutex *mutex)
  45. {
  46. int err;
  47. err = pthread_mutex_lock(&mutex->lock);
  48. if (err)
  49. error_exit(err, __func__);
  50. }
  51. int qemu_mutex_trylock(QemuMutex *mutex)
  52. {
  53. return pthread_mutex_trylock(&mutex->lock);
  54. }
  55. void qemu_mutex_unlock(QemuMutex *mutex)
  56. {
  57. int err;
  58. err = pthread_mutex_unlock(&mutex->lock);
  59. if (err)
  60. error_exit(err, __func__);
  61. }
  62. void qemu_cond_init(QemuCond *cond)
  63. {
  64. int err;
  65. err = pthread_cond_init(&cond->cond, NULL);
  66. if (err)
  67. error_exit(err, __func__);
  68. }
  69. void qemu_cond_destroy(QemuCond *cond)
  70. {
  71. int err;
  72. err = pthread_cond_destroy(&cond->cond);
  73. if (err)
  74. error_exit(err, __func__);
  75. }
  76. void qemu_cond_signal(QemuCond *cond)
  77. {
  78. int err;
  79. err = pthread_cond_signal(&cond->cond);
  80. if (err)
  81. error_exit(err, __func__);
  82. }
  83. void qemu_cond_broadcast(QemuCond *cond)
  84. {
  85. int err;
  86. err = pthread_cond_broadcast(&cond->cond);
  87. if (err)
  88. error_exit(err, __func__);
  89. }
  90. void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
  91. {
  92. int err;
  93. err = pthread_cond_wait(&cond->cond, &mutex->lock);
  94. if (err)
  95. error_exit(err, __func__);
  96. }
  97. void qemu_thread_create(QemuThread *thread,
  98. void *(*start_routine)(void*),
  99. void *arg)
  100. {
  101. int err;
  102. /* Leave signal handling to the iothread. */
  103. sigset_t set, oldset;
  104. sigfillset(&set);
  105. pthread_sigmask(SIG_SETMASK, &set, &oldset);
  106. err = pthread_create(&thread->thread, NULL, start_routine, arg);
  107. if (err)
  108. error_exit(err, __func__);
  109. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  110. }
  111. void qemu_thread_get_self(QemuThread *thread)
  112. {
  113. thread->thread = pthread_self();
  114. }
  115. int qemu_thread_is_self(QemuThread *thread)
  116. {
  117. return pthread_equal(pthread_self(), thread->thread);
  118. }
  119. void qemu_thread_exit(void *retval)
  120. {
  121. pthread_exit(retval);
  122. }