kvm: x86: Synchronize PAT MSR with the kernel
[qemu.git] / qemu-thread-win32.c
blob2edcb1a077b990b22ff2f4352f9e5fa9866bec7c
1 /*
2 * Win32 implementation for mutex/cond/thread functions
4 * Copyright Red Hat, Inc. 2010
6 * Author:
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"
15 #include <process.h>
16 #include <assert.h>
17 #include <limits.h>
19 static void error_exit(int err, const char *msg)
21 char *pstr;
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);
26 LocalFree(pstr);
27 exit(1);
30 void qemu_mutex_init(QemuMutex *mutex)
32 mutex->owner = 0;
33 InitializeCriticalSection(&mutex->lock);
36 void qemu_mutex_lock(QemuMutex *mutex)
38 EnterCriticalSection(&mutex->lock);
40 /* Win32 CRITICAL_SECTIONs are recursive. Assert that we're not
41 * using them as such.
43 assert(mutex->owner == 0);
44 mutex->owner = GetCurrentThreadId();
47 int qemu_mutex_trylock(QemuMutex *mutex)
49 int owned;
51 owned = TryEnterCriticalSection(&mutex->lock);
52 if (owned) {
53 assert(mutex->owner == 0);
54 mutex->owner = GetCurrentThreadId();
56 return !owned;
59 void qemu_mutex_unlock(QemuMutex *mutex)
61 assert(mutex->owner == GetCurrentThreadId());
62 mutex->owner = 0;
63 LeaveCriticalSection(&mutex->lock);
66 void qemu_cond_init(QemuCond *cond)
68 memset(cond, 0, sizeof(*cond));
70 cond->sema = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
71 if (!cond->sema) {
72 error_exit(GetLastError(), __func__);
74 cond->continue_event = CreateEvent(NULL, /* security */
75 FALSE, /* auto-reset */
76 FALSE, /* not signaled */
77 NULL); /* name */
78 if (!cond->continue_event) {
79 error_exit(GetLastError(), __func__);
83 void qemu_cond_signal(QemuCond *cond)
85 DWORD result;
88 * Signal only when there are waiters. cond->waiters is
89 * incremented by pthread_cond_wait under the external lock,
90 * so we are safe about that.
92 if (cond->waiters == 0) {
93 return;
97 * Waiting threads decrement it outside the external lock, but
98 * only if another thread is executing pthread_cond_broadcast and
99 * has the mutex. So, it also cannot be decremented concurrently
100 * with this particular access.
102 cond->target = cond->waiters - 1;
103 result = SignalObjectAndWait(cond->sema, cond->continue_event,
104 INFINITE, FALSE);
105 if (result == WAIT_ABANDONED || result == WAIT_FAILED) {
106 error_exit(GetLastError(), __func__);
110 void qemu_cond_broadcast(QemuCond *cond)
112 BOOLEAN result;
114 * As in pthread_cond_signal, access to cond->waiters and
115 * cond->target is locked via the external mutex.
117 if (cond->waiters == 0) {
118 return;
121 cond->target = 0;
122 result = ReleaseSemaphore(cond->sema, cond->waiters, NULL);
123 if (!result) {
124 error_exit(GetLastError(), __func__);
128 * At this point all waiters continue. Each one takes its
129 * slice of the semaphore. Now it's our turn to wait: Since
130 * the external mutex is held, no thread can leave cond_wait,
131 * yet. For this reason, we can be sure that no thread gets
132 * a chance to eat *more* than one slice. OTOH, it means
133 * that the last waiter must send us a wake-up.
135 WaitForSingleObject(cond->continue_event, INFINITE);
138 void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex)
141 * This access is protected under the mutex.
143 cond->waiters++;
146 * Unlock external mutex and wait for signal.
147 * NOTE: we've held mutex locked long enough to increment
148 * waiters count above, so there's no problem with
149 * leaving mutex unlocked before we wait on semaphore.
151 qemu_mutex_unlock(mutex);
152 WaitForSingleObject(cond->sema, INFINITE);
154 /* Now waiters must rendez-vous with the signaling thread and
155 * let it continue. For cond_broadcast this has heavy contention
156 * and triggers thundering herd. So goes life.
158 * Decrease waiters count. The mutex is not taken, so we have
159 * to do this atomically.
161 * All waiters contend for the mutex at the end of this function
162 * until the signaling thread relinquishes it. To ensure
163 * each waiter consumes exactly one slice of the semaphore,
164 * the signaling thread stops until it is told by the last
165 * waiter that it can go on.
167 if (InterlockedDecrement(&cond->waiters) == cond->target) {
168 SetEvent(cond->continue_event);
171 qemu_mutex_lock(mutex);
174 struct QemuThreadData {
175 QemuThread *thread;
176 void *(*start_routine)(void *);
177 void *arg;
180 static int qemu_thread_tls_index = TLS_OUT_OF_INDEXES;
182 static unsigned __stdcall win32_start_routine(void *arg)
184 struct QemuThreadData data = *(struct QemuThreadData *) arg;
185 QemuThread *thread = data.thread;
187 free(arg);
188 TlsSetValue(qemu_thread_tls_index, thread);
191 * Use DuplicateHandle instead of assigning thread->thread in the
192 * creating thread to avoid races. It's simpler this way than with
193 * synchronization.
195 DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
196 GetCurrentProcess(), &thread->thread,
197 0, FALSE, DUPLICATE_SAME_ACCESS);
199 qemu_thread_exit(data.start_routine(data.arg));
200 abort();
203 void qemu_thread_exit(void *arg)
205 QemuThread *thread = TlsGetValue(qemu_thread_tls_index);
206 thread->ret = arg;
207 CloseHandle(thread->thread);
208 thread->thread = NULL;
209 ExitThread(0);
212 static inline void qemu_thread_init(void)
214 if (qemu_thread_tls_index == TLS_OUT_OF_INDEXES) {
215 qemu_thread_tls_index = TlsAlloc();
216 if (qemu_thread_tls_index == TLS_OUT_OF_INDEXES) {
217 error_exit(ERROR_NO_SYSTEM_RESOURCES, __func__);
223 void qemu_thread_create(QemuThread *thread,
224 void *(*start_routine)(void *),
225 void *arg)
227 HANDLE hThread;
229 struct QemuThreadData *data;
230 qemu_thread_init();
231 data = qemu_malloc(sizeof *data);
232 data->thread = thread;
233 data->start_routine = start_routine;
234 data->arg = arg;
236 hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
237 data, 0, NULL);
238 if (!hThread) {
239 error_exit(GetLastError(), __func__);
241 CloseHandle(hThread);
244 void qemu_thread_get_self(QemuThread *thread)
246 if (!thread->thread) {
247 /* In the main thread of the process. Initialize the QemuThread
248 pointer in TLS, and use the dummy GetCurrentThread handle as
249 the identifier for qemu_thread_is_self. */
250 qemu_thread_init();
251 TlsSetValue(qemu_thread_tls_index, thread);
252 thread->thread = GetCurrentThread();
256 int qemu_thread_is_self(QemuThread *thread)
258 QemuThread *this_thread = TlsGetValue(qemu_thread_tls_index);
259 return this_thread->thread == thread->thread;