From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:41079) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbArG-0001aX-Lv for qemu-devel@nongnu.org; Wed, 21 Nov 2012 09:01:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TbArE-0004Ms-Jp for qemu-devel@nongnu.org; Wed, 21 Nov 2012 09:00:54 -0500 Received: from goliath.siemens.de ([192.35.17.28]:27082) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TbArE-0004ME-9x for qemu-devel@nongnu.org; Wed, 21 Nov 2012 09:00:52 -0500 Message-ID: <50ACDE8F.5010507@siemens.com> Date: Wed, 21 Nov 2012 15:00:47 +0100 From: Jan Kiszka MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] [PATCH] coroutine: Fix win32 variant for older mingw32 compilers List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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 --- 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