Skip to contentSkip to conversation

fix win32 build

PATCH Applied Paolo Bonzini 2 participants 2 messages Dec 13, 2011 – Dec 15, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix win32 build

On Windows, cpus.c needs access to the hThread. Add a Windows-specific function to grab it. This requires changing the CPU threads to joinable. There is no substantial change because the threads run in an infinite loop.

---
 cpu-defs.h          |    9 +++++++++
 cpus.c              |   11 +++++++----
 qemu-thread-win32.c |   29 +++++++++++++++++++++++------
 qemu-thread-win32.h |    3 +++
 4 files changed, 42 insertions(+), 10 deletions(-)
cpu-defs.h +9 -0
index db48a7a..57a709b 100644
@@ -153,6 +153,14 @@ typedef struct CPUWatchpoint {
     QTAILQ_ENTRY(CPUWatchpoint) entry;
 } CPUWatchpoint;
 
+#ifdef _WIN32
+#define CPU_COMMON_THREAD \
+    void *hThread;
+
+#else
+#define CPU_COMMON_THREAD
+#endif
+
 #define CPU_TEMP_BUF_NLONGS 128
 #define CPU_COMMON                                                      \
     struct TranslationBlock *current_tb; /* currently executing TB  */  \
@@ -211,6 +219,7 @@ typedef struct CPUWatchpoint {
     uint32_t stop;   /* Stop request */                                 \
     uint32_t stopped; /* Artificially stopped */                        \
     struct QemuThread *thread;                                          \
+    CPU_COMMON_THREAD                                                   \
     struct QemuCond *halt_cond;                                         \
     int thread_kicked;                                                  \
     struct qemu_work_item *queued_work_first, *queued_work_last;        \
cpus.c +7 -4
index 1ada0f5..11893d5 100644
@@ -793,9 +793,9 @@ static void qemu_cpu_kick_thread(CPUState *env)
     }
 #else /* _WIN32 */
     if (!qemu_cpu_is_self(env)) {
-        SuspendThread(env->thread->thread);
+        SuspendThread(env->hThread);
         cpu_signal(0);
-        ResumeThread(env->thread->thread);
+        ResumeThread(env->hThread);
     }
 #endif
 }
@@ -911,7 +911,10 @@ static void qemu_tcg_init_vcpu(void *_env)
         qemu_cond_init(env->halt_cond);
         tcg_halt_cond = env->halt_cond;
         qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env,
-                           QEMU_THREAD_DETACHED);
+                           QEMU_THREAD_JOINABLE);
+#ifdef _WIN32
+        env->hThread = qemu_thread_get_handle(env->thread);
+#endif
         while (env->created == 0) {
             qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
         }
@@ -928,7 +931,7 @@ static void qemu_kvm_start_vcpu(CPUState *env)
     env->halt_cond = g_malloc0(sizeof(QemuCond));
     qemu_cond_init(env->halt_cond);
     qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env,
-                       QEMU_THREAD_DETACHED);
+                       QEMU_THREAD_JOINABLE);
     while (env->created == 0) {
         qemu_cond_wait(&qemu_cpu_cond, &qemu_global_mutex);
     }
qemu-thread-win32.c +23 -6
index a13ffcc..fe9b931 100644
@@ -252,14 +252,10 @@ void *qemu_thread_join(QemuThread *thread)
      * discard the handle that _beginthreadex gives back, and
      * get another copy of the handle here.
      */
-    EnterCriticalSection(&data->cs);
-    if (!data->exited) {
-        handle = OpenThread(SYNCHRONIZE, FALSE, thread->tid);
-        LeaveCriticalSection(&data->cs);
+    handle = qemu_thread_get_handle(thread);
+    if (handle) {
         WaitForSingleObject(handle, INFINITE);
         CloseHandle(handle);
-    } else {
-        LeaveCriticalSection(&data->cs);
     }
     ret = data->ret;
     DeleteCriticalSection(&data->cs);
@@ -308,6 +304,27 @@ void qemu_thread_get_self(QemuThread *thread)
     thread->tid = GetCurrentThreadId();
 }
 
