Make opengl initialization thread-safe

svn path=/trunk/; revision=30429
This commit is contained in:
Thomas Bluemel 2007-11-13 23:30:17 +00:00
parent d3e6d6d283
commit 65636af627

View file

@ -46,46 +46,61 @@ static GETPIXELFMT glGetPixelFormat = NULL;
*/ */
HINSTANCE hOpenGL = NULL; HINSTANCE hOpenGL = NULL;
static BOOL OpenGLInitFunction(PCSTR name,
FARPROC *funcptr)
{
PVOID func;
func = (PVOID)GetProcAddress(hOpenGL, name);
if (func != NULL)
{
(void)InterlockedCompareExchangePointer((PVOID*)funcptr,
func,
NULL);
return TRUE;
}
return FALSE;
}
static BOOL OpenGLEnable(void) static BOOL OpenGLEnable(void)
{ {
if(hOpenGL == NULL) HMODULE hModOpengl32;
BOOL Ret = TRUE;
hModOpengl32 = LoadLibraryW(L"OPENGL32.DLL");
if (hModOpengl32 == NULL)
return FALSE;
if (InterlockedCompareExchangePointer((PVOID*)&hOpenGL,
(PVOID)hModOpengl32,
NULL) != NULL)
{ {
hOpenGL = LoadLibraryA("OPENGL32.DLL"); FreeLibrary(hModOpengl32);
if(hOpenGL == NULL)
return(FALSE); /* NOTE: Even though another thread was faster loading the
library we can't just bail out here. We really need
to *try* to locate every function. This is slow but
thread-safe */
} }
if(glChoosePixelFormat == NULL) {
glChoosePixelFormat = (CHOOSEPIXELFMT)GetProcAddress(hOpenGL, "wglChoosePixelFormat");
if(glChoosePixelFormat == NULL)
return(FALSE);
}
if(glSetPixelFormat == NULL) { if (!OpenGLInitFunction("wglChoosePixelFormat", &glChoosePixelFormat))
glSetPixelFormat = (SETPIXELFMT)GetProcAddress(hOpenGL, "wglSetPixelFormat"); Ret = FALSE;
if(glSetPixelFormat == NULL)
return(FALSE);
}
if(glSwapBuffers == NULL) { if (!OpenGLInitFunction("wglSetPixelFormat", &glSetPixelFormat))
glSwapBuffers = (SWAPBUFFERS)GetProcAddress(hOpenGL, "wglSwapBuffers"); Ret = FALSE;
if(glSwapBuffers == NULL)
return(FALSE);
}
if(glDescribePixelFormat == NULL) { if (!OpenGLInitFunction("wglSwapBuffers", &glSwapBuffers))
glDescribePixelFormat = (DESCRIBEPIXELFMT)GetProcAddress(hOpenGL, "wglDescribePixelFormat"); Ret = FALSE;
if(glDescribePixelFormat == NULL)
return(FALSE);
}
if(glGetPixelFormat == NULL) { if (!OpenGLInitFunction("wglDescribePixelFormat", &glDescribePixelFormat))
glGetPixelFormat = (GETPIXELFMT)GetProcAddress(hOpenGL, "wglGetPixelFormat"); Ret = FALSE;
if(glGetPixelFormat == NULL)
return(FALSE);
}
return(TRUE); /* OpenGL is initialized and enabled*/ if (!OpenGLInitFunction("wglGetPixelFormat", &glGetPixelFormat))
Ret = FALSE;
return Ret;
} }