Patchwork win32: Switch thread abstraction to us TLS variable internally

login
register
mail settings
Submitter Jan Kiszka
Date Nov. 23, 2012, 11:12 a.m.
Message ID <50AF5A01.9010601@siemens.com>
Download mbox | patch
Permalink /patch/201293/
State New
Headers show

Comments

Jan Kiszka - Nov. 23, 2012, 11:12 a.m.
We already depend on working __thread support for coroutines, so this
complication here is no longer needed.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---

Post-1.3 material.

 qemu-thread-win32.c |   24 ++++++------------------
 1 files changed, 6 insertions(+), 18 deletions(-)
Paolo Bonzini - Nov. 23, 2012, 11:17 a.m.
> We already depend on working __thread support for coroutines, so this
> complication here is no longer needed.
> 
> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>

> ---
> 
> Post-1.3 material.
> 
>  qemu-thread-win32.c |   24 ++++++------------------
>  1 files changed, 6 insertions(+), 18 deletions(-)
> 
> diff --git a/qemu-thread-win32.c b/qemu-thread-win32.c
> index 4b3db60..6499ce9 100644
> --- a/qemu-thread-win32.c
> +++ b/qemu-thread-win32.c
> @@ -239,7 +239,7 @@ struct QemuThreadData {
>      CRITICAL_SECTION  cs;
>  };
>  
> -static int qemu_thread_tls_index = TLS_OUT_OF_INDEXES;
> +static __thread QemuThreadData *qemu_thread_data;
>  
>  static unsigned __stdcall win32_start_routine(void *arg)
>  {
> @@ -251,14 +251,15 @@ static unsigned __stdcall
> win32_start_routine(void *arg)
>          g_free(data);
>          data = NULL;
>      }
> -    TlsSetValue(qemu_thread_tls_index, data);
> +    qemu_thread_data = data;
>      qemu_thread_exit(start_routine(thread_arg));
>      abort();
>  }
>  
>  void qemu_thread_exit(void *arg)
>  {
> -    QemuThreadData *data = TlsGetValue(qemu_thread_tls_index);
> +    QemuThreadData *data = qemu_thread_data;
> +
>      if (data) {
>          assert(data->mode != QEMU_THREAD_DETACHED);
>          data->ret = arg;
> @@ -298,25 +299,13 @@ void *qemu_thread_join(QemuThread *thread)
>      return ret;
>  }
>  
> -static inline void qemu_thread_init(void)
> -{
> -    if (qemu_thread_tls_index == TLS_OUT_OF_INDEXES) {
> -        qemu_thread_tls_index = TlsAlloc();
> -        if (qemu_thread_tls_index == TLS_OUT_OF_INDEXES) {
> -            error_exit(ERROR_NO_SYSTEM_RESOURCES, __func__);
> -        }
> -    }
> -}
> -
> -
>  void qemu_thread_create(QemuThread *thread,
>                         void *(*start_routine)(void *),
>                         void *arg, int mode)
>  {
>      HANDLE hThread;
> -
>      struct QemuThreadData *data;
> -    qemu_thread_init();
> +
>      data = g_malloc(sizeof *data);
>      data->start_routine = start_routine;
>      data->arg = arg;
> @@ -338,8 +327,7 @@ void qemu_thread_create(QemuThread *thread,
>  
>  void qemu_thread_get_self(QemuThread *thread)
>  {
> -    qemu_thread_init();
> -    thread->data = TlsGetValue(qemu_thread_tls_index);
> +    thread->data = qemu_thread_data;
>      thread->tid = GetCurrentThreadId();
>  }
>  
> --
> 1.7.3.4
>

Patch

diff --git a/qemu-thread-win32.c b/qemu-thread-win32.c
index 4b3db60..6499ce9 100644
--- a/qemu-thread-win32.c
+++ b/qemu-thread-win32.c
@@ -239,7 +239,7 @@  struct QemuThreadData {
     CRITICAL_SECTION  cs;
 };
 
-static int qemu_thread_tls_index = TLS_OUT_OF_INDEXES;
+static __thread QemuThreadData *qemu_thread_data;
 
 static unsigned __stdcall win32_start_routine(void *arg)
 {
@@ -251,14 +251,15 @@  static unsigned __stdcall win32_start_routine(void *arg)
         g_free(data);
         data = NULL;
     }
-    TlsSetValue(qemu_thread_tls_index, data);
+    qemu_thread_data = data;
     qemu_thread_exit(start_routine(thread_arg));
     abort();
 }
 
 void qemu_thread_exit(void *arg)
 {
-    QemuThreadData *data = TlsGetValue(qemu_thread_tls_index);
+    QemuThreadData *data = qemu_thread_data;
+
     if (data) {
         assert(data->mode != QEMU_THREAD_DETACHED);
         data->ret = arg;
@@ -298,25 +299,13 @@  void *qemu_thread_join(QemuThread *thread)
     return ret;
 }
 
-static inline void qemu_thread_init(void)
-{
-    if (qemu_thread_tls_index == TLS_OUT_OF_INDEXES) {
-        qemu_thread_tls_index = TlsAlloc();
-        if (qemu_thread_tls_index == TLS_OUT_OF_INDEXES) {
-            error_exit(ERROR_NO_SYSTEM_RESOURCES, __func__);
-        }
-    }
-}
-
-
 void qemu_thread_create(QemuThread *thread,
                        void *(*start_routine)(void *),
                        void *arg, int mode)
 {
     HANDLE hThread;
-
     struct QemuThreadData *data;
-    qemu_thread_init();
+
     data = g_malloc(sizeof *data);
     data->start_routine = start_routine;
     data->arg = arg;
@@ -338,8 +327,7 @@  void qemu_thread_create(QemuThread *thread,
 
 void qemu_thread_get_self(QemuThread *thread)
 {
-    qemu_thread_init();
-    thread->data = TlsGetValue(qemu_thread_tls_index);
+    thread->data = qemu_thread_data;
     thread->tid = GetCurrentThreadId();
 }