2 * Win32 implementation for mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2010
7 * Paolo Bonzini <pbonzini@redhat.com>
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.
13 #include "qemu-common.h"
14 #include "qemu/thread.h"
19 static void error_exit(int err
, const char *msg
)
23 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
,
24 NULL
, err
, 0, (LPTSTR
)&pstr
, 2, NULL
);
25 fprintf(stderr
, "qemu: %s: %s\n", msg
, pstr
);
30 void qemu_mutex_init(QemuMutex
*mutex
)
33 InitializeCriticalSection(&mutex
->lock
);
36 void qemu_mutex_destroy(QemuMutex
*mutex
)
38 assert(mutex
->owner
== 0);
39 DeleteCriticalSection(&mutex
->lock
);
42 void qemu_mutex_lock(QemuMutex
*mutex
)
44 EnterCriticalSection(&mutex
->lock
);
46 /* Win32 CRITICAL_SECTIONs are recursive. Assert that we're not
49 assert(mutex
->owner
== 0);
50 mutex
->owner
= GetCurrentThreadId();
53 int qemu_mutex_trylock(QemuMutex
*mutex
)
57 owned
= TryEnterCriticalSection(&mutex
->lock
);
59 assert(mutex
->owner
== 0);
60 mutex
->owner
= GetCurrentThreadId();
65 void qemu_mutex_unlock(QemuMutex
*mutex
)
67 assert(mutex
->owner
== GetCurrentThreadId());
69 LeaveCriticalSection(&mutex
->lock
);
72 void qemu_cond_init(QemuCond
*cond
)
74 memset(cond
, 0, sizeof(*cond
));
76 cond
->sema
= CreateSemaphore(NULL
, 0, LONG_MAX
, NULL
);
78 error_exit(GetLastError(), __func__
);
80 cond
->continue_event
= CreateEvent(NULL
, /* security */
81 FALSE
, /* auto-reset */
82 FALSE
, /* not signaled */
84 if (!cond
->continue_event
) {
85 error_exit(GetLastError(), __func__
);
89 void qemu_cond_destroy(QemuCond
*cond
)
92 result
= CloseHandle(cond
->continue_event
);
94 error_exit(GetLastError(), __func__
);
96 cond
->continue_event
= 0;
97 result
= CloseHandle(cond
->sema
);
99 error_exit(GetLastError(), __func__
);
104 void qemu_cond_signal(QemuCond
*cond
)
109 * Signal only when there are waiters. cond->waiters is
110 * incremented by pthread_cond_wait under the external lock,
111 * so we are safe about that.
113 if (cond
->waiters
== 0) {
118 * Waiting threads decrement it outside the external lock, but
119 * only if another thread is executing pthread_cond_broadcast and
120 * has the mutex. So, it also cannot be decremented concurrently
121 * with this particular access.
123 cond
->target
= cond
->waiters
- 1;
124 result
= SignalObjectAndWait(cond
->sema
, cond
->continue_event
,
126 if (result
== WAIT_ABANDONED
|| result
== WAIT_FAILED
) {
127 error_exit(GetLastError(), __func__
);
131 void qemu_cond_broadcast(QemuCond
*cond
)
135 * As in pthread_cond_signal, access to cond->waiters and
136 * cond->target is locked via the external mutex.
138 if (cond
->waiters
== 0) {
143 result
= ReleaseSemaphore(cond
->sema
, cond
->waiters
, NULL
);
145 error_exit(GetLastError(), __func__
);
149 * At this point all waiters continue. Each one takes its
150 * slice of the semaphore. Now it's our turn to wait: Since
151 * the external mutex is held, no thread can leave cond_wait,
152 * yet. For this reason, we can be sure that no thread gets
153 * a chance to eat *more* than one slice. OTOH, it means
154 * that the last waiter must send us a wake-up.
156 WaitForSingleObject(cond
->continue_event
, INFINITE
);
159 void qemu_cond_wait(QemuCond
*cond
, QemuMutex
*mutex
)
162 * This access is protected under the mutex.
167 * Unlock external mutex and wait for signal.
168 * NOTE: we've held mutex locked long enough to increment
169 * waiters count above, so there's no problem with
170 * leaving mutex unlocked before we wait on semaphore.
172 qemu_mutex_unlock(mutex
);
173 WaitForSingleObject(cond
->sema
, INFINITE
);
175 /* Now waiters must rendez-vous with the signaling thread and
176 * let it continue. For cond_broadcast this has heavy contention
177 * and triggers thundering herd. So goes life.
179 * Decrease waiters count. The mutex is not taken, so we have
180 * to do this atomically.
182 * All waiters contend for the mutex at the end of this function
183 * until the signaling thread relinquishes it. To ensure
184 * each waiter consumes exactly one slice of the semaphore,
185 * the signaling thread stops until it is told by the last
186 * waiter that it can go on.
188 if (InterlockedDecrement(&cond
->waiters
) == cond
->target
) {
189 SetEvent(cond
->continue_event
);
192 qemu_mutex_lock(mutex
);
195 void qemu_sem_init(QemuSemaphore
*sem
, int init
)
198 sem
->sema
= CreateSemaphore(NULL
, init
, LONG_MAX
, NULL
);
201 void qemu_sem_destroy(QemuSemaphore
*sem
)
203 CloseHandle(sem
->sema
);
206 void qemu_sem_post(QemuSemaphore
*sem
)
208 ReleaseSemaphore(sem
->sema
, 1, NULL
);
211 int qemu_sem_timedwait(QemuSemaphore
*sem
, int ms
)
213 int rc
= WaitForSingleObject(sem
->sema
, ms
);
214 if (rc
== WAIT_OBJECT_0
) {
217 if (rc
!= WAIT_TIMEOUT
) {
218 error_exit(GetLastError(), __func__
);
223 void qemu_sem_wait(QemuSemaphore
*sem
)
225 if (WaitForSingleObject(sem
->sema
, INFINITE
) != WAIT_OBJECT_0
) {
226 error_exit(GetLastError(), __func__
);
230 void qemu_event_init(QemuEvent
*ev
, bool init
)
233 ev
->event
= CreateEvent(NULL
, TRUE
, init
, NULL
);
236 void qemu_event_destroy(QemuEvent
*ev
)
238 CloseHandle(ev
->event
);
241 void qemu_event_set(QemuEvent
*ev
)
246 void qemu_event_reset(QemuEvent
*ev
)
248 ResetEvent(ev
->event
);
251 void qemu_event_wait(QemuEvent
*ev
)
253 WaitForSingleObject(ev
->event
, INFINITE
);
256 struct QemuThreadData
{
257 /* Passed to win32_start_routine. */
258 void *(*start_routine
)(void *);
262 /* Only used for joinable threads. */
268 static __thread QemuThreadData
*qemu_thread_data
;
270 static unsigned __stdcall
win32_start_routine(void *arg
)
272 QemuThreadData
*data
= (QemuThreadData
*) arg
;
273 void *(*start_routine
)(void *) = data
->start_routine
;
274 void *thread_arg
= data
->arg
;
276 if (data
->mode
== QEMU_THREAD_DETACHED
) {
280 qemu_thread_data
= data
;
281 qemu_thread_exit(start_routine(thread_arg
));
285 void qemu_thread_exit(void *arg
)
287 QemuThreadData
*data
= qemu_thread_data
;
290 assert(data
->mode
!= QEMU_THREAD_DETACHED
);
292 EnterCriticalSection(&data
->cs
);
294 LeaveCriticalSection(&data
->cs
);
299 void *qemu_thread_join(QemuThread
*thread
)
301 QemuThreadData
*data
;
310 * Because multiple copies of the QemuThread can exist via
311 * qemu_thread_get_self, we need to store a value that cannot
312 * leak there. The simplest, non racy way is to store the TID,
313 * discard the handle that _beginthreadex gives back, and
314 * get another copy of the handle here.
316 handle
= qemu_thread_get_handle(thread
);
318 WaitForSingleObject(handle
, INFINITE
);
322 assert(data
->mode
!= QEMU_THREAD_DETACHED
);
323 DeleteCriticalSection(&data
->cs
);
328 void qemu_thread_create(QemuThread
*thread
,
329 void *(*start_routine
)(void *),
333 struct QemuThreadData
*data
;
335 data
= g_malloc(sizeof *data
);
336 data
->start_routine
= start_routine
;
339 data
->exited
= false;
341 if (data
->mode
!= QEMU_THREAD_DETACHED
) {
342 InitializeCriticalSection(&data
->cs
);
345 hThread
= (HANDLE
) _beginthreadex(NULL
, 0, win32_start_routine
,
346 data
, 0, &thread
->tid
);
348 error_exit(GetLastError(), __func__
);
350 CloseHandle(hThread
);
351 thread
->data
= (mode
== QEMU_THREAD_DETACHED
) ? NULL
: data
;
354 void qemu_thread_get_self(QemuThread
*thread
)
356 thread
->data
= qemu_thread_data
;
357 thread
->tid
= GetCurrentThreadId();
360 HANDLE
qemu_thread_get_handle(QemuThread
*thread
)
362 QemuThreadData
*data
;
370 assert(data
->mode
!= QEMU_THREAD_DETACHED
);
371 EnterCriticalSection(&data
->cs
);
373 handle
= OpenThread(SYNCHRONIZE
| THREAD_SUSPEND_RESUME
, FALSE
,
378 LeaveCriticalSection(&data
->cs
);
382 bool qemu_thread_is_self(QemuThread
*thread
)
384 return GetCurrentThreadId() == thread
->tid
;