From 8b39ec5b0c4d291a7d01df92a64778355bca8cef Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Wed, 2 Sep 2015 16:13:12 +0800 Subject: [PATCH] initial support to remove a static variable in qemu-thread-win32.c --- include/uc_priv.h | 2 ++ qemu/cpus.c | 4 ++-- qemu/include/qemu/thread.h | 7 ++++--- qemu/util/qemu-thread-posix.c | 6 +++--- qemu/util/qemu-thread-win32.c | 14 ++++++-------- uc.c | 2 +- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/include/uc_priv.h b/include/uc_priv.h index 007fecea..7a51767b 100644 --- a/include/uc_priv.h +++ b/include/uc_priv.h @@ -172,6 +172,8 @@ struct uc_struct { bool block_full; MemoryRegion **mapped_blocks; uint32_t mapped_block_count; + + void *qemu_thread_data; // to support cross compile to Windows (qemu-thread-win32.c) }; #include "qemu_macro.h" diff --git a/qemu/cpus.c b/qemu/cpus.c index ecc66e83..e274fe5a 100644 --- a/qemu/cpus.c +++ b/qemu/cpus.c @@ -124,7 +124,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) struct uc_struct *uc = cpu->uc; //qemu_tcg_init_cpu_signals(); - qemu_thread_get_self(cpu->thread); + qemu_thread_get_self(uc, cpu->thread); qemu_mutex_lock(&uc->qemu_global_mutex); CPU_FOREACH(cpu) { @@ -185,7 +185,7 @@ static void qemu_tcg_init_vcpu(CPUState *cpu) uc->tcg_halt_cond = cpu->halt_cond; snprintf(thread_name, VCPU_THREAD_NAME_SIZE, "CPU %d/TCG", cpu->cpu_index); - qemu_thread_create(cpu->thread, thread_name, qemu_tcg_cpu_thread_fn, + qemu_thread_create(uc, cpu->thread, thread_name, qemu_tcg_cpu_thread_fn, cpu, QEMU_THREAD_JOINABLE); #ifdef _WIN32 cpu->hThread = qemu_thread_get_handle(cpu->thread); diff --git a/qemu/include/qemu/thread.h b/qemu/include/qemu/thread.h index ef2f6962..de5956b2 100644 --- a/qemu/include/qemu/thread.h +++ b/qemu/include/qemu/thread.h @@ -52,12 +52,13 @@ void qemu_event_reset(QemuEvent *ev); void qemu_event_wait(QemuEvent *ev); void qemu_event_destroy(QemuEvent *ev); -void qemu_thread_create(QemuThread *thread, const char *name, +struct uc_struct; +void qemu_thread_create(struct uc_struct *uc, QemuThread *thread, const char *name, void *(*start_routine)(void *), void *arg, int mode); void *qemu_thread_join(QemuThread *thread); -void qemu_thread_get_self(QemuThread *thread); +void qemu_thread_get_self(struct uc_struct *uc, QemuThread *thread); bool qemu_thread_is_self(QemuThread *thread); -void qemu_thread_exit(void *retval); +void qemu_thread_exit(struct uc_struct *uc, void *retval); #endif diff --git a/qemu/util/qemu-thread-posix.c b/qemu/util/qemu-thread-posix.c index 1e8f8304..34632bca 100644 --- a/qemu/util/qemu-thread-posix.c +++ b/qemu/util/qemu-thread-posix.c @@ -389,7 +389,7 @@ void qemu_event_wait(QemuEvent *ev) } } -void qemu_thread_create(QemuThread *thread, const char *name, +void qemu_thread_create(struct uc_struct *uc, QemuThread *thread, const char *name, void *(*start_routine)(void*), void *arg, int mode) { @@ -426,7 +426,7 @@ void qemu_thread_create(QemuThread *thread, const char *name, pthread_attr_destroy(&attr); } -void qemu_thread_get_self(QemuThread *thread) +void qemu_thread_get_self(struct uc_struct *uc, QemuThread *thread) { thread->thread = pthread_self(); } @@ -436,7 +436,7 @@ bool qemu_thread_is_self(QemuThread *thread) return pthread_equal(pthread_self(), thread->thread); } -void qemu_thread_exit(void *retval) +void qemu_thread_exit(struct uc_struct *uc, void *retval) { pthread_exit(retval); } diff --git a/qemu/util/qemu-thread-win32.c b/qemu/util/qemu-thread-win32.c index 004f17bf..51ca88f8 100644 --- a/qemu/util/qemu-thread-win32.c +++ b/qemu/util/qemu-thread-win32.c @@ -266,8 +266,6 @@ struct QemuThreadData { CRITICAL_SECTION cs; }; -static __thread QemuThreadData *qemu_thread_data; - static unsigned __stdcall win32_start_routine(void *arg) { QemuThreadData *data = (QemuThreadData *) arg; @@ -278,14 +276,13 @@ static unsigned __stdcall win32_start_routine(void *arg) g_free(data); data = NULL; } - qemu_thread_data = data; qemu_thread_exit(start_routine(thread_arg)); abort(); } -void qemu_thread_exit(void *arg) +void qemu_thread_exit(struct uc_struct *uc, void *arg) { - QemuThreadData *data = qemu_thread_data; + QemuThreadData *data = uc->qemu_thread_data; if (data) { assert(data->mode != QEMU_THREAD_DETACHED); @@ -326,7 +323,7 @@ void *qemu_thread_join(QemuThread *thread) return ret; } -void qemu_thread_create(QemuThread *thread, const char *name, +void qemu_thread_create(struct uc_struct *uc, QemuThread *thread, const char *name, void *(*start_routine)(void *), void *arg, int mode) { @@ -338,6 +335,7 @@ void qemu_thread_create(QemuThread *thread, const char *name, data->arg = arg; data->mode = mode; data->exited = false; + uc->qemu_thread_data = data; if (data->mode != QEMU_THREAD_DETACHED) { InitializeCriticalSection(&data->cs); @@ -352,9 +350,9 @@ void qemu_thread_create(QemuThread *thread, const char *name, thread->data = (mode == QEMU_THREAD_DETACHED) ? NULL : data; } -void qemu_thread_get_self(QemuThread *thread) +void qemu_thread_get_self(struct uc_struct *uc, QemuThread *thread) { - thread->data = qemu_thread_data; + thread->data = uc->qemu_thread_data; thread->tid = GetCurrentThreadId(); } diff --git a/uc.c b/uc.c index 6d93d0e7..d1584b4f 100644 --- a/uc.c +++ b/uc.c @@ -465,7 +465,7 @@ static void enable_emu_timer(uch handle, uint64_t timeout) struct uc_struct *uc = (struct uc_struct *)handle; uc->timeout = timeout; - qemu_thread_create(&uc->timer, "timeout", _timeout_fn, + qemu_thread_create(uc, &uc->timer, "timeout", _timeout_fn, uc, QEMU_THREAD_JOINABLE); }