2011-03-12 19:43:52 +03:00
|
|
|
/*
|
|
|
|
* Win32 implementation for mutex/cond/thread functions
|
|
|
|
*
|
|
|
|
* Copyright Red Hat, Inc. 2010
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* Paolo Bonzini <pbonzini@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
2017-03-25 01:01:41 +03:00
|
|
|
|
2016-01-29 20:49:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/thread.h"
|
2014-12-02 14:05:45 +03:00
|
|
|
#include "qemu/notify.h"
|
2018-04-25 05:54:57 +03:00
|
|
|
#include "qemu-thread-common.h"
|
2011-03-12 19:43:52 +03:00
|
|
|
#include <process.h>
|
|
|
|
|
2014-01-30 14:20:31 +04:00
|
|
|
static bool name_threads;
|
|
|
|
|
2022-09-13 20:53:20 +03:00
|
|
|
typedef HRESULT (WINAPI *pSetThreadDescription) (HANDLE hThread,
|
|
|
|
PCWSTR lpThreadDescription);
|
|
|
|
static pSetThreadDescription SetThreadDescriptionFunc;
|
|
|
|
static HMODULE kernel32_module;
|
|
|
|
|
|
|
|
static bool load_set_thread_description(void)
|
|
|
|
{
|
|
|
|
static gsize _init_once = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter(&_init_once)) {
|
|
|
|
kernel32_module = LoadLibrary("kernel32.dll");
|
|
|
|
if (kernel32_module) {
|
|
|
|
SetThreadDescriptionFunc =
|
|
|
|
(pSetThreadDescription)GetProcAddress(kernel32_module,
|
|
|
|
"SetThreadDescription");
|
|
|
|
if (!SetThreadDescriptionFunc) {
|
|
|
|
FreeLibrary(kernel32_module);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_once_init_leave(&_init_once, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !!SetThreadDescriptionFunc;
|
|
|
|
}
|
|
|
|
|
2014-01-30 14:20:31 +04:00
|
|
|
void qemu_thread_naming(bool enable)
|
|
|
|
{
|
|
|
|
name_threads = enable;
|
2014-03-12 15:48:18 +04:00
|
|
|
|
2022-09-13 20:53:20 +03:00
|
|
|
if (enable && !load_set_thread_description()) {
|
|
|
|
fprintf(stderr, "qemu: thread naming not supported on this host\n");
|
|
|
|
name_threads = false;
|
|
|
|
}
|
2014-01-30 14:20:31 +04:00
|
|
|
}
|
|
|
|
|
2011-03-12 19:43:52 +03:00
|
|
|
static void error_exit(int err, const char *msg)
|
|
|
|
{
|
|
|
|
char *pstr;
|
|
|
|
|
|
|
|
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
NULL, err, 0, (LPTSTR)&pstr, 2, NULL);
|
|
|
|
fprintf(stderr, "qemu: %s: %s\n", msg, pstr);
|
|
|
|
LocalFree(pstr);
|
2011-09-21 11:28:31 +04:00
|
|
|
abort();
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_mutex_init(QemuMutex *mutex)
|
|
|
|
{
|
2017-03-25 01:01:41 +03:00
|
|
|
InitializeSRWLock(&mutex->lock);
|
2018-04-25 05:54:57 +03:00
|
|
|
qemu_mutex_post_init(mutex);
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-13 21:00:52 +03:00
|
|
|
void qemu_mutex_destroy(QemuMutex *mutex)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(mutex->initialized);
|
|
|
|
mutex->initialized = false;
|
2017-03-25 01:01:41 +03:00
|
|
|
InitializeSRWLock(&mutex->lock);
|
2011-03-13 21:00:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:27:16 +03:00
|
|
|
void qemu_mutex_lock_impl(QemuMutex *mutex, const char *file, const int line)
|
2011-03-12 19:43:52 +03:00
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(mutex->initialized);
|
2018-04-25 05:54:57 +03:00
|
|
|
qemu_mutex_pre_lock(mutex, file, line);
|
2017-03-25 01:01:41 +03:00
|
|
|
AcquireSRWLockExclusive(&mutex->lock);
|
2018-04-25 05:54:57 +03:00
|
|
|
qemu_mutex_post_lock(mutex, file, line);
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:27:16 +03:00
|
|
|
int qemu_mutex_trylock_impl(QemuMutex *mutex, const char *file, const int line)
|
2011-03-12 19:43:52 +03:00
|
|
|
{
|
|
|
|
int owned;
|
|
|
|
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(mutex->initialized);
|
2017-03-25 01:01:41 +03:00
|
|
|
owned = TryAcquireSRWLockExclusive(&mutex->lock);
|
trace: add qemu mutex lock and unlock trace events
These trace events were very useful to help me to understand and find a
reordering issue in vfio, for example:
qemu_mutex_lock locked mutex 0x10905ad8
vfio_region_write (0001:03:00.0:region1+0xc0, 0x2020c, 4)
qemu_mutex_unlock unlocked mutex 0x10905ad8
qemu_mutex_lock locked mutex 0x10905ad8
vfio_region_write (0001:03:00.0:region1+0xc4, 0xa0000, 4)
qemu_mutex_unlock unlocked mutex 0x10905ad8
that also helped me to see the desired result after the fix:
qemu_mutex_lock locked mutex 0x10905ad8
vfio_region_write (0001:03:00.0:region1+0xc0, 0x2000c, 4)
vfio_region_write (0001:03:00.0:region1+0xc4, 0xb0000, 4)
qemu_mutex_unlock unlocked mutex 0x10905ad8
So it could be a good idea to have these traces implemented. It's worth
mentioning that they should be surgically enabled during the debugging,
otherwise it can flood the trace logs with lock/unlock messages.
How to use it:
trace-event qemu_mutex_lock on|off
trace-event qemu_mutex_unlock on|off
or
trace-event qemu_mutex* on|off
Signed-off-by: Jose Ricardo Ziviani <joserz@linux.vnet.ibm.com>
Message-Id: <1493054398-26013-1-git-send-email-joserz@linux.vnet.ibm.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
[Also handle trylock, cond_wait and win32; trace "unlocked" while still
in the critical section, so that "unlocked" always comes before the
next "locked" tracepoint. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2017-04-24 20:19:58 +03:00
|
|
|
if (owned) {
|
2018-04-25 05:54:57 +03:00
|
|
|
qemu_mutex_post_lock(mutex, file, line);
|
trace: add qemu mutex lock and unlock trace events
These trace events were very useful to help me to understand and find a
reordering issue in vfio, for example:
qemu_mutex_lock locked mutex 0x10905ad8
vfio_region_write (0001:03:00.0:region1+0xc0, 0x2020c, 4)
qemu_mutex_unlock unlocked mutex 0x10905ad8
qemu_mutex_lock locked mutex 0x10905ad8
vfio_region_write (0001:03:00.0:region1+0xc4, 0xa0000, 4)
qemu_mutex_unlock unlocked mutex 0x10905ad8
that also helped me to see the desired result after the fix:
qemu_mutex_lock locked mutex 0x10905ad8
vfio_region_write (0001:03:00.0:region1+0xc0, 0x2000c, 4)
vfio_region_write (0001:03:00.0:region1+0xc4, 0xb0000, 4)
qemu_mutex_unlock unlocked mutex 0x10905ad8
So it could be a good idea to have these traces implemented. It's worth
mentioning that they should be surgically enabled during the debugging,
otherwise it can flood the trace logs with lock/unlock messages.
How to use it:
trace-event qemu_mutex_lock on|off
trace-event qemu_mutex_unlock on|off
or
trace-event qemu_mutex* on|off
Signed-off-by: Jose Ricardo Ziviani <joserz@linux.vnet.ibm.com>
Message-Id: <1493054398-26013-1-git-send-email-joserz@linux.vnet.ibm.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
[Also handle trylock, cond_wait and win32; trace "unlocked" while still
in the critical section, so that "unlocked" always comes before the
next "locked" tracepoint. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2017-04-24 20:19:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EBUSY;
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:27:16 +03:00
|
|
|
void qemu_mutex_unlock_impl(QemuMutex *mutex, const char *file, const int line)
|
2011-03-12 19:43:52 +03:00
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(mutex->initialized);
|
2018-04-25 05:54:57 +03:00
|
|
|
qemu_mutex_pre_unlock(mutex, file, line);
|
2017-03-25 01:01:41 +03:00
|
|
|
ReleaseSRWLockExclusive(&mutex->lock);
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2016-10-27 13:49:07 +03:00
|
|
|
void qemu_rec_mutex_init(QemuRecMutex *mutex)
|
|
|
|
{
|
|
|
|
InitializeCriticalSection(&mutex->lock);
|
2017-07-04 15:23:25 +03:00
|
|
|
mutex->initialized = true;
|
2016-10-27 13:49:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_rec_mutex_destroy(QemuRecMutex *mutex)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(mutex->initialized);
|
|
|
|
mutex->initialized = false;
|
2016-10-27 13:49:07 +03:00
|
|
|
DeleteCriticalSection(&mutex->lock);
|
|
|
|
}
|
|
|
|
|
2017-08-08 20:53:15 +03:00
|
|
|
void qemu_rec_mutex_lock_impl(QemuRecMutex *mutex, const char *file, int line)
|
2016-10-27 13:49:07 +03:00
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(mutex->initialized);
|
2016-10-27 13:49:07 +03:00
|
|
|
EnterCriticalSection(&mutex->lock);
|
|
|
|
}
|
|
|
|
|
2017-08-08 20:53:15 +03:00
|
|
|
int qemu_rec_mutex_trylock_impl(QemuRecMutex *mutex, const char *file, int line)
|
2016-10-27 13:49:07 +03:00
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(mutex->initialized);
|
2016-10-27 13:49:07 +03:00
|
|
|
return !TryEnterCriticalSection(&mutex->lock);
|
|
|
|
}
|
|
|
|
|
2021-06-15 02:31:39 +03:00
|
|
|
void qemu_rec_mutex_unlock_impl(QemuRecMutex *mutex, const char *file, int line)
|
2016-10-27 13:49:07 +03:00
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(mutex->initialized);
|
2016-10-27 13:49:07 +03:00
|
|
|
LeaveCriticalSection(&mutex->lock);
|
|
|
|
}
|
|
|
|
|
2011-03-12 19:43:52 +03:00
|
|
|
void qemu_cond_init(QemuCond *cond)
|
|
|
|
{
|
|
|
|
memset(cond, 0, sizeof(*cond));
|
2017-03-25 01:01:41 +03:00
|
|
|
InitializeConditionVariable(&cond->var);
|
2017-07-04 15:23:25 +03:00
|
|
|
cond->initialized = true;
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-13 21:00:52 +03:00
|
|
|
void qemu_cond_destroy(QemuCond *cond)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(cond->initialized);
|
|
|
|
cond->initialized = false;
|
2017-03-25 01:01:41 +03:00
|
|
|
InitializeConditionVariable(&cond->var);
|
2011-03-13 21:00:52 +03:00
|
|
|
}
|
|
|
|
|
2011-03-12 19:43:52 +03:00
|
|
|
void qemu_cond_signal(QemuCond *cond)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(cond->initialized);
|
2017-03-25 01:01:41 +03:00
|
|
|
WakeConditionVariable(&cond->var);
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_cond_broadcast(QemuCond *cond)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(cond->initialized);
|
2017-03-25 01:01:41 +03:00
|
|
|
WakeAllConditionVariable(&cond->var);
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-11 11:27:16 +03:00
|
|
|
void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line)
|
2011-03-12 19:43:52 +03:00
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(cond->initialized);
|
2018-04-25 05:54:57 +03:00
|
|
|
qemu_mutex_pre_unlock(mutex, file, line);
|
2017-03-25 01:01:41 +03:00
|
|
|
SleepConditionVariableSRW(&cond->var, &mutex->lock, INFINITE, 0);
|
2018-04-25 05:54:57 +03:00
|
|
|
qemu_mutex_post_lock(mutex, file, line);
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2019-09-09 16:13:33 +03:00
|
|
|
bool qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms,
|
|
|
|
const char *file, const int line)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
assert(cond->initialized);
|
|
|
|
trace_qemu_mutex_unlock(mutex, file, line);
|
|
|
|
if (!SleepConditionVariableSRW(&cond->var, &mutex->lock, ms, 0)) {
|
|
|
|
rc = GetLastError();
|
|
|
|
}
|
|
|
|
trace_qemu_mutex_locked(mutex, file, line);
|
|
|
|
if (rc && rc != ERROR_TIMEOUT) {
|
|
|
|
error_exit(rc, __func__);
|
|
|
|
}
|
|
|
|
return rc != ERROR_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2011-08-08 16:36:41 +04:00
|
|
|
void qemu_sem_init(QemuSemaphore *sem, int init)
|
|
|
|
{
|
|
|
|
/* Manual reset. */
|
|
|
|
sem->sema = CreateSemaphore(NULL, init, LONG_MAX, NULL);
|
2017-07-04 15:23:25 +03:00
|
|
|
sem->initialized = true;
|
2011-08-08 16:36:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_sem_destroy(QemuSemaphore *sem)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(sem->initialized);
|
|
|
|
sem->initialized = false;
|
2011-08-08 16:36:41 +04:00
|
|
|
CloseHandle(sem->sema);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_sem_post(QemuSemaphore *sem)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(sem->initialized);
|
2011-08-08 16:36:41 +04:00
|
|
|
ReleaseSemaphore(sem->sema, 1, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemu_sem_timedwait(QemuSemaphore *sem, int ms)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
assert(sem->initialized);
|
|
|
|
rc = WaitForSingleObject(sem->sema, ms);
|
2011-08-08 16:36:41 +04:00
|
|
|
if (rc == WAIT_OBJECT_0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (rc != WAIT_TIMEOUT) {
|
|
|
|
error_exit(GetLastError(), __func__);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_sem_wait(QemuSemaphore *sem)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(sem->initialized);
|
2011-08-08 16:36:41 +04:00
|
|
|
if (WaitForSingleObject(sem->sema, INFINITE) != WAIT_OBJECT_0) {
|
|
|
|
error_exit(GetLastError(), __func__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 16:38:18 +03:00
|
|
|
/* Wrap a Win32 manual-reset event with a fast userspace path. The idea
|
|
|
|
* is to reset the Win32 event lazily, as part of a test-reset-test-wait
|
|
|
|
* sequence. Such a sequence is, indeed, how QemuEvents are used by
|
|
|
|
* RCU and other subsystems!
|
|
|
|
*
|
|
|
|
* Valid transitions:
|
|
|
|
* - free->set, when setting the event
|
2017-01-12 21:07:54 +03:00
|
|
|
* - busy->set, when setting the event, followed by SetEvent
|
2015-08-12 16:38:18 +03:00
|
|
|
* - set->free, when resetting the event
|
|
|
|
* - free->busy, when waiting
|
|
|
|
*
|
|
|
|
* set->busy does not happen (it can be observed from the outside but
|
|
|
|
* it really is set->free->busy).
|
|
|
|
*
|
|
|
|
* busy->free provably cannot happen; to enforce it, the set->free transition
|
|
|
|
* is done with an OR, which becomes a no-op if the event has concurrently
|
|
|
|
* transitioned to free or busy (and is faster than cmpxchg).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define EV_SET 0
|
|
|
|
#define EV_FREE 1
|
|
|
|
#define EV_BUSY -1
|
|
|
|
|
2013-09-25 10:20:59 +04:00
|
|
|
void qemu_event_init(QemuEvent *ev, bool init)
|
|
|
|
{
|
|
|
|
/* Manual reset. */
|
2015-08-12 16:38:18 +03:00
|
|
|
ev->event = CreateEvent(NULL, TRUE, TRUE, NULL);
|
|
|
|
ev->value = (init ? EV_SET : EV_FREE);
|
2017-07-04 15:23:25 +03:00
|
|
|
ev->initialized = true;
|
2013-09-25 10:20:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_event_destroy(QemuEvent *ev)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(ev->initialized);
|
|
|
|
ev->initialized = false;
|
2013-09-25 10:20:59 +04:00
|
|
|
CloseHandle(ev->event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_event_set(QemuEvent *ev)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(ev->initialized);
|
2023-03-02 13:22:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pairs with both qemu_event_reset() and qemu_event_wait().
|
|
|
|
*
|
|
|
|
* qemu_event_set has release semantics, but because it *loads*
|
2016-09-19 12:10:57 +03:00
|
|
|
* ev->value we need a full memory barrier here.
|
|
|
|
*/
|
|
|
|
smp_mb();
|
2020-09-23 13:56:46 +03:00
|
|
|
if (qatomic_read(&ev->value) != EV_SET) {
|
2023-03-02 13:22:50 +03:00
|
|
|
int old = qatomic_xchg(&ev->value, EV_SET);
|
|
|
|
|
|
|
|
/* Pairs with memory barrier after ResetEvent. */
|
|
|
|
smp_mb__after_rmw();
|
|
|
|
if (old == EV_BUSY) {
|
2015-08-12 16:38:18 +03:00
|
|
|
/* There were waiters, wake them up. */
|
|
|
|
SetEvent(ev->event);
|
|
|
|
}
|
|
|
|
}
|
2013-09-25 10:20:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_event_reset(QemuEvent *ev)
|
|
|
|
{
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(ev->initialized);
|
2023-03-02 13:22:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there was a concurrent reset (or even reset+wait),
|
|
|
|
* do nothing. Otherwise change EV_SET->EV_FREE.
|
|
|
|
*/
|
|
|
|
qatomic_or(&ev->value, EV_FREE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Order reset before checking the condition in the caller.
|
|
|
|
* Pairs with the first memory barrier in qemu_event_set().
|
|
|
|
*/
|
|
|
|
smp_mb__after_rmw();
|
2013-09-25 10:20:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_event_wait(QemuEvent *ev)
|
|
|
|
{
|
2015-08-12 16:38:18 +03:00
|
|
|
unsigned value;
|
|
|
|
|
2017-07-04 15:23:25 +03:00
|
|
|
assert(ev->initialized);
|
2023-03-02 13:22:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* qemu_event_wait must synchronize with qemu_event_set even if it does
|
|
|
|
* not go down the slow path, so this load-acquire is needed that
|
|
|
|
* synchronizes with the first memory barrier in qemu_event_set().
|
|
|
|
*
|
|
|
|
* If we do go down the slow path, there is no requirement at all: we
|
|
|
|
* might miss a qemu_event_set() here but ultimately the memory barrier in
|
|
|
|
* qemu_futex_wait() will ensure the check is done correctly.
|
|
|
|
*/
|
|
|
|
value = qatomic_load_acquire(&ev->value);
|
2015-08-12 16:38:18 +03:00
|
|
|
if (value != EV_SET) {
|
|
|
|
if (value == EV_FREE) {
|
2023-03-02 13:22:50 +03:00
|
|
|
/*
|
|
|
|
* Here the underlying kernel event is reset, but qemu_event_set is
|
|
|
|
* not yet going to call SetEvent. However, there will be another
|
|
|
|
* check for EV_SET below when setting EV_BUSY. At that point it
|
|
|
|
* is safe to call WaitForSingleObject.
|
2015-08-12 16:38:18 +03:00
|
|
|
*/
|
|
|
|
ResetEvent(ev->event);
|
|
|
|
|
2023-03-02 13:22:50 +03:00
|
|
|
/*
|
|
|
|
* It is not clear whether ResetEvent provides this barrier; kernel
|
|
|
|
* APIs (KeResetEvent/KeClearEvent) do not. Better safe than sorry!
|
|
|
|
*/
|
|
|
|
smp_mb();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Leave the event reset and tell qemu_event_set that there are
|
|
|
|
* waiters. No need to retry, because there cannot be a concurrent
|
|
|
|
* busy->free transition. After the CAS, the event will be either
|
|
|
|
* set or busy.
|
2015-08-12 16:38:18 +03:00
|
|
|
*/
|
2020-09-23 13:56:46 +03:00
|
|
|
if (qatomic_cmpxchg(&ev->value, EV_FREE, EV_BUSY) == EV_SET) {
|
2023-03-02 13:22:50 +03:00
|
|
|
return;
|
2015-08-12 16:38:18 +03:00
|
|
|
}
|
|
|
|
}
|
2023-03-02 13:22:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ev->value is now EV_BUSY. Since we didn't observe EV_SET,
|
|
|
|
* qemu_event_set() must observe EV_BUSY and call SetEvent().
|
|
|
|
*/
|
|
|
|
WaitForSingleObject(ev->event, INFINITE);
|
2015-08-12 16:38:18 +03:00
|
|
|
}
|
2013-09-25 10:20:59 +04:00
|
|
|
}
|
|
|
|
|
2011-03-12 19:43:52 +03:00
|
|
|
struct QemuThreadData {
|
2011-12-12 20:21:33 +04:00
|
|
|
/* Passed to win32_start_routine. */
|
|
|
|
void *(*start_routine)(void *);
|
|
|
|
void *arg;
|
|
|
|
short mode;
|
2014-12-02 14:05:45 +03:00
|
|
|
NotifierList exit;
|
2011-12-12 20:21:33 +04:00
|
|
|
|
|
|
|
/* Only used for joinable threads. */
|
|
|
|
bool exited;
|
|
|
|
void *ret;
|
|
|
|
CRITICAL_SECTION cs;
|
2011-03-12 19:43:52 +03:00
|
|
|
};
|
|
|
|
|
2014-12-02 14:05:45 +03:00
|
|
|
static bool atexit_registered;
|
|
|
|
static NotifierList main_thread_exit;
|
|
|
|
|
2012-11-23 15:12:01 +04:00
|
|
|
static __thread QemuThreadData *qemu_thread_data;
|
2011-03-12 19:43:52 +03:00
|
|
|
|
2014-12-02 14:05:45 +03:00
|
|
|
static void run_main_thread_exit(void)
|
|
|
|
{
|
|
|
|
notifier_list_notify(&main_thread_exit, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_thread_atexit_add(Notifier *notifier)
|
|
|
|
{
|
|
|
|
if (!qemu_thread_data) {
|
|
|
|
if (!atexit_registered) {
|
|
|
|
atexit_registered = true;
|
|
|
|
atexit(run_main_thread_exit);
|
|
|
|
}
|
|
|
|
notifier_list_add(&main_thread_exit, notifier);
|
|
|
|
} else {
|
|
|
|
notifier_list_add(&qemu_thread_data->exit, notifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_thread_atexit_remove(Notifier *notifier)
|
|
|
|
{
|
|
|
|
notifier_remove(notifier);
|
|
|
|
}
|
|
|
|
|
2011-03-12 19:43:52 +03:00
|
|
|
static unsigned __stdcall win32_start_routine(void *arg)
|
|
|
|
{
|
2011-12-12 20:21:33 +04:00
|
|
|
QemuThreadData *data = (QemuThreadData *) arg;
|
|
|
|
void *(*start_routine)(void *) = data->start_routine;
|
|
|
|
void *thread_arg = data->arg;
|
|
|
|
|
2012-11-23 15:12:01 +04:00
|
|
|
qemu_thread_data = data;
|
2011-12-12 20:21:33 +04:00
|
|
|
qemu_thread_exit(start_routine(thread_arg));
|
2011-03-12 19:43:52 +03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_thread_exit(void *arg)
|
|
|
|
{
|
2012-11-23 15:12:01 +04:00
|
|
|
QemuThreadData *data = qemu_thread_data;
|
|
|
|
|
2014-12-02 14:05:45 +03:00
|
|
|
notifier_list_notify(&data->exit, NULL);
|
|
|
|
if (data->mode == QEMU_THREAD_JOINABLE) {
|
2011-12-12 20:21:33 +04:00
|
|
|
data->ret = arg;
|
|
|
|
EnterCriticalSection(&data->cs);
|
|
|
|
data->exited = true;
|
|
|
|
LeaveCriticalSection(&data->cs);
|
2014-12-02 14:05:45 +03:00
|
|
|
} else {
|
|
|
|
g_free(data);
|
2011-12-12 20:21:33 +04:00
|
|
|
}
|
|
|
|
_endthreadex(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *qemu_thread_join(QemuThread *thread)
|
|
|
|
{
|
|
|
|
QemuThreadData *data;
|
|
|
|
void *ret;
|
|
|
|
HANDLE handle;
|
|
|
|
|
|
|
|
data = thread->data;
|
2014-12-02 14:05:45 +03:00
|
|
|
if (data->mode == QEMU_THREAD_DETACHED) {
|
2011-12-12 20:21:33 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-12-02 14:05:45 +03:00
|
|
|
|
2011-12-12 20:21:33 +04:00
|
|
|
/*
|
|
|
|
* Because multiple copies of the QemuThread can exist via
|
|
|
|
* qemu_thread_get_self, we need to store a value that cannot
|
|
|
|
* leak there. The simplest, non racy way is to store the TID,
|
|
|
|
* discard the handle that _beginthreadex gives back, and
|
|
|
|
* get another copy of the handle here.
|
|
|
|
*/
|
2011-12-13 16:43:52 +04:00
|
|
|
handle = qemu_thread_get_handle(thread);
|
|
|
|
if (handle) {
|
2011-12-12 20:21:33 +04:00
|
|
|
WaitForSingleObject(handle, INFINITE);
|
|
|
|
CloseHandle(handle);
|
|
|
|
}
|
|
|
|
ret = data->ret;
|
|
|
|
DeleteCriticalSection(&data->cs);
|
|
|
|
g_free(data);
|
|
|
|
return ret;
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2022-09-13 20:53:20 +03:00
|
|
|
static bool set_thread_description(HANDLE h, const char *name)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
g_autofree wchar_t *namew = NULL;
|
|
|
|
|
|
|
|
if (!load_set_thread_description()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
namew = g_utf8_to_utf16(name, -1, NULL, NULL, NULL);
|
|
|
|
if (!namew) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = SetThreadDescriptionFunc(h, namew);
|
|
|
|
|
|
|
|
return SUCCEEDED(hr);
|
|
|
|
}
|
|
|
|
|
2014-01-30 14:20:32 +04:00
|
|
|
void qemu_thread_create(QemuThread *thread, const char *name,
|
2011-03-12 19:43:52 +03:00
|
|
|
void *(*start_routine)(void *),
|
2011-12-12 20:21:31 +04:00
|
|
|
void *arg, int mode)
|
2011-03-12 19:43:52 +03:00
|
|
|
{
|
|
|
|
HANDLE hThread;
|
|
|
|
struct QemuThreadData *data;
|
2012-11-23 15:12:01 +04:00
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
data = g_malloc(sizeof *data);
|
2011-03-12 19:43:52 +03:00
|
|
|
data->start_routine = start_routine;
|
|
|
|
data->arg = arg;
|
2011-12-12 20:21:33 +04:00
|
|
|
data->mode = mode;
|
|
|
|
data->exited = false;
|
2014-12-02 14:05:45 +03:00
|
|
|
notifier_list_init(&data->exit);
|
2011-03-12 19:43:52 +03:00
|
|
|
|
2012-01-31 10:14:15 +04:00
|
|
|
if (data->mode != QEMU_THREAD_DETACHED) {
|
|
|
|
InitializeCriticalSection(&data->cs);
|
|
|
|
}
|
|
|
|
|
2011-03-12 19:43:52 +03:00
|
|
|
hThread = (HANDLE) _beginthreadex(NULL, 0, win32_start_routine,
|
2011-12-12 20:21:33 +04:00
|
|
|
data, 0, &thread->tid);
|
2011-03-12 19:43:52 +03:00
|
|
|
if (!hThread) {
|
|
|
|
error_exit(GetLastError(), __func__);
|
|
|
|
}
|
2022-09-13 20:53:20 +03:00
|
|
|
if (name_threads && name && !set_thread_description(hThread, name)) {
|
|
|
|
fprintf(stderr, "qemu: failed to set thread description: %s\n", name);
|
|
|
|
}
|
2011-03-12 19:43:52 +03:00
|
|
|
CloseHandle(hThread);
|
2022-09-13 20:53:20 +03:00
|
|
|
|
2014-12-02 14:05:45 +03:00
|
|
|
thread->data = data;
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2022-10-14 16:47:15 +03:00
|
|
|
int qemu_thread_set_affinity(QemuThread *thread, unsigned long *host_cpus,
|
|
|
|
unsigned long nbits)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qemu_thread_get_affinity(QemuThread *thread, unsigned long **host_cpus,
|
|
|
|
unsigned long *nbits)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2011-03-12 19:43:52 +03:00
|
|
|
void qemu_thread_get_self(QemuThread *thread)
|
|
|
|
{
|
2012-11-23 15:12:01 +04:00
|
|
|
thread->data = qemu_thread_data;
|
2011-12-12 20:21:33 +04:00
|
|
|
thread->tid = GetCurrentThreadId();
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|
|
|
|
|
2011-12-13 16:43:52 +04:00
|
|
|
HANDLE qemu_thread_get_handle(QemuThread *thread)
|
|
|
|
{
|
|
|
|
QemuThreadData *data;
|
|
|
|
HANDLE handle;
|
|
|
|
|
|
|
|
data = thread->data;
|
2014-12-02 14:05:45 +03:00
|
|
|
if (data->mode == QEMU_THREAD_DETACHED) {
|
2011-12-13 16:43:52 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnterCriticalSection(&data->cs);
|
|
|
|
if (!data->exited) {
|
2017-01-10 13:59:57 +03:00
|
|
|
handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME |
|
|
|
|
THREAD_SET_CONTEXT, FALSE, thread->tid);
|
2011-12-13 16:43:52 +04:00
|
|
|
} else {
|
|
|
|
handle = NULL;
|
|
|
|
}
|
|
|
|
LeaveCriticalSection(&data->cs);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2012-05-02 19:21:31 +04:00
|
|
|
bool qemu_thread_is_self(QemuThread *thread)
|
2011-03-12 19:43:52 +03:00
|
|
|
{
|
2011-12-12 20:21:33 +04:00
|
|
|
return GetCurrentThreadId() == thread->tid;
|
2011-03-12 19:43:52 +03:00
|
|
|
}
|