From cd2b6eb83cb34349183e1098cb2f80106f50a3f2 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Mon, 8 Apr 2013 03:48:39 +0200 Subject: [PATCH] Replaced __declspec(thread) with TlsAlloc. Variables created within a DLL with __declspec(thread) may not get a TLS slot on Windows XP, leading to segfaults on use. Moving to TlsAlloc works around this. --- src/wgl_context.c | 36 ++++++++++++++++-------------------- src/wgl_platform.h | 3 ++- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/wgl_context.c b/src/wgl_context.c index a87c50b2..ddaa6245 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -35,22 +35,6 @@ #include -// Thread local storage attribute macro -// -#if defined(_MSC_VER) - #define _GLFW_TLS __declspec(thread) -#elif defined(__GNUC__) - #define _GLFW_TLS __thread -#else - #define _GLFW_TLS -#endif - - -// The per-thread current context/window pointer -// -static _GLFW_TLS _GLFWwindow* _glfwCurrentWindow = NULL; - - // Initialize WGL-specific extensions // This function is called once before initial context creation, i.e. before // any WGL extensions could be present. This is done in order to have both @@ -149,6 +133,16 @@ static void initWGLExtensions(_GLFWwindow* window) // int _glfwInitContextAPI(void) { + _glfw.wgl.tls = TlsAlloc(); + if (_glfw.wgl.tls == TLS_OUT_OF_INDEXES) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "WGL: Failed to allocate TLS index"); + return GL_FALSE; + } + + _glfw.wgl.hasTLS = GL_TRUE; + return GL_TRUE; } @@ -156,6 +150,8 @@ int _glfwInitContextAPI(void) // void _glfwTerminateContextAPI(void) { + if (_glfw.wgl.hasTLS) + TlsFree(_glfw.wgl.tls); } #define setWGLattrib(attribName, attribValue) \ @@ -516,12 +512,12 @@ void _glfwPlatformMakeContextCurrent(_GLFWwindow* window) else wglMakeCurrent(NULL, NULL); - _glfwCurrentWindow = window; + TlsSetValue(_glfw.wgl.tls, window); } _GLFWwindow* _glfwPlatformGetCurrentContext(void) { - return _glfwCurrentWindow; + return TlsGetValue(_glfw.wgl.tls); } void _glfwPlatformSwapBuffers(_GLFWwindow* window) @@ -531,7 +527,7 @@ void _glfwPlatformSwapBuffers(_GLFWwindow* window) void _glfwPlatformSwapInterval(int interval) { - _GLFWwindow* window = _glfwCurrentWindow; + _GLFWwindow* window = _glfwPlatformGetCurrentContext(); if (_glfwIsCompositionEnabled()) { @@ -548,7 +544,7 @@ int _glfwPlatformExtensionSupported(const char* extension) { const GLubyte* extensions; - _GLFWwindow* window = _glfwCurrentWindow; + _GLFWwindow* window = _glfwPlatformGetCurrentContext(); if (window->wgl.GetExtensionsStringEXT != NULL) { diff --git a/src/wgl_platform.h b/src/wgl_platform.h index 5983aef5..b9781282 100644 --- a/src/wgl_platform.h +++ b/src/wgl_platform.h @@ -76,7 +76,8 @@ typedef struct _GLFWcontextWGL //------------------------------------------------------------------------ typedef struct _GLFWlibraryWGL { - int dummy; + GLboolean hasTLS; + DWORD tls; } _GLFWlibraryWGL;