qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] coroutine: Fix win32 variant for older mingw32 compilers
@ 2012-11-21 14:00 Jan Kiszka
  2012-11-21 14:08 ` Paolo Bonzini
  0 siblings, 1 reply; 12+ messages in thread
From: Jan Kiszka @ 2012-11-21 14:00 UTC (permalink / raw)
  To: Kevin Wolf, qemu-devel; +Cc: Paolo Bonzini, Stefan Weil

mingw32 with gcc up to (at least) 4.4 has broken __thread support.
This means all __thread variables silently become global ones.

Address this by switching the coroutine implementation for win32 to
Get/SetTlsValue.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 coroutine-win32.c |   29 ++++++++++++++++++++++-------
 1 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/coroutine-win32.c b/coroutine-win32.c
index 4179609..bf0aeac 100644
--- a/coroutine-win32.c
+++ b/coroutine-win32.c
@@ -33,8 +33,7 @@ typedef struct
     CoroutineAction action;
 } CoroutineWin32;
 
-static __thread CoroutineWin32 leader;
-static __thread Coroutine *current;
+static int current_tls_index = TLS_OUT_OF_INDEXES;
 
 CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
                                       CoroutineAction action)
@@ -42,7 +41,7 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_,
     CoroutineWin32 *from = DO_UPCAST(CoroutineWin32, base, from_);
     CoroutineWin32 *to = DO_UPCAST(CoroutineWin32, base, to_);
 
-    current = to_;
+    TlsSetValue(current_tls_index, to_);
 
     to->action = action;
     SwitchToFiber(to->fiber);
@@ -79,14 +78,30 @@ void qemu_coroutine_delete(Coroutine *co_)
 
 Coroutine *qemu_coroutine_self(void)
 {
-    if (!current) {
-        current = &leader.base;
-        leader.fiber = ConvertThreadToFiber(NULL);
+    CoroutineWin32 *leader;
+
+    if (current_tls_index == TLS_OUT_OF_INDEXES) {
+        current_tls_index = TlsAlloc();
+        if (current_tls_index == TLS_OUT_OF_INDEXES) {
+            fprintf(stderr, "qemu: %s: out of TLS handles\n", __func__);
+            abort();
+        }
+    }
+    if (!TlsGetValue(current_tls_index)) {
+        leader = g_malloc0(sizeof(*leader));
+        leader->fiber = ConvertThreadToFiber(NULL);
+        TlsSetValue(current_tls_index, &leader->base);
     }
-    return current;
+    return TlsGetValue(current_tls_index);
 }
 
 bool qemu_in_coroutine(void)
 {
+    Coroutine *current;
+
+    if (current_tls_index == TLS_OUT_OF_INDEXES) {
+        return false;
+    }
+    current = TlsGetValue(current_tls_index);
     return current && current->caller;
 }
-- 
1.7.3.4

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-11-22 12:07 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-21 14:00 [Qemu-devel] [PATCH] coroutine: Fix win32 variant for older mingw32 compilers Jan Kiszka
2012-11-21 14:08 ` Paolo Bonzini
2012-11-21 14:29   ` Jan Kiszka
2012-11-21 14:33     ` malc
2012-11-21 14:38       ` Paolo Bonzini
2012-11-21 14:49         ` Jan Kiszka
2012-11-21 15:44           ` Jan Kiszka
2012-11-21 15:54             ` Paolo Bonzini
2012-11-21 16:00               ` Jan Kiszka
2012-11-21 19:11         ` Stefan Weil
2012-11-22  8:59           ` Paolo Bonzini
2012-11-22 12:07           ` Jan Kiszka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).