TLS key cleanup.

This commit is contained in:
Camilla Berglund 2013-05-02 16:48:11 +02:00
parent 6b7f5671f8
commit 4a2a00766c
6 changed files with 31 additions and 33 deletions

View File

@ -33,6 +33,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <pthread.h>
// This is the only glXGetProcAddress variant not declared by glxext.h // This is the only glXGetProcAddress variant not declared by glxext.h
@ -44,20 +45,6 @@ void (*glXGetProcAddressEXT(const GLubyte* procName))();
#endif #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 // Error handler used when creating a context
// //
static int errorHandler(Display *display, XErrorEvent* event) static int errorHandler(Display *display, XErrorEvent* event)
@ -124,6 +111,13 @@ int _glfwInitContextAPI(void)
} }
#endif #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 // Check if GLX is supported on this display
if (!glXQueryExtension(_glfw.x11.display, if (!glXQueryExtension(_glfw.x11.display,
&_glfw.glx.errorBase, &_glfw.glx.errorBase,
@ -228,6 +222,8 @@ void _glfwTerminateContextAPI(void)
_glfw.glx.libGL = NULL; _glfw.glx.libGL = NULL;
} }
#endif #endif
pthread_key_delete(_glfw.glx.current);
} }
#define setGLXattrib(attribName, attribValue) \ #define setGLXattrib(attribName, attribValue) \
@ -529,12 +525,12 @@ void _glfwPlatformMakeContextCurrent(_GLFWwindow* window)
else else
glXMakeCurrent(_glfw.x11.display, None, NULL); glXMakeCurrent(_glfw.x11.display, None, NULL);
_glfwCurrentWindow = window; pthread_setspecific(_glfw.glx.current, window);
} }
_GLFWwindow* _glfwPlatformGetCurrentContext(void) _GLFWwindow* _glfwPlatformGetCurrentContext(void)
{ {
return _glfwCurrentWindow; return (_GLFWwindow*) pthread_getspecific(_glfw.glx.current);
} }
void _glfwPlatformSwapBuffers(_GLFWwindow* window) void _glfwPlatformSwapBuffers(_GLFWwindow* window)
@ -544,7 +540,7 @@ void _glfwPlatformSwapBuffers(_GLFWwindow* window)
void _glfwPlatformSwapInterval(int interval) void _glfwPlatformSwapInterval(int interval)
{ {
_GLFWwindow* window = _glfwCurrentWindow; _GLFWwindow* window = _glfwPlatformGetCurrentContext();
if (_glfw.glx.EXT_swap_control) if (_glfw.glx.EXT_swap_control)
{ {

View File

@ -93,6 +93,9 @@ typedef struct _GLFWlibraryGLX
int eventBase; int eventBase;
int errorBase; int errorBase;
// TLS key for per-thread current context/window
pthread_key_t current;
// GLX error code received by Xlib error callback // GLX error code received by Xlib error callback
int errorCode; int errorCode;

View File

@ -32,11 +32,6 @@
#include <pthread.h> #include <pthread.h>
// The per-thread current context/window pointer
//
static pthread_key_t _glfwCurrentTLS;
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////// GLFW internal API ////// ////// GLFW internal API //////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
@ -45,7 +40,7 @@ static pthread_key_t _glfwCurrentTLS;
// //
int _glfwInitContextAPI(void) int _glfwInitContextAPI(void)
{ {
if (pthread_key_create(&_glfwCurrentTLS, NULL) != 0) if (pthread_key_create(&_glfw.nsgl.current, NULL) != 0)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"NSOpenGL: Failed to create context TLS"); "NSOpenGL: Failed to create context TLS");
@ -59,7 +54,7 @@ int _glfwInitContextAPI(void)
// //
void _glfwTerminateContextAPI(void) void _glfwTerminateContextAPI(void)
{ {
pthread_key_delete(_glfwCurrentTLS); pthread_key_delete(_glfw.nsgl.current);
} }
// Create the OpenGL context // Create the OpenGL context
@ -242,12 +237,12 @@ void _glfwPlatformMakeContextCurrent(_GLFWwindow* window)
else else
[NSOpenGLContext clearCurrentContext]; [NSOpenGLContext clearCurrentContext];
pthread_setspecific(_glfwCurrentTLS, window); pthread_setspecific(_glfw.nsgl.current, window);
} }
_GLFWwindow* _glfwPlatformGetCurrentContext(void) _GLFWwindow* _glfwPlatformGetCurrentContext(void)
{ {
return (_GLFWwindow*) pthread_getspecific(_glfwCurrentTLS); return (_GLFWwindow*) pthread_getspecific(_glfw.nsgl.current);
} }
void _glfwPlatformSwapBuffers(_GLFWwindow* window) void _glfwPlatformSwapBuffers(_GLFWwindow* window)

View File

@ -55,7 +55,11 @@ typedef struct _GLFWcontextNSGL
typedef struct _GLFWlibraryNSGL typedef struct _GLFWlibraryNSGL
{ {
// dlopen handle for dynamically loading OpenGL extension entry points // 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; } _GLFWlibraryNSGL;

View File

@ -133,8 +133,8 @@ static void initWGLExtensions(_GLFWwindow* window)
// //
int _glfwInitContextAPI(void) int _glfwInitContextAPI(void)
{ {
_glfw.wgl.tls = TlsAlloc(); _glfw.wgl.current = TlsAlloc();
if (_glfw.wgl.tls == TLS_OUT_OF_INDEXES) if (_glfw.wgl.current == TLS_OUT_OF_INDEXES)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"WGL: Failed to allocate TLS index"); "WGL: Failed to allocate TLS index");
@ -151,7 +151,7 @@ int _glfwInitContextAPI(void)
void _glfwTerminateContextAPI(void) void _glfwTerminateContextAPI(void)
{ {
if (_glfw.wgl.hasTLS) if (_glfw.wgl.hasTLS)
TlsFree(_glfw.wgl.tls); TlsFree(_glfw.wgl.current);
} }
#define setWGLattrib(attribName, attribValue) \ #define setWGLattrib(attribName, attribValue) \
@ -512,12 +512,12 @@ void _glfwPlatformMakeContextCurrent(_GLFWwindow* window)
else else
wglMakeCurrent(NULL, NULL); wglMakeCurrent(NULL, NULL);
TlsSetValue(_glfw.wgl.tls, window); TlsSetValue(_glfw.wgl.current, window);
} }
_GLFWwindow* _glfwPlatformGetCurrentContext(void) _GLFWwindow* _glfwPlatformGetCurrentContext(void)
{ {
return TlsGetValue(_glfw.wgl.tls); return TlsGetValue(_glfw.wgl.current);
} }
void _glfwPlatformSwapBuffers(_GLFWwindow* window) void _glfwPlatformSwapBuffers(_GLFWwindow* window)

View File

@ -77,7 +77,7 @@ typedef struct _GLFWcontextWGL
typedef struct _GLFWlibraryWGL typedef struct _GLFWlibraryWGL
{ {
GLboolean hasTLS; GLboolean hasTLS;
DWORD tls; DWORD current;
} _GLFWlibraryWGL; } _GLFWlibraryWGL;