diff --git a/src/glx_context.c b/src/glx_context.c index ff244ac1..46b502d3 100644 --- a/src/glx_context.c +++ b/src/glx_context.c @@ -33,6 +33,7 @@ #include #include #include +#include // This is the only glXGetProcAddress variant not declared by glxext.h @@ -44,20 +45,6 @@ void (*glXGetProcAddressEXT(const GLubyte* procName))(); #endif -// Thread local storage attribute macro -// -#if defined(__GNUC__) - #define _GLFW_TLS __thread -#else - #define _GLFW_TLS -#endif - - -// The per-thread current context/window pointer -// -static _GLFW_TLS _GLFWwindow* _glfwCurrentWindow = NULL; - - // Error handler used when creating a context // static int errorHandler(Display *display, XErrorEvent* event) @@ -124,6 +111,13 @@ int _glfwInitContextAPI(void) } #endif + if (pthread_key_create(&_glfw.glx.current, NULL) != 0) + { + _glfwInputError(GLFW_PLATFORM_ERROR, + "GLX: Failed to create context TLS"); + return GL_FALSE; + } + // Check if GLX is supported on this display if (!glXQueryExtension(_glfw.x11.display, &_glfw.glx.errorBase, @@ -228,6 +222,8 @@ void _glfwTerminateContextAPI(void) _glfw.glx.libGL = NULL; } #endif + + pthread_key_delete(_glfw.glx.current); } #define setGLXattrib(attribName, attribValue) \ @@ -529,12 +525,12 @@ void _glfwPlatformMakeContextCurrent(_GLFWwindow* window) else glXMakeCurrent(_glfw.x11.display, None, NULL); - _glfwCurrentWindow = window; + pthread_setspecific(_glfw.glx.current, window); } _GLFWwindow* _glfwPlatformGetCurrentContext(void) { - return _glfwCurrentWindow; + return (_GLFWwindow*) pthread_getspecific(_glfw.glx.current); } void _glfwPlatformSwapBuffers(_GLFWwindow* window) @@ -544,7 +540,7 @@ void _glfwPlatformSwapBuffers(_GLFWwindow* window) void _glfwPlatformSwapInterval(int interval) { - _GLFWwindow* window = _glfwCurrentWindow; + _GLFWwindow* window = _glfwPlatformGetCurrentContext(); if (_glfw.glx.EXT_swap_control) { diff --git a/src/glx_platform.h b/src/glx_platform.h index 0bb514de..77d4690d 100644 --- a/src/glx_platform.h +++ b/src/glx_platform.h @@ -93,6 +93,9 @@ typedef struct _GLFWlibraryGLX int eventBase; int errorBase; + // TLS key for per-thread current context/window + pthread_key_t current; + // GLX error code received by Xlib error callback int errorCode; diff --git a/src/nsgl_context.m b/src/nsgl_context.m index 1d5a0fcd..b6747e96 100644 --- a/src/nsgl_context.m +++ b/src/nsgl_context.m @@ -32,11 +32,6 @@ #include -// The per-thread current context/window pointer -// -static pthread_key_t _glfwCurrentTLS; - - ////////////////////////////////////////////////////////////////////////// ////// GLFW internal API ////// ////////////////////////////////////////////////////////////////////////// @@ -45,7 +40,7 @@ static pthread_key_t _glfwCurrentTLS; // int _glfwInitContextAPI(void) { - if (pthread_key_create(&_glfwCurrentTLS, NULL) != 0) + if (pthread_key_create(&_glfw.nsgl.current, NULL) != 0) { _glfwInputError(GLFW_PLATFORM_ERROR, "NSOpenGL: Failed to create context TLS"); @@ -59,7 +54,7 @@ int _glfwInitContextAPI(void) // void _glfwTerminateContextAPI(void) { - pthread_key_delete(_glfwCurrentTLS); + pthread_key_delete(_glfw.nsgl.current); } // Create the OpenGL context @@ -242,12 +237,12 @@ void _glfwPlatformMakeContextCurrent(_GLFWwindow* window) else [NSOpenGLContext clearCurrentContext]; - pthread_setspecific(_glfwCurrentTLS, window); + pthread_setspecific(_glfw.nsgl.current, window); } _GLFWwindow* _glfwPlatformGetCurrentContext(void) { - return (_GLFWwindow*) pthread_getspecific(_glfwCurrentTLS); + return (_GLFWwindow*) pthread_getspecific(_glfw.nsgl.current); } void _glfwPlatformSwapBuffers(_GLFWwindow* window) diff --git a/src/nsgl_platform.h b/src/nsgl_platform.h index 2ddf4b84..71041202 100644 --- a/src/nsgl_platform.h +++ b/src/nsgl_platform.h @@ -55,7 +55,11 @@ typedef struct _GLFWcontextNSGL typedef struct _GLFWlibraryNSGL { // dlopen handle for dynamically loading OpenGL extension entry points - void* framework; + void* framework; + + // TLS key for per-thread current context/window + pthread_key_t current; + } _GLFWlibraryNSGL; diff --git a/src/wgl_context.c b/src/wgl_context.c index ddaa6245..5f86dae2 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -133,8 +133,8 @@ static void initWGLExtensions(_GLFWwindow* window) // int _glfwInitContextAPI(void) { - _glfw.wgl.tls = TlsAlloc(); - if (_glfw.wgl.tls == TLS_OUT_OF_INDEXES) + _glfw.wgl.current = TlsAlloc(); + if (_glfw.wgl.current == TLS_OUT_OF_INDEXES) { _glfwInputError(GLFW_PLATFORM_ERROR, "WGL: Failed to allocate TLS index"); @@ -151,7 +151,7 @@ int _glfwInitContextAPI(void) void _glfwTerminateContextAPI(void) { if (_glfw.wgl.hasTLS) - TlsFree(_glfw.wgl.tls); + TlsFree(_glfw.wgl.current); } #define setWGLattrib(attribName, attribValue) \ @@ -512,12 +512,12 @@ void _glfwPlatformMakeContextCurrent(_GLFWwindow* window) else wglMakeCurrent(NULL, NULL); - TlsSetValue(_glfw.wgl.tls, window); + TlsSetValue(_glfw.wgl.current, window); } _GLFWwindow* _glfwPlatformGetCurrentContext(void) { - return TlsGetValue(_glfw.wgl.tls); + return TlsGetValue(_glfw.wgl.current); } void _glfwPlatformSwapBuffers(_GLFWwindow* window) diff --git a/src/wgl_platform.h b/src/wgl_platform.h index 71f757ad..b5d481fd 100644 --- a/src/wgl_platform.h +++ b/src/wgl_platform.h @@ -77,7 +77,7 @@ typedef struct _GLFWcontextWGL typedef struct _GLFWlibraryWGL { GLboolean hasTLS; - DWORD tls; + DWORD current; } _GLFWlibraryWGL;