diff --git a/src/cocoa_init.m b/src/cocoa_init.m index 7c208b90..3bb61c18 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -103,6 +103,9 @@ int _glfwPlatformInit(void) _glfwInitJoysticks(); + if (!_glfwInitOpenGL()) + return GL_FALSE; + _glfwLibrary.NS.eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); if (!_glfwLibrary.NS.eventSource) return GL_FALSE; @@ -143,6 +146,8 @@ int _glfwPlatformTerminate(void) _glfwTerminateJoysticks(); + _glfwTerminateOpenGL(); + return GL_TRUE; } diff --git a/src/cocoa_opengl.m b/src/cocoa_opengl.m index 1f1f0f23..4d4c4299 100644 --- a/src/cocoa_opengl.m +++ b/src/cocoa_opengl.m @@ -29,18 +29,46 @@ #include "internal.h" +#include + //======================================================================== // The per-thread current context/window pointer -// TODO: Implement pthreads TLS //======================================================================== -_GLFWwindow* _glfwCurrentWindow = NULL; +static pthread_key_t _glfwCurrentTLS; ////////////////////////////////////////////////////////////////////////// ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// +//======================================================================== +// 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 @@ void _glfwPlatformMakeContextCurrent(_GLFWwindow* window) else [NSOpenGLContext clearCurrentContext]; - _glfwCurrentWindow = window; + pthread_setspecific(_glfwCurrentTLS, window); } @@ -62,7 +90,7 @@ void _glfwPlatformMakeContextCurrent(_GLFWwindow* window) _GLFWwindow* _glfwPlatformGetCurrentContext(void) { - return _glfwCurrentWindow; + return (_GLFWwindow*) pthread_getspecific(_glfwCurrentTLS); } @@ -83,7 +111,7 @@ void _glfwPlatformSwapBuffers(_GLFWwindow* window) 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 97e903d7..a202c25e 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -124,4 +124,8 @@ void _glfwTerminateJoysticks(void); 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 fb0ba47f..6801e144 100644 --- a/src/win32_opengl.c +++ b/src/win32_opengl.c @@ -33,10 +33,22 @@ #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 //======================================================================== -__declspec(thread) _GLFWwindow* _glfwCurrentWindow = NULL; +static _GLFW_TLS _GLFWwindow* _glfwCurrentWindow = NULL; //======================================================================== diff --git a/src/x11_opengl.c b/src/x11_opengl.c index d7f33999..38a43ff0 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; //========================================================================