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_rec_mutex_init(QemuRecMutex *mutex)
83{
84 InitializeCriticalSection(&mutex->lock);
85}
86
87void qemu_rec_mutex_destroy(QemuRecMutex *mutex)
88{
89 DeleteCriticalSection(&mutex->lock);
90}
91
92void qemu_rec_mutex_lock(QemuRecMutex *mutex)
93{
94 EnterCriticalSection(&mutex->lock);
95}
96
97int qemu_rec_mutex_trylock(QemuRecMutex *mutex)
98{
99 return !TryEnterCriticalSection(&mutex->lock);
100}
101
102void qemu_rec_mutex_unlock(QemuRecMutex *mutex)
103{
104 LeaveCriticalSection(&mutex->lock);
105}
106
107void qemu_cond_init(QemuCond *cond)
108{
109 memset(cond, 0, sizeof(*cond));
110
111 cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
112 if (!cond->sema) {
113 error_exit(GetLastError(), __func__);
114 }
115 cond->continue_event = CreateEvent(NULL,
116 FALSE,
117 FALSE,
118 NULL);
119 if (!cond->continue_event) {
120 error_exit(GetLastError(), __func__);
121 }
122}
123
124void qemu_cond_destroy(QemuCond *cond)
125{
126 BOOL result;
127 result = CloseHandle(cond->continue_event);
128 if (!result) {
129 error_exit(GetLastError(), __func__);
130 }
131 cond->continue_event = 0;
132 result = CloseHandle(cond->sema);
133 if (!result) {
134 error_exit(GetLastError(), __func__);
135 }
136 cond->sema = 0;
137}
138
139void qemu_cond_signal(QemuCond *cond)
140{
141 DWORD result;
142
143
144
145
146
147
148 if (cond->waiters == 0) {
149 return;
150 }
151
152
153
154
155
156
157
158 cond->target = cond->waiters - 1;
159 result = SignalObjectAndWait(cond->sema, cond->continue_event,
160 INFINITE, FALSE);
161 if (result == WAIT_ABANDONED || result == WAIT_FAILED) {
162 error_exit(GetLastError(), __func__);
163 }
164}
165
166void qemu_cond_broadcast(QemuCond *cond)
167{
168 BOOLEAN result;
169
170
171
172
173 if (cond->waiters == 0) {
174 return;
175 }
176
177 cond->target = 0;
178 result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
179 if (!result) {
180 error_exit(GetLastError(), __func__);
181 }
182
183
184
185
186
187
188
189
190
191 WaitForSingleObject(cond->continue_event, INFINITE);
192}
193
194void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
195{
196
197
198
199 cond->waiters++;
200
201
202
203
204
205
206
207 qemu_mutex_unlock(mutex);
208 WaitForSingleObject(cond->sema, INFINITE);
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223 if (InterlockedDecrement(&cond->waiters) == cond->target) {
224 SetEvent(cond->continue_event);
225 }
226
227 qemu_mutex_lock(mutex);
228}
229
230void qemu_sem_init(QemuSemaphore *sem, int init)
231{
232
233 sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL);
234}
235
236void qemu_sem_destroy(QemuSemaphore *sem)
237{
238 CloseHandle(sem->sema);
239}
240
241void qemu_sem_post(QemuSemaphore *sem)
242{
243 ReleaseSemaphore(sem->sema, 1, NULL);
244}
245
246int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
247{
248 int rc = WaitForSingleObject(sem->sema, ms);
249 if (rc == WAIT_OBJECT_0) {
250 return 0;
251 }
252 if (rc != WAIT_TIMEOUT) {
253 error_exit(GetLastError(), __func__);
254 }
255 return -1;
256}
257
258void qemu_sem_wait(QemuSemaphore *sem)
259{
260 if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) {
261 error_exit(GetLastError(), __func__);
262 }
263}
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284#define EV_SET 0
285#define EV_FREE 1
286#define EV_BUSY -1
287
288void qemu_event_init(QemuEvent *ev, bool init)
289{
290
291 ev->event = CreateEvent(NULL, TRUE, TRUE, NULL);
292 ev->value = (init ? EV_SET : EV_FREE);
293}
294
295void qemu_event_destroy(QemuEvent *ev)
296{
297 CloseHandle(ev->event);
298}
299
300void qemu_event_set(QemuEvent *ev)
301{
302
303
304
305 smp_mb();
306 if (atomic_read(&ev->value) != EV_SET) {
307 if (atomic_xchg(&ev->value, EV_SET) == EV_BUSY) {
308
309 SetEvent(ev->event);
310 }
311 }
312}
313
314void qemu_event_reset(QemuEvent *ev)
315{
316 unsigned value;
317
318 value = atomic_read(&ev->value);
319 smp_mb_acquire();
320 if (value == EV_SET) {
321
322
323
324 atomic_or(&ev->value, EV_FREE);
325 }
326}
327
328void qemu_event_wait(QemuEvent *ev)
329{
330 unsigned value;
331
332 value = atomic_read(&ev->value);
333 smp_mb_acquire();
334 if (value != EV_SET) {
335 if (value == EV_FREE) {
336
337
338
339
340 ResetEvent(ev->event);
341
342
343
344
345
346 if (atomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
347 value = EV_SET;
348 } else {
349 value = EV_BUSY;
350 }
351 }
352 if (value == EV_BUSY) {
353 WaitForSingleObject(ev->event, INFINITE);
354 }
355 }
356}
357
358struct QemuThreadData {
359
360 void *(*start_routine)(void *);
361 void *arg;
362 short mode;
363 NotifierList exit;
364
365
366 bool exited;
367 void *ret;
368 CRITICAL_SECTION cs;
369};
370
371static bool atexit_registered;
372static NotifierList main_thread_exit;
373
374static __thread QemuThreadData *qemu_thread_data;
375
376static void run_main_thread_exit(void)
377{
378 notifier_list_notify(&main_thread_exit, NULL);
379}
380
381void qemu_thread_atexit_add(Notifier *notifier)
382{
383 if (!qemu_thread_data) {
384 if (!atexit_registered) {
385 atexit_registered = true;
386 atexit(run_main_thread_exit);
387 }
388 notifier_list_add(&main_thread_exit, notifier);
389 } else {
390 notifier_list_add(&qemu_thread_data->exit, notifier);
391 }
392}
393
394void qemu_thread_atexit_remove(Notifier *notifier)
395{
396 notifier_remove(notifier);
397}
398
399static unsigned __stdcall win32_start_routine(void *arg)
400{
401 QemuThreadData *data = (QemuThreadData *) arg;
402 void *(*start_routine)(void *) = data->start_routine;
403 void *thread_arg = data->arg;
404
405 qemu_thread_data = data;
406 qemu_thread_exit(start_routine(thread_arg));
407 abort();
408}
409
410void qemu_thread_exit(void *arg)
411{
412 QemuThreadData *data = qemu_thread_data;
413
414 notifier_list_notify(&data->exit, NULL);
415 if (data->mode == QEMU_THREAD_JOINABLE) {
416 data->ret = arg;
417 EnterCriticalSection(&data->cs);
418 data->exited = true;
419 LeaveCriticalSection(&data->cs);
420 } else {
421 g_free(data);
422 }
423 _endthreadex(0);
424}
425
426void *qemu_thread_join(QemuThread *thread)
427{
428 QemuThreadData *data;
429 void *ret;
430 HANDLE handle;
431
432 data = thread->data;
433 if (data->mode == QEMU_THREAD_DETACHED) {
434 return NULL;
435 }
436
437
438
439
440
441
442
443
444 handle = qemu_thread_get_handle(thread);
445 if (handle) {
446 WaitForSingleObject(handle, INFINITE);
447 CloseHandle(handle);
448 }
449 ret = data->ret;
450 DeleteCriticalSection(&data->cs);
451 g_free(data);
452 return ret;
453}
454
455void qemu_thread_create(QemuThread *thread, const char *name,
456 void *(*start_routine)(void *),
457 void *arg, int mode)
458{
459 HANDLE hThread;
460 struct QemuThreadData *data;
461
462 data = g_malloc(sizeof *data);
463 data->start_routine = start_routine;
464 data->arg = arg;
465 data->mode = mode;
466 data->exited = false;
467 notifier_list_init(&data->exit);
468
469 if (data->mode != QEMU_THREAD_DETACHED) {
470 InitializeCriticalSection(&data->cs);
471 }
472
473 hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
474 data, 0, &thread->tid);
475 if (!hThread) {
476 error_exit(GetLastError(), __func__);
477 }
478 CloseHandle(hThread);
479 thread->data = data;
480}
481
482void qemu_thread_get_self(QemuThread *thread)
483{
484 thread->data = qemu_thread_data;
485 thread->tid = GetCurrentThreadId();
486}
487
488HANDLE qemu_thread_get_handle(QemuThread *thread)
489{
490 QemuThreadData *data;
491 HANDLE handle;
492
493 data = thread->data;
494 if (data->mode == QEMU_THREAD_DETACHED) {
495 return NULL;
496 }
497
498 EnterCriticalSection(&data->cs);
499 if (!data->exited) {
500 handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME, FALSE,
501 thread->tid);
502 } else {
503 handle = NULL;
504 }
505 LeaveCriticalSection(&data->cs);
506 return handle;
507}
508
509bool qemu_thread_is_self(QemuThread *thread)
510{
511 return GetCurrentThreadId() == thread->tid;
512}
513