Implemented TLS for all platforms.

This commit is contained in:
Camilla Berglund 2012-08-13 16:03:44 +02:00
parent c594bb4689
commit 18a5aba8f1
5 changed files with 67 additions and 7 deletions

View File

@ -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;
}

View File

@ -29,18 +29,46 @@
#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;
//////////////////////////////////////////////////////////////////////////
////// 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];

View File

@ -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_

View File

@ -33,10 +33,22 @@
#include <stdlib.h>
//========================================================================
// 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;
//========================================================================

View File

@ -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;
//========================================================================