Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 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-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 14 | #include "qemu/thread.h" |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 15 | #include <process.h> |
| 16 | #include <assert.h> |
| 17 | #include <limits.h> |
| 18 | |
Dr. David Alan Gilbert | 8f480de | 2014-01-30 10:20:31 +0000 | [diff] [blame] | 19 | static bool name_threads; |
| 20 | |
| 21 | void qemu_thread_naming(bool enable) |
| 22 | { |
| 23 | /* But note we don't actually name them on Windows yet */ |
| 24 | name_threads = enable; |
Dr. David Alan Gilbert | 5c31207 | 2014-03-12 11:48:18 +0000 | [diff] [blame] | 25 | |
| 26 | fprintf(stderr, "qemu: thread naming not supported on this host\n"); |
Dr. David Alan Gilbert | 8f480de | 2014-01-30 10:20:31 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 29 | static 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); |
Jan Kiszka | 53380ac | 2011-09-21 09:28:31 +0200 | [diff] [blame] | 37 | abort(); |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void qemu_mutex_init(QemuMutex *mutex) |
| 41 | { |
| 42 | mutex->owner = 0; |
| 43 | InitializeCriticalSection(&mutex->lock); |
| 44 | } |
| 45 | |
Stefan Weil | 1a290ae | 2011-03-13 19:00:52 +0100 | [diff] [blame] | 46 | void qemu_mutex_destroy(QemuMutex *mutex) |
| 47 | { |
| 48 | assert(mutex->owner == 0); |
| 49 | DeleteCriticalSection(&mutex->lock); |
| 50 | } |
| 51 | |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 52 | void qemu_mutex_lock(QemuMutex *mutex) |
| 53 | { |
| 54 | EnterCriticalSection(&mutex->lock); |
| 55 | |
| 56 | /* Win32 CRITICAL_SECTIONs are recursive. Assert that we're not |
| 57 | * using them as such. |
| 58 | */ |
| 59 | assert(mutex->owner == 0); |
| 60 | mutex->owner = GetCurrentThreadId(); |
| 61 | } |
| 62 | |
| 63 | int 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 | |
| 75 | void qemu_mutex_unlock(QemuMutex *mutex) |
| 76 | { |
| 77 | assert(mutex->owner == GetCurrentThreadId()); |
| 78 | mutex->owner = 0; |
| 79 | LeaveCriticalSection(&mutex->lock); |
| 80 | } |
| 81 | |
| 82 | void 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, /* security */ |
| 91 | FALSE, /* auto-reset */ |
| 92 | FALSE, /* not signaled */ |
| 93 | NULL); /* name */ |
| 94 | if (!cond->continue_event) { |
| 95 | error_exit(GetLastError(), __func__); |
| 96 | } |
| 97 | } |
| 98 | |
Stefan Weil | 1a290ae | 2011-03-13 19:00:52 +0100 | [diff] [blame] | 99 | void 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 | |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 114 | void qemu_cond_signal(QemuCond *cond) |
| 115 | { |
| 116 | DWORD result; |
| 117 | |
| 118 | /* |
| 119 | * Signal only when there are waiters. cond->waiters is |
| 120 | * incremented by pthread_cond_wait under the external lock, |
| 121 | * so we are safe about that. |
| 122 | */ |
| 123 | if (cond->waiters == 0) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Waiting threads decrement it outside the external lock, but |
| 129 | * only if another thread is executing pthread_cond_broadcast and |
| 130 | * has the mutex. So, it also cannot be decremented concurrently |
| 131 | * with this particular access. |
| 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 | |
| 141 | void qemu_cond_broadcast(QemuCond *cond) |
| 142 | { |
| 143 | BOOLEAN result; |
| 144 | /* |
| 145 | * As in pthread_cond_signal, access to cond->waiters and |
| 146 | * cond->target is locked via the external mutex. |
| 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 | * At this point all waiters continue. Each one takes its |
| 160 | * slice of the semaphore. Now it's our turn to wait: Since |
| 161 | * the external mutex is held, no thread can leave cond_wait, |
| 162 | * yet. For this reason, we can be sure that no thread gets |
| 163 | * a chance to eat *more* than one slice. OTOH, it means |
| 164 | * that the last waiter must send us a wake-up. |
| 165 | */ |
| 166 | WaitForSingleObject(cond->continue_event, INFINITE); |
| 167 | } |
| 168 | |
| 169 | void qemu_cond_wait(QemuCond *cond, QemuMutex *mutex) |
| 170 | { |
| 171 | /* |
| 172 | * This access is protected under the mutex. |
| 173 | */ |
| 174 | cond->waiters++; |
| 175 | |
| 176 | /* |
| 177 | * Unlock external mutex and wait for signal. |
| 178 | * NOTE: we've held mutex locked long enough to increment |
| 179 | * waiters count above, so there's no problem with |
| 180 | * leaving mutex unlocked before we wait on semaphore. |
| 181 | */ |
| 182 | qemu_mutex_unlock(mutex); |
| 183 | WaitForSingleObject(cond->sema, INFINITE); |
| 184 | |
| 185 | /* Now waiters must rendez-vous with the signaling thread and |
| 186 | * let it continue. For cond_broadcast this has heavy contention |
| 187 | * and triggers thundering herd. So goes life. |
| 188 | * |
| 189 | * Decrease waiters count. The mutex is not taken, so we have |
| 190 | * to do this atomically. |
| 191 | * |
| 192 | * All waiters contend for the mutex at the end of this function |
| 193 | * until the signaling thread relinquishes it. To ensure |
| 194 | * each waiter consumes exactly one slice of the semaphore, |
| 195 | * the signaling thread stops until it is told by the last |
| 196 | * waiter that it can go on. |
| 197 | */ |
| 198 | if (InterlockedDecrement(&cond->waiters) == cond->target) { |
| 199 | SetEvent(cond->continue_event); |
| 200 | } |
| 201 | |
| 202 | qemu_mutex_lock(mutex); |
| 203 | } |
| 204 | |
Paolo Bonzini | 38b14db | 2011-08-08 14:36:41 +0200 | [diff] [blame] | 205 | void qemu_sem_init(QemuSemaphore *sem, int init) |
| 206 | { |
| 207 | /* Manual reset. */ |
| 208 | sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL); |
| 209 | } |
| 210 | |
| 211 | void qemu_sem_destroy(QemuSemaphore *sem) |
| 212 | { |
| 213 | CloseHandle(sem->sema); |
| 214 | } |
| 215 | |
| 216 | void qemu_sem_post(QemuSemaphore *sem) |
| 217 | { |
| 218 | ReleaseSemaphore(sem->sema, 1, NULL); |
| 219 | } |
| 220 | |
| 221 | int 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 | |
| 233 | void qemu_sem_wait(QemuSemaphore *sem) |
| 234 | { |
| 235 | if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) { |
| 236 | error_exit(GetLastError(), __func__); |
| 237 | } |
| 238 | } |
| 239 | |
Paolo Bonzini | c7c4d06 | 2013-09-25 14:20:59 +0800 | [diff] [blame] | 240 | void qemu_event_init(QemuEvent *ev, bool init) |
| 241 | { |
| 242 | /* Manual reset. */ |
| 243 | ev->event = CreateEvent(NULL, TRUE, init, NULL); |
| 244 | } |
| 245 | |
| 246 | void qemu_event_destroy(QemuEvent *ev) |
| 247 | { |
| 248 | CloseHandle(ev->event); |
| 249 | } |
| 250 | |
| 251 | void qemu_event_set(QemuEvent *ev) |
| 252 | { |
| 253 | SetEvent(ev->event); |
| 254 | } |
| 255 | |
| 256 | void qemu_event_reset(QemuEvent *ev) |
| 257 | { |
| 258 | ResetEvent(ev->event); |
| 259 | } |
| 260 | |
| 261 | void qemu_event_wait(QemuEvent *ev) |
| 262 | { |
| 263 | WaitForSingleObject(ev->event, INFINITE); |
| 264 | } |
| 265 | |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 266 | struct QemuThreadData { |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 267 | /* Passed to win32_start_routine. */ |
| 268 | void *(*start_routine)(void *); |
| 269 | void *arg; |
| 270 | short mode; |
| 271 | |
| 272 | /* Only used for joinable threads. */ |
| 273 | bool exited; |
| 274 | void *ret; |
| 275 | CRITICAL_SECTION cs; |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 276 | }; |
| 277 | |
Jan Kiszka | 6265e4f | 2012-11-23 12:12:01 +0100 | [diff] [blame] | 278 | static __thread QemuThreadData *qemu_thread_data; |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 279 | |
| 280 | static unsigned __stdcall win32_start_routine(void *arg) |
| 281 | { |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 282 | QemuThreadData *data = (QemuThreadData *) arg; |
| 283 | void *(*start_routine)(void *) = data->start_routine; |
| 284 | void *thread_arg = data->arg; |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 285 | |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 286 | if (data->mode == QEMU_THREAD_DETACHED) { |
| 287 | g_free(data); |
| 288 | data = NULL; |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 289 | } |
Jan Kiszka | 6265e4f | 2012-11-23 12:12:01 +0100 | [diff] [blame] | 290 | qemu_thread_data = data; |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 291 | qemu_thread_exit(start_routine(thread_arg)); |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 292 | abort(); |
| 293 | } |
| 294 | |
| 295 | void qemu_thread_exit(void *arg) |
| 296 | { |
Jan Kiszka | 6265e4f | 2012-11-23 12:12:01 +0100 | [diff] [blame] | 297 | QemuThreadData *data = qemu_thread_data; |
| 298 | |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 299 | if (data) { |
Stefan Weil | edc1de9 | 2012-01-31 07:14:15 +0100 | [diff] [blame] | 300 | assert(data->mode != QEMU_THREAD_DETACHED); |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 301 | data->ret = arg; |
| 302 | EnterCriticalSection(&data->cs); |
| 303 | data->exited = true; |
| 304 | LeaveCriticalSection(&data->cs); |
| 305 | } |
| 306 | _endthreadex(0); |
| 307 | } |
| 308 | |
| 309 | void *qemu_thread_join(QemuThread *thread) |
| 310 | { |
| 311 | QemuThreadData *data; |
| 312 | void *ret; |
| 313 | HANDLE handle; |
| 314 | |
| 315 | data = thread->data; |
| 316 | if (!data) { |
| 317 | return NULL; |
| 318 | } |
| 319 | /* |
| 320 | * Because multiple copies of the QemuThread can exist via |
| 321 | * qemu_thread_get_self, we need to store a value that cannot |
| 322 | * leak there. The simplest, non racy way is to store the TID, |
| 323 | * discard the handle that _beginthreadex gives back, and |
| 324 | * get another copy of the handle here. |
| 325 | */ |
Paolo Bonzini | 1ecf47b | 2011-12-13 13:43:52 +0100 | [diff] [blame] | 326 | handle = qemu_thread_get_handle(thread); |
| 327 | if (handle) { |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 328 | WaitForSingleObject(handle, INFINITE); |
| 329 | CloseHandle(handle); |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 330 | } |
| 331 | ret = data->ret; |
Stefan Weil | edc1de9 | 2012-01-31 07:14:15 +0100 | [diff] [blame] | 332 | assert(data->mode != QEMU_THREAD_DETACHED); |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 333 | DeleteCriticalSection(&data->cs); |
| 334 | g_free(data); |
| 335 | return ret; |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Dr. David Alan Gilbert | 4900116 | 2014-01-30 10:20:32 +0000 | [diff] [blame] | 338 | void qemu_thread_create(QemuThread *thread, const char *name, |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 339 | void *(*start_routine)(void *), |
Jan Kiszka | cf21871 | 2011-12-12 17:21:31 +0100 | [diff] [blame] | 340 | void *arg, int mode) |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 341 | { |
| 342 | HANDLE hThread; |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 343 | struct QemuThreadData *data; |
Jan Kiszka | 6265e4f | 2012-11-23 12:12:01 +0100 | [diff] [blame] | 344 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 345 | data = g_malloc(sizeof *data); |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 346 | data->start_routine = start_routine; |
| 347 | data->arg = arg; |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 348 | data->mode = mode; |
| 349 | data->exited = false; |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 350 | |
Stefan Weil | edc1de9 | 2012-01-31 07:14:15 +0100 | [diff] [blame] | 351 | if (data->mode != QEMU_THREAD_DETACHED) { |
| 352 | InitializeCriticalSection(&data->cs); |
| 353 | } |
| 354 | |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 355 | hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine, |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 356 | data, 0, &thread->tid); |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 357 | if (!hThread) { |
| 358 | error_exit(GetLastError(), __func__); |
| 359 | } |
| 360 | CloseHandle(hThread); |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 361 | thread->data = (mode == QEMU_THREAD_DETACHED) ? NULL : data; |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void qemu_thread_get_self(QemuThread *thread) |
| 365 | { |
Jan Kiszka | 6265e4f | 2012-11-23 12:12:01 +0100 | [diff] [blame] | 366 | thread->data = qemu_thread_data; |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 367 | thread->tid = GetCurrentThreadId(); |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Paolo Bonzini | 1ecf47b | 2011-12-13 13:43:52 +0100 | [diff] [blame] | 370 | HANDLE qemu_thread_get_handle(QemuThread *thread) |
| 371 | { |
| 372 | QemuThreadData *data; |
| 373 | HANDLE handle; |
| 374 | |
| 375 | data = thread->data; |
| 376 | if (!data) { |
| 377 | return NULL; |
| 378 | } |
| 379 | |
Stefan Weil | edc1de9 | 2012-01-31 07:14:15 +0100 | [diff] [blame] | 380 | assert(data->mode != QEMU_THREAD_DETACHED); |
Paolo Bonzini | 1ecf47b | 2011-12-13 13:43:52 +0100 | [diff] [blame] | 381 | EnterCriticalSection(&data->cs); |
| 382 | if (!data->exited) { |
| 383 | handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME, FALSE, |
| 384 | thread->tid); |
| 385 | } else { |
| 386 | handle = NULL; |
| 387 | } |
| 388 | LeaveCriticalSection(&data->cs); |
| 389 | return handle; |
| 390 | } |
| 391 | |
Andreas Färber | 2d797b6 | 2012-05-02 17:21:31 +0200 | [diff] [blame] | 392 | bool qemu_thread_is_self(QemuThread *thread) |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 393 | { |
Paolo Bonzini | 403e633 | 2011-12-12 17:21:33 +0100 | [diff] [blame] | 394 | return GetCurrentThreadId() == thread->tid; |
Paolo Bonzini | 9257d46 | 2011-03-12 17:43:52 +0100 | [diff] [blame] | 395 | } |