[ros-diffs] [weiden] 30429: Make opengl initialization thread-safe

weiden at svn.reactos.org weiden at svn.reactos.org
Wed Nov 14 00:30:18 CET 2007


Author: weiden
Date: Wed Nov 14 02:30:17 2007
New Revision: 30429

URL: http://svn.reactos.org/svn/reactos?rev=30429&view=rev
Log:
Make opengl initialization thread-safe

Modified:
    trunk/reactos/dll/win32/gdi32/misc/wingl.c

Modified: trunk/reactos/dll/win32/gdi32/misc/wingl.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/gdi32/misc/wingl.c?rev=30429&r1=30428&r2=30429&view=diff
==============================================================================
--- trunk/reactos/dll/win32/gdi32/misc/wingl.c (original)
+++ trunk/reactos/dll/win32/gdi32/misc/wingl.c Wed Nov 14 02:30:17 2007
@@ -46,46 +46,61 @@
 */
 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)
 {
-  if(hOpenGL == NULL)
-  {
-     hOpenGL = LoadLibraryA("OPENGL32.DLL");
-     if(hOpenGL == NULL)
-       return(FALSE);
-  }
-
-  if(glChoosePixelFormat == NULL) {
-        glChoosePixelFormat = (CHOOSEPIXELFMT)GetProcAddress(hOpenGL, "wglChoosePixelFormat");
-        if(glChoosePixelFormat == NULL)
-                return(FALSE);
-  }
-
-  if(glSetPixelFormat == NULL) {
-        glSetPixelFormat = (SETPIXELFMT)GetProcAddress(hOpenGL, "wglSetPixelFormat");
-        if(glSetPixelFormat == NULL)
-                return(FALSE);
-  }
-
-  if(glSwapBuffers == NULL) {
-        glSwapBuffers = (SWAPBUFFERS)GetProcAddress(hOpenGL, "wglSwapBuffers");
-        if(glSwapBuffers == NULL)
-                return(FALSE);
-  }
-
-  if(glDescribePixelFormat == NULL) {
-        glDescribePixelFormat = (DESCRIBEPIXELFMT)GetProcAddress(hOpenGL, "wglDescribePixelFormat");
-        if(glDescribePixelFormat == NULL)
-                return(FALSE);
-  }
-
-  if(glGetPixelFormat == NULL) {
-        glGetPixelFormat = (GETPIXELFMT)GetProcAddress(hOpenGL, "wglGetPixelFormat");
-        if(glGetPixelFormat == NULL)
-                return(FALSE);
-  }
-
-  return(TRUE);	/* OpenGL is initialized and enabled*/
+    HMODULE hModOpengl32;
+    BOOL Ret = TRUE;
+
+    hModOpengl32 = LoadLibraryW(L"OPENGL32.DLL");
+    if (hModOpengl32 == NULL)
+        return FALSE;
+
+    if (InterlockedCompareExchangePointer((PVOID*)&hOpenGL,
+                                          (PVOID)hModOpengl32,
+                                          NULL) != NULL)
+    {
+        FreeLibrary(hModOpengl32);
+
+        /* 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 (!OpenGLInitFunction("wglChoosePixelFormat", &glChoosePixelFormat))
+        Ret = FALSE;
+
+    if (!OpenGLInitFunction("wglSetPixelFormat", &glSetPixelFormat))
+        Ret = FALSE;
+
+    if (!OpenGLInitFunction("wglSwapBuffers", &glSwapBuffers))
+        Ret = FALSE;
+
+    if (!OpenGLInitFunction("wglDescribePixelFormat", &glDescribePixelFormat))
+        Ret = FALSE;
+
+    if (!OpenGLInitFunction("wglGetPixelFormat", &glGetPixelFormat))
+        Ret = FALSE;
+
+    return Ret;
 }
 
 




More information about the Ros-diffs mailing list