| 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 | |
| 14 | #include "qemu/osdep.h" |
| 15 | #include "qemu/thread.h" |
| 16 | #include "qemu/notify.h" |
| 17 | #include "qemu-thread-common.h" |
| 18 | #include <process.h> |
| 19 | |
| 20 | typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread, |
| 21 | PCWSTR lpThreadDescription); |
| 22 | typedef HRESULT (WINAPI *pGetThreadDescription) (HANDLE hThread, |
| 23 | PWSTR *lpThreadDescription); |
| 24 | static pSetThreadDescription SetThreadDescriptionFunc; |
| 25 | static pGetThreadDescription GetThreadDescriptionFunc; |
| 26 | static HMODULE kernel32_module; |
| 27 | |
| 28 | static void __attribute__((__constructor__(QEMU_CONSTRUCTOR_EARLY))) |
| 29 | qemu_thread_init(void) |
| 30 | { |
| 31 | qemu_thread_set_name("main"); |
| 32 | } |
| 33 | |
| 34 | static bool load_thread_description(void) |
| 35 | { |
| 36 | static gsize _init_once = 0; |
| 37 | |
| 38 | if (g_once_init_enter(&_init_once)) { |
| 39 | kernel32_module = LoadLibrary("kernel32.dll"); |
| 40 | if (kernel32_module) { |
| 41 | SetThreadDescriptionFunc = |
| 42 | (pSetThreadDescription)GetProcAddress(kernel32_module, |
| 43 | "SetThreadDescription"); |
| 44 | GetThreadDescriptionFunc = |
| 45 | (pGetThreadDescription)GetProcAddress(kernel32_module, |
| 46 | "GetThreadDescription"); |
| 47 | if (!SetThreadDescriptionFunc || !GetThreadDescriptionFunc) { |
| 48 | FreeLibrary(kernel32_module); |
| 49 | } |
| 50 | } |
| 51 | g_once_init_leave(&_init_once, 1); |
| 52 | } |
| 53 | |
| 54 | return (SetThreadDescriptionFunc && GetThreadDescriptionFunc); |
| 55 | } |
| 56 | |
| 57 | static void error_exit(int err, const char *msg) |
| 58 | { |
| 59 | char *pstr; |
| 60 | |
| 61 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
| 62 | NULL, err, 0, (LPTSTR)&pstr, 2, NULL); |
| 63 | fprintf(stderr, "qemu: %s: %s\n", msg, pstr); |
| 64 | LocalFree(pstr); |
| 65 | abort(); |
| 66 | } |
| 67 | |
| 68 | void qemu_mutex_init(QemuMutex *mutex) |
| 69 | { |
| 70 | InitializeSRWLock(&mutex->lock); |
| 71 | qemu_mutex_post_init(mutex); |
| 72 | } |
| 73 | |
| 74 | void qemu_mutex_destroy(QemuMutex *mutex) |
| 75 | { |
| 76 | assert(mutex->initialized); |
| 77 | mutex->initialized = false; |
| 78 | InitializeSRWLock(&mutex->lock); |
| 79 | } |
| 80 | |
| 81 | void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line) |
| 82 | { |
| 83 | assert(mutex->initialized); |
| 84 | qemu_mutex_pre_lock(mutex, file, line); |
| 85 | AcquireSRWLockExclusive(&mutex->lock); |
| 86 | qemu_mutex_post_lock(mutex, file, line); |
| 87 | } |
| 88 | |
| 89 | int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line) |
| 90 | { |
| 91 | int owned; |
| 92 | |
| 93 | assert(mutex->initialized); |
| 94 | owned = TryAcquireSRWLockExclusive(&mutex->lock); |
| 95 | if (owned) { |
| 96 | qemu_mutex_post_lock(mutex, file, line); |
| 97 | return 0; |
| 98 | } |
| 99 | return -EBUSY; |
| 100 | } |
| 101 | |
| 102 | void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line) |
| 103 | { |
| 104 | assert(mutex->initialized); |
| 105 | qemu_mutex_pre_unlock(mutex, file, line); |
| 106 | ReleaseSRWLockExclusive(&mutex->lock); |
| 107 | } |
| 108 | |
| 109 | void qemu_rec_mutex_init(QemuRecMutex *mutex) |
| 110 | { |
| 111 | InitializeCriticalSection(&mutex->lock); |
| 112 | mutex->initialized = true; |
| 113 | } |
| 114 | |
| 115 | void qemu_rec_mutex_destroy(QemuRecMutex *mutex) |
| 116 | { |
| 117 | assert(mutex->initialized); |
| 118 | mutex->initialized = false; |
| 119 | DeleteCriticalSection(&mutex->lock); |
| 120 | } |
| 121 | |
| 122 | void qemu_rec_mutex_lock_impl(QemuRecMutex *mutex, const char *file, int line) |
| 123 | { |
| 124 | assert(mutex->initialized); |
| 125 | EnterCriticalSection(&mutex->lock); |
| 126 | } |
| 127 | |
| 128 | int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line) |
| 129 | { |
| 130 | assert(mutex->initialized); |
| 131 | return !TryEnterCriticalSection(&mutex->lock); |
| 132 | } |
| 133 | |
| 134 | void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, const char *file, int line) |
| 135 | { |
| 136 | assert(mutex->initialized); |
| 137 | LeaveCriticalSection(&mutex->lock); |
| 138 | } |
| 139 | |
| 140 | void qemu_cond_init(QemuCond *cond) |
| 141 | { |
| 142 | memset(cond, 0, sizeof(*cond)); |
| 143 | InitializeConditionVariable(&cond->var); |
| 144 | cond->initialized = true; |
| 145 | } |
| 146 | |
| 147 | void qemu_cond_destroy(QemuCond *cond) |
| 148 | { |
| 149 | assert(cond->initialized); |
| 150 | cond->initialized = false; |
| 151 | InitializeConditionVariable(&cond->var); |
| 152 | } |
| 153 | |
| 154 | void qemu_cond_signal(QemuCond *cond) |
| 155 | { |
| 156 | assert(cond->initialized); |
| 157 | WakeConditionVariable(&cond->var); |
| 158 | } |
| 159 | |
| 160 | void qemu_cond_broadcast(QemuCond *cond) |
| 161 | { |
| 162 | assert(cond->initialized); |
| 163 | WakeAllConditionVariable(&cond->var); |
| 164 | } |
| 165 | |
| 166 | void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line) |
| 167 | { |
| 168 | assert(cond->initialized); |
| 169 | qemu_mutex_pre_unlock(mutex, file, line); |
| 170 | SleepConditionVariableSRW(&cond->var, &mutex->lock, INFINITE, 0); |
| 171 | qemu_mutex_post_lock(mutex, file, line); |
| 172 | } |
| 173 | |
| 174 | bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms, |
| 175 | const char *file, const int line) |
| 176 | { |
| 177 | int rc = 0; |
| 178 | |
| 179 | assert(cond->initialized); |
| 180 | trace_qemu_mutex_unlock(mutex, file, line); |
| 181 | if (!SleepConditionVariableSRW(&cond->var, &mutex->lock, ms, 0)) { |
| 182 | rc = GetLastError(); |
| 183 | } |
| 184 | trace_qemu_mutex_locked(mutex, file, line); |
| 185 | if (rc && rc != ERROR_TIMEOUT) { |
| 186 | error_exit(rc, __func__); |
| 187 | } |
| 188 | return rc != ERROR_TIMEOUT; |
| 189 | } |
| 190 | |
| 191 | void qemu_sem_init(QemuSemaphore *sem, int init) |
| 192 | { |
| 193 | /* Manual reset. */ |
| 194 | sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL); |
| 195 | sem->initialized = true; |
| 196 | } |
| 197 | |
| 198 | void qemu_sem_destroy(QemuSemaphore *sem) |
| 199 | { |
| 200 | assert(sem->initialized); |
| 201 | sem->initialized = false; |
| 202 | CloseHandle(sem->sema); |
| 203 | } |
| 204 | |
| 205 | void qemu_sem_post(QemuSemaphore *sem) |
| 206 | { |
| 207 | assert(sem->initialized); |
| 208 | ReleaseSemaphore(sem->sema, 1, NULL); |
| 209 | } |
| 210 | |
| 211 | int qemu_sem_timedwait(QemuSemaphore *sem, int ms) |
| 212 | { |
| 213 | int rc; |
| 214 | |
| 215 | assert(sem->initialized); |
| 216 | rc = WaitForSingleObject(sem->sema, ms); |
| 217 | if (rc == WAIT_OBJECT_0) { |
| 218 | return 0; |
| 219 | } |
| 220 | if (rc != WAIT_TIMEOUT) { |
| 221 | error_exit(GetLastError(), __func__); |
| 222 | } |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | void qemu_sem_wait(QemuSemaphore *sem) |
| 227 | { |
| 228 | assert(sem->initialized); |
| 229 | if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) { |
| 230 | error_exit(GetLastError(), __func__); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | struct QemuThreadData { |
| 235 | /* Passed to win32_start_routine. */ |
| 236 | void *(*start_routine)(void *); |
| 237 | void *arg; |
| 238 | short mode; |
| 239 | NotifierList exit; |
| 240 | char *name; /* Freed in win32_start_routine */ |
| 241 | |
| 242 | /* Only used for joinable threads. */ |
| 243 | bool exited; |
| 244 | void *ret; |
| 245 | SRWLOCK lock; |
| 246 | }; |
| 247 | |
| 248 | static bool atexit_registered; |
| 249 | static NotifierList main_thread_exit; |
| 250 | |
| 251 | static __thread QemuThreadData *qemu_thread_data; |
| 252 | |
| 253 | static void run_main_thread_exit(void) |
| 254 | { |
| 255 | notifier_list_notify(&main_thread_exit, NULL); |
| 256 | } |
| 257 | |
| 258 | void qemu_thread_atexit_add(Notifier *notifier) |
| 259 | { |
| 260 | if (!qemu_thread_data) { |
| 261 | if (!atexit_registered) { |
| 262 | atexit_registered = true; |
| 263 | atexit(run_main_thread_exit); |
| 264 | } |
| 265 | notifier_list_add(&main_thread_exit, notifier); |
| 266 | } else { |
| 267 | notifier_list_add(&qemu_thread_data->exit, notifier); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | void qemu_thread_atexit_remove(Notifier *notifier) |
| 272 | { |
| 273 | notifier_remove(notifier); |
| 274 | } |
| 275 | |
| 276 | static unsigned __stdcall win32_start_routine(void *arg) |
| 277 | { |
| 278 | QemuThreadData *data = (QemuThreadData *) arg; |
| 279 | void *(*start_routine)(void *) = data->start_routine; |
| 280 | void *thread_arg = data->arg; |
| 281 | |
| 282 | if (data->name) { |
| 283 | qemu_thread_set_name(data->name); |
| 284 | g_clear_pointer(&data->name, g_free); |
| 285 | } |
| 286 | qemu_thread_data = data; |
| 287 | qemu_thread_exit(start_routine(thread_arg)); |
| 288 | abort(); |
| 289 | } |
| 290 | |
| 291 | void qemu_thread_exit(void *arg) |
| 292 | { |
| 293 | QemuThreadData *data = qemu_thread_data; |
| 294 | |
| 295 | notifier_list_notify(&data->exit, NULL); |
| 296 | if (data->mode == QEMU_THREAD_JOINABLE) { |
| 297 | data->ret = arg; |
| 298 | AcquireSRWLockExclusive(&data->lock); |
| 299 | data->exited = true; |
| 300 | ReleaseSRWLockExclusive(&data->lock); |
| 301 | } else { |
| 302 | g_free(data); |
| 303 | } |
| 304 | _endthreadex(0); |
| 305 | } |
| 306 | |
| 307 | void *qemu_thread_join(QemuThread *thread) |
| 308 | { |
| 309 | QemuThreadData *data; |
| 310 | void *ret; |
| 311 | HANDLE handle; |
| 312 | |
| 313 | data = thread->data; |
| 314 | if (data->mode == QEMU_THREAD_DETACHED) { |
| 315 | return NULL; |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Because multiple copies of the QemuThread can exist via |
| 320 | * qemu_thread_get_self, we need to store a value that cannot |
| 321 | * leak there. The simplest, non racy way is to store the TID, |
| 322 | * discard the handle that _beginthreadex gives back, and |
| 323 | * get another copy of the handle here. |
| 324 | */ |
| 325 | handle = qemu_thread_get_handle(thread); |
| 326 | if (handle) { |
| 327 | WaitForSingleObject(handle, INFINITE); |
| 328 | CloseHandle(handle); |
| 329 | } |
| 330 | ret = data->ret; |
| 331 | g_free(data); |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | void qemu_thread_set_name(const char *name) |
| 336 | { |
| 337 | g_autofree wchar_t *namew = NULL; |
| 338 | |
| 339 | if (!load_thread_description()) { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | namew = g_utf8_to_utf16(name, -1, NULL, NULL, NULL); |
| 344 | if (!namew) { |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | SetThreadDescriptionFunc(GetCurrentThread(), namew); |
| 349 | } |
| 350 | |
| 351 | void qemu_thread_create(QemuThread *thread, const char *name, |
| 352 | void *(*start_routine)(void *), |
| 353 | void *arg, int mode) |
| 354 | { |
| 355 | HANDLE hThread; |
| 356 | struct QemuThreadData *data; |
| 357 | |
| 358 | data = g_malloc(sizeof *data); |
| 359 | InitializeSRWLock(&data->lock); |
| 360 | data->start_routine = start_routine; |
| 361 | data->arg = arg; |
| 362 | data->mode = mode; |
| 363 | data->exited = false; |
| 364 | data->name = g_strdup(name); |
| 365 | notifier_list_init(&data->exit); |
| 366 | |
| 367 | hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine, |
| 368 | data, 0, &thread->tid); |
| 369 | if (!hThread) { |
| 370 | error_exit(GetLastError(), __func__); |
| 371 | } |
| 372 | CloseHandle(hThread); |
| 373 | |
| 374 | thread->data = data; |
| 375 | } |
| 376 | |
| 377 | int qemu_thread_set_affinity(QemuThread *thread, unsigned long *host_cpus, |
| 378 | unsigned long nbits) |
| 379 | { |
| 380 | return -ENOSYS; |
| 381 | } |
| 382 | |
| 383 | int qemu_thread_get_affinity(QemuThread *thread, unsigned long **host_cpus, |
| 384 | unsigned long *nbits) |
| 385 | { |
| 386 | return -ENOSYS; |
| 387 | } |
| 388 | |
| 389 | void qemu_thread_get_self(QemuThread *thread) |
| 390 | { |
| 391 | thread->data = qemu_thread_data; |
| 392 | thread->tid = GetCurrentThreadId(); |
| 393 | } |
| 394 | |
| 395 | HANDLE qemu_thread_get_handle(QemuThread *thread) |
| 396 | { |
| 397 | QemuThreadData *data; |
| 398 | HANDLE handle; |
| 399 | |
| 400 | data = thread->data; |
| 401 | if (data->mode == QEMU_THREAD_DETACHED) { |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | AcquireSRWLockExclusive(&data->lock); |
| 406 | if (!data->exited) { |
| 407 | handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME | |
| 408 | THREAD_SET_CONTEXT, FALSE, thread->tid); |
| 409 | } else { |
| 410 | handle = NULL; |
| 411 | } |
| 412 | ReleaseSRWLockExclusive(&data->lock); |
| 413 | return handle; |
| 414 | } |
| 415 | |
| 416 | bool qemu_thread_is_self(QemuThread *thread) |
| 417 | { |
| 418 | return GetCurrentThreadId() == thread->tid; |
| 419 | } |
| 420 | |
| 421 | static __thread char namebuf[64]; |
| 422 | |
| 423 | const char *qemu_thread_get_name(void) |
| 424 | { |
| 425 | HRESULT hr; |
| 426 | wchar_t *namew = NULL; |
| 427 | g_autofree char *name = NULL; |
| 428 | |
| 429 | if (namebuf[0] != '\0') { |
| 430 | return namebuf; |
| 431 | } |
| 432 | |
| 433 | if (!load_thread_description()) { |
| 434 | goto error; |
| 435 | } |
| 436 | |
| 437 | hr = GetThreadDescriptionFunc(GetCurrentThread(), &namew); |
| 438 | if (!SUCCEEDED(hr)) { |
| 439 | goto error; |
| 440 | } |
| 441 | |
| 442 | name = g_utf16_to_utf8(namew, -1, NULL, NULL, NULL); |
| 443 | LocalFree(namew); |
| 444 | if (!name) { |
| 445 | goto error; |
| 446 | } |
| 447 | |
| 448 | g_strlcpy(namebuf, name, G_N_ELEMENTS(namebuf)); |
| 449 | return namebuf; |
| 450 | |
| 451 | error: |
| 452 | g_strlcpy(namebuf, "unnamed", G_N_ELEMENTS(namebuf)); |
| 453 | return namebuf; |
| 454 | } |