Implemented TLS for all platforms.
diff --git a/src/cocoa_init.m b/src/cocoa_init.m index 7c208b9..3bb61c1 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m
@@ -103,6 +103,9 @@ _glfwInitJoysticks(); + if (!_glfwInitOpenGL()) + return GL_FALSE; + _glfwLibrary.NS.eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); if (!_glfwLibrary.NS.eventSource) return GL_FALSE; @@ -143,6 +146,8 @@ _glfwTerminateJoysticks(); + _glfwTerminateOpenGL(); + return GL_TRUE; }
diff --git a/src/cocoa_opengl.m b/src/cocoa_opengl.m index 1f1f0f2..4d4c429 100644 --- a/src/cocoa_opengl.m +++ b/src/cocoa_opengl.m
@@ -29,12 +29,13 @@ #include "internal.h" +#include <pthread.h> + //======================================================================== // The per-thread current context/window pointer -// TODO: Implement pthreads TLS //======================================================================== -_GLFWwindow* _glfwCurrentWindow = NULL; +static pthread_key_t _glfwCurrentTLS; ////////////////////////////////////////////////////////////////////////// @@ -42,6 +43,33 @@ ////////////////////////////////////////////////////////////////////////// //======================================================================== +// Initialize OpenGL support +//======================================================================== + +int _glfwInitOpenGL(void) +{ + if (pthread_key_create(&_glfwCurrentTLS, NULL) != 0) + { + _glfwSetError(GLFW_PLATFORM_ERROR, + "Cocoa/NSGL: Failed to create context TLS"); + return GL_FALSE; + } + + return GL_TRUE; +} + + +//======================================================================== +// Terminate OpenGL support +//======================================================================== + +void _glfwTerminateOpenGL(void) +{ + pthread_key_delete(_glfwCurrentTLS); +} + + +//======================================================================== // Make the OpenGL context associated with the specified window current //======================================================================== @@ -52,7 +80,7 @@ else [NSOpenGLContext clearCurrentContext]; - _glfwCurrentWindow = window; + pthread_setspecific(_glfwCurrentTLS, window); } @@ -62,7 +90,7 @@ _GLFWwindow* _glfwPlatformGetCurrentContext(void) { - return _glfwCurrentWindow; + return (_GLFWwindow*) pthread_getspecific(_glfwCurrentTLS); } @@ -83,7 +111,7 @@ void _glfwPlatformSwapInterval(int interval) { - _GLFWwindow* window = _glfwCurrentWindow; + _GLFWwindow* window = _glfwPlatformGetCurrentContext(); GLint sync = interval; [window->NSGL.context setValues:&sync forParameter:NSOpenGLCPSwapInterval];
diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index 97e903d..a202c25 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h
@@ -124,4 +124,8 @@ GLboolean _glfwSetVideoMode(int* width, int* height, int* bpp, int* refreshRate); void _glfwRestoreVideoMode(void); +// OpenGL support +int _glfwInitOpenGL(void); +void _glfwTerminateOpenGL(void); + #endif // _platform_h_
diff --git a/src/win32_opengl.c b/src/win32_opengl.c index fb0ba47..6801e14 100644 --- a/src/win32_opengl.c +++ b/src/win32_opengl.c
@@ -34,9 +34,21 @@ //======================================================================== +// 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 //======================================================================== -__declspec(thread) _GLFWwindow* _glfwCurrentWindow = NULL; +static _GLFW_TLS _GLFWwindow* _glfwCurrentWindow = NULL; //========================================================================
diff --git a/src/x11_opengl.c b/src/x11_opengl.c index d7f3399..38a43ff 100644 --- a/src/x11_opengl.c +++ b/src/x11_opengl.c
@@ -37,10 +37,21 @@ // This is the only glXGetProcAddress variant not declared by glxext.h void (*glXGetProcAddressEXT(const GLubyte* procName))(); + +//======================================================================== +// Thread local storage attribute macro +//======================================================================== +#if defined(__GNUC__) + #define _GLFW_TLS __thread +#else + #define _GLFW_TLS +#endif + + //======================================================================== // The per-thread current context/window pointer //======================================================================== -__thread _GLFWwindow* _glfwCurrentWindow = NULL; +static _GLFW_TLS _GLFWwindow* _glfwCurrentWindow = NULL; //========================================================================