1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qemu-common.h"
15#include "qemu/thread.h"
16#include "qemu/notify.h"
17#include <process.h>
18
19static bool name_threads;
20
21void qemu_thread_naming(bool enable)
22{
23
24 name_threads = enable;
25
26 fprintf(stderr, "qemu: thread naming not supported on this host\n");
27}
28
29static void error_exit(int err, const char *msg)
30{
31 char *pstr;
32
33 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
34 NULL, err, 0, (LPTSTR)&pstr, 2, NULL);
35 fprintf(stderr, "qemu: %s: %s\n", msg, pstr);
36 LocalFree(pstr);
37 abort();
38}
39
40void qemu_mutex_init(QemuMutex *mutex)
41{
42 mutex->owner = 0;
43 InitializeCriticalSection(&mutex->lock);
44}
45
46void qemu_mutex_destroy(QemuMutex *mutex)
47{
48 assert(mutex->owner == 0);
49 DeleteCriticalSection(&mutex->lock);
50}
51
52void qemu_mutex_lock(QemuMutex *mutex)
53{
54 EnterCriticalSection(&mutex->lock);
55
56
57
58
59 assert(mutex->owner == 0);
60 mutex->owner = GetCurrentThreadId();
61}
62
63int qemu_mutex_trylock(QemuMutex *mutex)
64{
65 int owned;
66
67 owned = TryEnterCriticalSection(&mutex->lock);
68 if (owned) {
69 assert(mutex->owner == 0);
70 mutex->owner = GetCurrentThreadId();
71 }
72 return !owned;
73}
74
75void qemu_mutex_unlock(QemuMutex *mutex)
76{
77 assert(mutex->owner == GetCurrentThreadId());
78 mutex->owner = 0;
79 LeaveCriticalSection(&mutex->lock);
80}
81
82void qemu_cond_init(QemuCond *cond)
83{
84 memset(cond, 0, sizeof(*cond));
85
86 cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
87 if (!cond->sema) {
88 error_exit(GetLastError(), __func__);
89 }
90 cond->continue_event = CreateEvent(NULL,
91 FALSE,
92 FALSE,
93 NULL);
94 if (!cond->continue_event) {
95 error_exit(GetLastError(), __func__);
96 }
97}
98
99void qemu_cond_destroy(QemuCond *cond)
100{
101 BOOL result;
102 result = CloseHandle(cond->continue_event);
103 if (!result) {
104 error_exit(GetLastError(), __func__);
105 }
106 cond->continue_event = 0;
107 result = CloseHandle(cond->sema);
108 if (!result) {
109 error_exit(GetLastError(), __func__);
110 }
111 cond->sema = 0;
112}
113
114void qemu_cond_signal(QemuCond *cond)
115{
116 DWORD result;
117
118
119
120
121
122
123 if (cond->waiters == 0) {
124 return;
125 }
126
127
128
129
130
131
132
133 cond->target = cond->waiters - 1;
134 result = SignalObjectAndWait(cond->sema, cond->continue_event,
135 INFINITE, FALSE);
136 if (result == WAIT_ABANDONED || result == WAIT_FAILED) {
137 error_exit(GetLastError(), __func__);
138 }
139}
140
141void qemu_cond_broadcast(QemuCond *cond)
142{
143 BOOLEAN result;
144
145
146
147
148 if (cond->waiters == 0) {
149 return;
150 }
151
152 cond->target = 0;
153 result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
154 if (!result) {
155 error_exit(GetLastError(), __func__);
156 }
157
158
159
160
161
162
163
164
165
166 WaitForSingleObject(cond->continue_event, INFINITE);
167}
168
169void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
170{
171
172
173
174 cond->waiters++;
175
176
177
178
179
180
181
182 qemu_mutex_unlock(mutex);
183 WaitForSingleObject(cond->sema, INFINITE);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 if (InterlockedDecrement(&cond->waiters) == cond->target) {
199 SetEvent(cond->continue_event);
200 }
201
202 qemu_mutex_lock(mutex);
203}
204
205void qemu_sem_init(QemuSemaphore *sem, int init)
206{
207
208 sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL);
209}
210
211void qemu_sem_destroy(QemuSemaphore *sem)
212{
213 CloseHandle(sem->sema);
214}
215
216void qemu_sem_post(QemuSemaphore *sem)
217{
218 ReleaseSemaphore(sem->sema, 1, NULL);
219}
220
221int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
222{
223 int rc = WaitForSingleObject(sem->sema, ms);
224 if (rc == WAIT_OBJECT_0) {
225 return 0;
226 }
227 if (rc != WAIT_TIMEOUT) {
228 error_exit(GetLastError(), __func__);
229 }
230 return -1;
231}
232
233void qemu_sem_wait(QemuSemaphore *sem)
234{
235 if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) {
236 error_exit(GetLastError(), __func__);
237 }
238}
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259#define EV_SET 0
260#define EV_FREE 1
261#define EV_BUSY -1
262
263void qemu_event_init(QemuEvent *ev, bool init)
264{
265
266 ev->event = CreateEvent(NULL, TRUE, TRUE, NULL);
267 ev->value = (init ? EV_SET : EV_FREE);
268}
269
270void qemu_event_destroy(QemuEvent *ev)
271{
272 CloseHandle(ev->event);
273}
274
275void qemu_event_set(QemuEvent *ev)
276{
277 if (atomic_mb_read(&ev->value) != EV_SET) {
278 if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
279
280 SetEvent(ev->event);
281 }
282 }
283}
284
285void qemu_event_reset(QemuEvent *ev)
286{
287 if (atomic_mb_read(&ev->value) == EV_SET) {
288
289
290
291 atomic_or(&ev->value, EV_FREE);
292 }
293}
294
295void qemu_event_wait(QemuEvent *ev)
296{
297 unsigned value;
298
299 value = atomic_mb_read(&ev->value);
300 if (value != EV_SET) {
301 if (value == EV_FREE) {
302
303
304
305
306 ResetEvent(ev->event);
307
308
309
310
311
312 if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
313 value = EV_SET;
314 } else {
315 value = EV_BUSY;
316 }
317 }
318 if (value == EV_BUSY) {
319 WaitForSingleObject(ev->event, INFINITE);
320 }
321 }
322}
323
324struct QemuThreadData {
325
326 void *(*start_routine)(void *);
327 void *arg;
328 short mode;
329 NotifierList exit;
330
331
332 bool exited;
333 void *ret;
334 CRITICAL_SECTION cs;
335};
336
337static bool atexit_registered;
338static NotifierList main_thread_exit;
339
340static __thread QemuThreadData *qemu_thread_data;
341
342static void run_main_thread_exit(void)
343{
344 notifier_list_notify(&main_thread_exit, NULL);
345}
346
347void qemu_thread_atexit_add(Notifier *notifier)
348{
349 if (!qemu_thread_data) {
350 if (!atexit_registered) {
351 atexit_registered = true;
352 atexit(run_main_thread_exit);
353 }
354 notifier_list_add(&main_thread_exit, notifier);
355 } else {
356 notifier_list_add(&qemu_thread_data->exit, notifier);
357 }
358}
359
360void qemu_thread_atexit_remove(Notifier *notifier)
361{
362 notifier_remove(notifier);
363}
364
365static unsigned __stdcall win32_start_routine(void *arg)
366{
367 QemuThreadData *data = (QemuThreadData *) arg;
368 void *(*start_routine)(void *) = data->start_routine;
369 void *thread_arg = data->arg;
370
371 qemu_thread_data = data;
372 qemu_thread_exit(start_routine(thread_arg));
373 abort();
374}
375
376void qemu_thread_exit(void *arg)
377{
378 QemuThreadData *data = qemu_thread_data;
379
380 notifier_list_notify(&data->exit, NULL);
381 if (data->mode == QEMU_THREAD_JOINABLE) {
382 data->ret = arg;
383 EnterCriticalSection(&data->cs);
384 data->exited = true;
385 LeaveCriticalSection(&data->cs);
386 } else {
387 g_free(data);
388 }
389 _endthreadex(0);
390}
391
392void *qemu_thread_join(QemuThread *thread)
393{
394 QemuThreadData *data;
395 void *ret;
396 HANDLE handle;
397
398 data = thread->data;
399 if (data->mode == QEMU_THREAD_DETACHED) {
400 return NULL;
401 }
402
403
404
405
406
407
408
409
410 handle = qemu_thread_get_handle(thread);
411 if (handle) {
412 WaitForSingleObject(handle, INFINITE);
413 CloseHandle(handle);
414 }
415 ret = data->ret;
416 DeleteCriticalSection(&data->cs);
417 g_free(data);
418 return ret;
419}
420
421void qemu_thread_create(QemuThread *thread, const char *name,
422 void *(*start_routine)(void *),
423 void *arg, int mode)
424{
425 HANDLE hThread;
426 struct QemuThreadData *data;
427
428 data = g_malloc(sizeof *data);
429 data->start_routine = start_routine;
430 data->arg = arg;
431 data->mode = mode;
432 data->exited = false;
433 notifier_list_init(&data->exit);
434
435 if (data->mode != QEMU_THREAD_DETACHED) {
436 InitializeCriticalSection(&data->cs);
437 }
438
439 hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
440 data, 0, &thread->tid);
441 if (!hThread) {
442 error_exit(GetLastError(), __func__);
443 }
444 CloseHandle(hThread);
445 thread->data = data;
446}
447
448void qemu_thread_get_self(QemuThread *thread)
449{
450 thread->data = qemu_thread_data;
451 thread->tid = GetCurrentThreadId();
452}
453
454HANDLE qemu_thread_get_handle(QemuThread *thread)
455{
456 QemuThreadData *data;
457 HANDLE handle;
458
459 data = thread->data;
460 if (data->mode == QEMU_THREAD_DETACHED) {
461 return NULL;
462 }
463
464 EnterCriticalSection(&data->cs);
465 if (!data->exited) {
466 handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME, FALSE,
467 thread->tid);
468 } else {
469 handle = NULL;
470 }
471 LeaveCriticalSection(&data->cs);
472 return handle;
473}
474
475bool qemu_thread_is_self(QemuThread *thread)
476{
477 return GetCurrentThreadId() == thread->tid;
478}
479