2
0

qemu-thread-win32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Win32 implementation for mutex/cond/thread functions
  3. *
  4. * Copyright Red Hat, Inc. 2010
  5. *
  6. * Author:
  7. * Paolo Bonzini <pbonzini@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-common.h"
  15. #include "qemu/thread.h"
  16. #include "qemu/notify.h"
  17. #include "qemu-thread-common.h"
  18. #include <process.h>
  19. static bool name_threads;
  20. void qemu_thread_naming(bool enable)
  21. {
  22. /* But note we don't actually name them on Windows yet */
  23. name_threads = enable;
  24. fprintf(stderr, "qemu: thread naming not supported on this host\n");
  25. }
  26. static void error_exit(int err, const char *msg)
  27. {
  28. char *pstr;
  29. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  30. NULL, err, 0, (LPTSTR)&pstr, 2, NULL);
  31. fprintf(stderr, "qemu: %s: %s\n", msg, pstr);
  32. LocalFree(pstr);
  33. abort();
  34. }
  35. void qemu_mutex_init(QemuMutex *mutex)
  36. {
  37. InitializeSRWLock(&mutex->lock);
  38. qemu_mutex_post_init(mutex);
  39. }
  40. void qemu_mutex_destroy(QemuMutex *mutex)
  41. {
  42. assert(mutex->initialized);
  43. mutex->initialized = false;
  44. InitializeSRWLock(&mutex->lock);
  45. }
  46. void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
  47. {
  48. assert(mutex->initialized);
  49. qemu_mutex_pre_lock(mutex, file, line);
  50. AcquireSRWLockExclusive(&mutex->lock);
  51. qemu_mutex_post_lock(mutex, file, line);
  52. }
  53. int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
  54. {
  55. int owned;
  56. assert(mutex->initialized);
  57. owned = TryAcquireSRWLockExclusive(&mutex->lock);
  58. if (owned) {
  59. qemu_mutex_post_lock(mutex, file, line);
  60. return 0;
  61. }
  62. return -EBUSY;
  63. }
  64. void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
  65. {
  66. assert(mutex->initialized);
  67. qemu_mutex_pre_unlock(mutex, file, line);
  68. ReleaseSRWLockExclusive(&mutex->lock);
  69. }
  70. void qemu_rec_mutex_init(QemuRecMutex *mutex)
  71. {
  72. InitializeCriticalSection(&mutex->lock);
  73. mutex->initialized = true;
  74. }
  75. void qemu_rec_mutex_destroy(QemuRecMutex *mutex)
  76. {
  77. assert(mutex->initialized);
  78. mutex->initialized = false;
  79. DeleteCriticalSection(&mutex->lock);
  80. }
  81. void qemu_rec_mutex_lock_impl(QemuRecMutex *mutex, const char *file, int line)
  82. {
  83. assert(mutex->initialized);
  84. EnterCriticalSection(&mutex->lock);
  85. }
  86. int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line)
  87. {
  88. assert(mutex->initialized);
  89. return !TryEnterCriticalSection(&mutex->lock);
  90. }
  91. void qemu_rec_mutex_unlock(QemuRecMutex *mutex)
  92. {
  93. assert(mutex->initialized);
  94. LeaveCriticalSection(&mutex->lock);
  95. }
  96. void qemu_cond_init(QemuCond *cond)
  97. {
  98. memset(cond, 0, sizeof(*cond));
  99. InitializeConditionVariable(&cond->var);
  100. cond->initialized = true;
  101. }
  102. void qemu_cond_destroy(QemuCond *cond)
  103. {
  104. assert(cond->initialized);
  105. cond->initialized = false;
  106. InitializeConditionVariable(&cond->var);
  107. }
  108. void qemu_cond_signal(QemuCond *cond)
  109. {
  110. assert(cond->initialized);
  111. WakeConditionVariable(&cond->var);
  112. }
  113. void qemu_cond_broadcast(QemuCond *cond)
  114. {
  115. assert(cond->initialized);
  116. WakeAllConditionVariable(&cond->var);
  117. }
  118. void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
  119. {
  120. assert(cond->initialized);
  121. qemu_mutex_pre_unlock(mutex, file, line);
  122. SleepConditionVariableSRW(&cond->var, &mutex->lock, INFINITE, 0);
  123. qemu_mutex_post_lock(mutex, file, line);
  124. }
  125. bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms,
  126. const char *file, const int line)
  127. {
  128. int rc = 0;
  129. assert(cond->initialized);
  130. trace_qemu_mutex_unlock(mutex, file, line);
  131. if (!SleepConditionVariableSRW(&cond->var, &mutex->lock, ms, 0)) {
  132. rc = GetLastError();
  133. }
  134. trace_qemu_mutex_locked(mutex, file, line);
  135. if (rc && rc != ERROR_TIMEOUT) {
  136. error_exit(rc, __func__);
  137. }
  138. return rc != ERROR_TIMEOUT;
  139. }
  140. void qemu_sem_init(QemuSemaphore *sem, int init)
  141. {
  142. /* Manual reset. */
  143. sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL);
  144. sem->initialized = true;
  145. }
  146. void qemu_sem_destroy(QemuSemaphore *sem)
  147. {
  148. assert(sem->initialized);
  149. sem->initialized = false;
  150. CloseHandle(sem->sema);
  151. }
  152. void qemu_sem_post(QemuSemaphore *sem)
  153. {
  154. assert(sem->initialized);
  155. ReleaseSemaphore(sem->sema, 1, NULL);
  156. }
  157. int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
  158. {
  159. int rc;
  160. assert(sem->initialized);
  161. rc = WaitForSingleObject(sem->sema, ms);
  162. if (rc == WAIT_OBJECT_0) {
  163. return 0;
  164. }
  165. if (rc != WAIT_TIMEOUT) {
  166. error_exit(GetLastError(), __func__);
  167. }
  168. return -1;
  169. }
  170. void qemu_sem_wait(QemuSemaphore *sem)
  171. {
  172. assert(sem->initialized);
  173. if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) {
  174. error_exit(GetLastError(), __func__);
  175. }
  176. }
  177. /* Wrap a Win32 manual-reset event with a fast userspace path. The idea
  178. * is to reset the Win32 event lazily, as part of a test-reset-test-wait
  179. * sequence. Such a sequence is, indeed, how QemuEvents are used by
  180. * RCU and other subsystems!
  181. *
  182. * Valid transitions:
  183. * - free->set, when setting the event
  184. * - busy->set, when setting the event, followed by SetEvent
  185. * - set->free, when resetting the event
  186. * - free->busy, when waiting
  187. *
  188. * set->busy does not happen (it can be observed from the outside but
  189. * it really is set->free->busy).
  190. *
  191. * busy->free provably cannot happen; to enforce it, the set->free transition
  192. * is done with an OR, which becomes a no-op if the event has concurrently
  193. * transitioned to free or busy (and is faster than cmpxchg).
  194. */
  195. #define EV_SET 0
  196. #define EV_FREE 1
  197. #define EV_BUSY -1
  198. void qemu_event_init(QemuEvent *ev, bool init)
  199. {
  200. /* Manual reset. */
  201. ev->event = CreateEvent(NULL, TRUE, TRUE, NULL);
  202. ev->value = (init ? EV_SET : EV_FREE);
  203. ev->initialized = true;
  204. }
  205. void qemu_event_destroy(QemuEvent *ev)
  206. {
  207. assert(ev->initialized);
  208. ev->initialized = false;
  209. CloseHandle(ev->event);
  210. }
  211. void qemu_event_set(QemuEvent *ev)
  212. {
  213. assert(ev->initialized);
  214. /* qemu_event_set has release semantics, but because it *loads*
  215. * ev->value we need a full memory barrier here.
  216. */
  217. smp_mb();
  218. if (atomic_read(&ev->value) != EV_SET) {
  219. if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
  220. /* There were waiters, wake them up. */
  221. SetEvent(ev->event);
  222. }
  223. }
  224. }
  225. void qemu_event_reset(QemuEvent *ev)
  226. {
  227. unsigned value;
  228. assert(ev->initialized);
  229. value = atomic_read(&ev->value);
  230. smp_mb_acquire();
  231. if (value == EV_SET) {
  232. /* If there was a concurrent reset (or even reset+wait),
  233. * do nothing. Otherwise change EV_SET->EV_FREE.
  234. */
  235. atomic_or(&ev->value, EV_FREE);
  236. }
  237. }
  238. void qemu_event_wait(QemuEvent *ev)
  239. {
  240. unsigned value;
  241. assert(ev->initialized);
  242. value = atomic_read(&ev->value);
  243. smp_mb_acquire();
  244. if (value != EV_SET) {
  245. if (value == EV_FREE) {
  246. /* qemu_event_set is not yet going to call SetEvent, but we are
  247. * going to do another check for EV_SET below when setting EV_BUSY.
  248. * At that point it is safe to call WaitForSingleObject.
  249. */
  250. ResetEvent(ev->event);
  251. /* Tell qemu_event_set that there are waiters. No need to retry
  252. * because there cannot be a concurent busy->free transition.
  253. * After the CAS, the event will be either set or busy.
  254. */
  255. if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
  256. value = EV_SET;
  257. } else {
  258. value = EV_BUSY;
  259. }
  260. }
  261. if (value == EV_BUSY) {
  262. WaitForSingleObject(ev->event, INFINITE);
  263. }
  264. }
  265. }
  266. struct QemuThreadData {
  267. /* Passed to win32_start_routine. */
  268. void *(*start_routine)(void *);
  269. void *arg;
  270. short mode;
  271. NotifierList exit;
  272. /* Only used for joinable threads. */
  273. bool exited;
  274. void *ret;
  275. CRITICAL_SECTION cs;
  276. };
  277. static bool atexit_registered;
  278. static NotifierList main_thread_exit;
  279. static __thread QemuThreadData *qemu_thread_data;
  280. static void run_main_thread_exit(void)
  281. {
  282. notifier_list_notify(&main_thread_exit, NULL);
  283. }
  284. void qemu_thread_atexit_add(Notifier *notifier)
  285. {
  286. if (!qemu_thread_data) {
  287. if (!atexit_registered) {
  288. atexit_registered = true;
  289. atexit(run_main_thread_exit);
  290. }
  291. notifier_list_add(&main_thread_exit, notifier);
  292. } else {
  293. notifier_list_add(&qemu_thread_data->exit, notifier);
  294. }
  295. }
  296. void qemu_thread_atexit_remove(Notifier *notifier)
  297. {
  298. notifier_remove(notifier);
  299. }
  300. static unsigned __stdcall win32_start_routine(void *arg)
  301. {
  302. QemuThreadData *data = (QemuThreadData *) arg;
  303. void *(*start_routine)(void *) = data->start_routine;
  304. void *thread_arg = data->arg;
  305. qemu_thread_data = data;
  306. qemu_thread_exit(start_routine(thread_arg));
  307. abort();
  308. }
  309. void qemu_thread_exit(void *arg)
  310. {
  311. QemuThreadData *data = qemu_thread_data;
  312. notifier_list_notify(&data->exit, NULL);
  313. if (data->mode == QEMU_THREAD_JOINABLE) {
  314. data->ret = arg;
  315. EnterCriticalSection(&data->cs);
  316. data->exited = true;
  317. LeaveCriticalSection(&data->cs);
  318. } else {
  319. g_free(data);
  320. }
  321. _endthreadex(0);
  322. }
  323. void *qemu_thread_join(QemuThread *thread)
  324. {
  325. QemuThreadData *data;
  326. void *ret;
  327. HANDLE handle;
  328. data = thread->data;
  329. if (data->mode == QEMU_THREAD_DETACHED) {
  330. return NULL;
  331. }
  332. /*
  333. * Because multiple copies of the QemuThread can exist via
  334. * qemu_thread_get_self, we need to store a value that cannot
  335. * leak there. The simplest, non racy way is to store the TID,
  336. * discard the handle that _beginthreadex gives back, and
  337. * get another copy of the handle here.
  338. */
  339. handle = qemu_thread_get_handle(thread);
  340. if (handle) {
  341. WaitForSingleObject(handle, INFINITE);
  342. CloseHandle(handle);
  343. }
  344. ret = data->ret;
  345. DeleteCriticalSection(&data->cs);
  346. g_free(data);
  347. return ret;
  348. }
  349. void qemu_thread_create(QemuThread *thread, const char *name,
  350. void *(*start_routine)(void *),
  351. void *arg, int mode)
  352. {
  353. HANDLE hThread;
  354. struct QemuThreadData *data;
  355. data = g_malloc(sizeof *data);
  356. data->start_routine = start_routine;
  357. data->arg = arg;
  358. data->mode = mode;
  359. data->exited = false;
  360. notifier_list_init(&data->exit);
  361. if (data->mode != QEMU_THREAD_DETACHED) {
  362. InitializeCriticalSection(&data->cs);
  363. }
  364. hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
  365. data, 0, &thread->tid);
  366. if (!hThread) {
  367. error_exit(GetLastError(), __func__);
  368. }
  369. CloseHandle(hThread);
  370. thread->data = data;
  371. }
  372. void qemu_thread_get_self(QemuThread *thread)
  373. {
  374. thread->data = qemu_thread_data;
  375. thread->tid = GetCurrentThreadId();
  376. }
  377. HANDLE qemu_thread_get_handle(QemuThread *thread)
  378. {
  379. QemuThreadData *data;
  380. HANDLE handle;
  381. data = thread->data;
  382. if (data->mode == QEMU_THREAD_DETACHED) {
  383. return NULL;
  384. }
  385. EnterCriticalSection(&data->cs);
  386. if (!data->exited) {
  387. handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME |
  388. THREAD_SET_CONTEXT, FALSE, thread->tid);
  389. } else {
  390. handle = NULL;
  391. }
  392. LeaveCriticalSection(&data->cs);
  393. return handle;
  394. }
  395. bool qemu_thread_is_self(QemuThread *thread)
  396. {
  397. return GetCurrentThreadId() == thread->tid;
  398. }