+HANDLE qemu_thread_get_handle(QemuThread *thread)
+{
+    QemuThreadData *data;
+    HANDLE handle;
+
+    data = thread->data;
+    if (!data) {
+        return NULL;
+    }
+
+    EnterCriticalSection(&data->cs);
+    if (!data->exited) {
+        handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME, FALSE,
+                            thread->tid);
+    } else {
+        handle = NULL;
+    }
+    LeaveCriticalSection(&data->cs);
+    return handle;
+}
+
 int qemu_thread_is_self(QemuThread *thread)
 {
     return GetCurrentThreadId() == thread->tid;
qemu-thread-win32.h +3 -0
index 2983490..b9d1be8 100644
@@ -19,4 +19,7 @@ struct QemuThread {
     unsigned tid;
 };
 
+/* Only valid for joinable threads.  */
+HANDLE qemu_thread_get_handle(QemuThread *thread);
+
 #endif
Signature · 1 line
1.7.7.1
#1 lore raw
Quoted 6 lines · Paolo Bonzini

On Windows, cpus.c needs access to the hThread. Add a Windows-specific function to grab it. This requires changing the CPU threads to joinable. There is no substantial change because the threads run in an infinite loop.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 132 lines
---
  cpu-defs.h          |    9 +++++++++
  cpus.c              |   11 +++++++----
  qemu-thread-win32.c |   29 +++++++++++++++++++++++------
  qemu-thread-win32.h |    3 +++
  4 files changed, 42 insertions(+), 10 deletions(-)
cpu-defs.h +9 -0
index db48a7a..57a709b 100644
@@ -153,6 +153,14 @@ typedef struct CPUWatchpoint {
      QTAILQ_ENTRY(CPUWatchpoint) entry;
  } CPUWatchpoint;
 
+#ifdef _WIN32
+#define CPU_COMMON_THREAD \
+    void *hThread;
+
+#else
+#define CPU_COMMON_THREAD
+#endif
+
  #define CPU_TEMP_BUF_NLONGS 128
  #define CPU_COMMON                                                      \
      struct TranslationBlock *current_tb; /* currently executing TB  */  \
@@ -211,6 +219,7 @@ typedef struct CPUWatchpoint {
      uint32_t stop;   /* Stop request */                                 \
      uint32_t stopped; /* Artificially stopped */                        \
      struct QemuThread *thread;                                          \
+    CPU_COMMON_THREAD                                                   \
      struct QemuCond *halt_cond;                                         \
      int thread_kicked;                                                  \
      struct qemu_work_item *queued_work_first, *queued_work_last;        \
cpus.c +7 -4
index 1ada0f5..11893d5 100644
@@ -793,9 +793,9 @@ static void qemu_cpu_kick_thread(CPUState *env)
      }
  #else /* _WIN32 */
      if (!qemu_cpu_is_self(env)) {
-        SuspendThread(env->thread->thread);
+        SuspendThread(env->hThread);
          cpu_signal(0);
-        ResumeThread(env->thread->thread);
+        ResumeThread(env->hThread);
      }
  #endif
  }
@@ -911,7 +911,10 @@ static void qemu_tcg_init_vcpu(void *_env)
          qemu_cond_init(env->halt_cond);
          tcg_halt_cond = env->halt_cond;
          qemu_thread_create(env->thread, qemu_tcg_cpu_thread_fn, env,
-                           QEMU_THREAD_DETACHED);
+                           QEMU_THREAD_JOINABLE);
+#ifdef _WIN32
+        env->hThread = qemu_thread_get_handle(env->thread);
+#endif
          while (env->created == 0) {
              qemu_cond_wait(&qemu_cpu_cond,&qemu_global_mutex);
          }
@@ -928,7 +931,7 @@ static void qemu_kvm_start_vcpu(CPUState *env)
      env->halt_cond = g_malloc0(sizeof(QemuCond));
      qemu_cond_init(env->halt_cond);
      qemu_thread_create(env->thread, qemu_kvm_cpu_thread_fn, env,
-                       QEMU_THREAD_DETACHED);
+                       QEMU_THREAD_JOINABLE);
      while (env->created == 0) {
          qemu_cond_wait(&qemu_cpu_cond,&qemu_global_mutex);
      }
qemu-thread-win32.c +23 -6
index a13ffcc..fe9b931 100644
@@ -252,14 +252,10 @@ void *qemu_thread_join(QemuThread *thread)
       * discard the handle that _beginthreadex gives back, and
       * get another copy of the handle here.
       */
-    EnterCriticalSection(&data->cs);
-    if (!data->exited) {
-        handle = OpenThread(SYNCHRONIZE, FALSE, thread->tid);
-        LeaveCriticalSection(&data->cs);
+    handle = qemu_thread_get_handle(thread);
+    if (handle) {
          WaitForSingleObject(handle, INFINITE);
          CloseHandle(handle);
-    } else {
-        LeaveCriticalSection(&data->cs);
      }
      ret = data->ret;
      DeleteCriticalSection(&data->cs);
@@ -308,6 +304,27 @@ void qemu_thread_get_self(QemuThread *thread)
      thread->tid = GetCurrentThreadId();
  }
 
+HANDLE qemu_thread_get_handle(QemuThread *thread)
+{
+    QemuThreadData *data;
+    HANDLE handle;
+
+    data = thread->data;
+    if (!data) {
+        return NULL;
+    }
+
+    EnterCriticalSection(&data->cs);
+    if (!data->exited) {
+        handle = OpenThread(SYNCHRONIZE | THREAD_SUSPEND_RESUME, FALSE,
+                            thread->tid);
+    } else {
+        handle = NULL;
+    }
+    LeaveCriticalSection(&data->cs);
+    return handle;
+}
+
  int qemu_thread_is_self(QemuThread *thread)
  {
      return GetCurrentThreadId() == thread->tid;
qemu-thread-win32.h +3 -0
index 2983490..b9d1be8 100644
@@ -19,4 +19,7 @@ struct QemuThread {
      unsigned tid;
  };
 
+/* Only valid for joinable threads.  */
+HANDLE qemu_thread_get_handle(QemuThread *thread);
+
  #endif