wgl: Move all thread related code together.
authorJosé Fonseca <jfonseca@vmware.com>
Wed, 17 Jun 2009 18:24:51 +0000 (19:24 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Thu, 18 Jun 2009 13:54:07 +0000 (14:54 +0100)
Not only for cosmetic reasons, but also because we need to set the
SetWindowsHookEx hook for threads created before the DllMain is called
(threads for each we don't get the DLL_THREAD_ATTACH notification).

src/gallium/state_trackers/wgl/shared/stw_device.c
src/gallium/state_trackers/wgl/shared/stw_framebuffer.c
src/gallium/state_trackers/wgl/shared/stw_framebuffer.h
src/gallium/state_trackers/wgl/shared/stw_tls.c
src/gallium/state_trackers/wgl/shared/stw_tls.h

index 1a6b298..070ffcb 100644 (file)
@@ -133,20 +133,13 @@ error1:
 boolean
 stw_init_thread(void)
 {
-   if (!stw_tls_init_thread())
-      return FALSE;
-
-   if (!stw_framebuffer_init_thread())
-      return FALSE;
-
-   return TRUE;
+   return stw_tls_init_thread();
 }
 
 
 void
 stw_cleanup_thread(void)
 {
-   stw_framebuffer_cleanup_thread();
    stw_tls_cleanup_thread();
 }
 
index 58f1830..8804385 100644 (file)
@@ -84,7 +84,7 @@ stw_framebuffer_destroy_locked(
  * @sa http://msdn.microsoft.com/en-us/library/ms644975(VS.85).aspx
  * @sa http://msdn.microsoft.com/en-us/library/ms644960(VS.85).aspx
  */
-static LRESULT CALLBACK
+LRESULT CALLBACK
 stw_call_window_proc(
    int nCode,
    WPARAM wParam,
@@ -423,38 +423,3 @@ stw_swap_layer_buffers(
 
    return FALSE;
 }
-
-
-boolean
-stw_framebuffer_init_thread(void)
-{
-   struct stw_tls_data *tls_data;
-   
-   tls_data = stw_tls_get_data();
-   if(!tls_data)
-      return FALSE;
-   
-   tls_data->hCallWndProcHook = SetWindowsHookEx(WH_CALLWNDPROC,
-                                                 stw_call_window_proc,
-                                                 NULL,
-                                                 GetCurrentThreadId());
-   if(tls_data->hCallWndProcHook == NULL)
-      return FALSE;
-   
-   return TRUE;
-}
-
-void
-stw_framebuffer_cleanup_thread(void)
-{
-   struct stw_tls_data *tls_data;
-   
-   tls_data = stw_tls_get_data();
-   if(!tls_data)
-      return;
-   
-   if(tls_data->hCallWndProcHook) {
-      UnhookWindowsHookEx(tls_data->hCallWndProcHook);
-      tls_data->hCallWndProcHook = NULL;
-   }
-}
index e7fa51c..d6f5950 100644 (file)
@@ -79,10 +79,4 @@ struct stw_framebuffer *
 stw_framebuffer_from_hdc(
    HDC hdc );
 
-boolean
-stw_framebuffer_init_thread(void);
-
-void
-stw_framebuffer_cleanup_thread(void);
-
 #endif /* STW_FRAMEBUFFER_H */
index 0c18a52..4bd6a92 100644 (file)
@@ -51,9 +51,23 @@ stw_tls_data_create()
 
    data = CALLOC_STRUCT(stw_tls_data);
    if (!data)
-      return NULL;
+      goto no_data;
+
+   data->hCallWndProcHook = SetWindowsHookEx(WH_CALLWNDPROC,
+                                             stw_call_window_proc,
+                                             NULL,
+                                             GetCurrentThreadId());
+   if(data->hCallWndProcHook == NULL)
+      goto no_hook;
+
+   TlsSetValue(tlsIndex, data);
 
    return data;
+
+no_hook:
+   FREE(data);
+no_data:
+   return NULL;
 }
 
 boolean
@@ -69,8 +83,6 @@ stw_tls_init_thread(void)
    if(!data)
       return FALSE;
 
-   TlsSetValue(tlsIndex, data);
-
    return TRUE;
 }
 
@@ -84,8 +96,16 @@ stw_tls_cleanup_thread(void)
    }
 
    data = (struct stw_tls_data *) TlsGetValue(tlsIndex);
-   TlsSetValue(tlsIndex, NULL);
-   FREE(data);
+   if(data) {
+      TlsSetValue(tlsIndex, NULL);
+   
+      if(data->hCallWndProcHook) {
+         UnhookWindowsHookEx(data->hCallWndProcHook);
+         data->hCallWndProcHook = NULL;
+      }
+   
+      FREE(data);
+   }
 }
 
 void
@@ -110,12 +130,9 @@ stw_tls_get_data(void)
    if(!data) {
       /* DllMain is called with DLL_THREAD_ATTACH only by threads created after 
        * the DLL is loaded by the process */
-      
       data = stw_tls_data_create();
       if(!data)
          return NULL;
-
-      TlsSetValue(tlsIndex, data);
    }
 
    return data;
index 6af8be7..fbf8b1c 100644 (file)
@@ -50,4 +50,10 @@ stw_tls_cleanup(void);
 struct stw_tls_data *
 stw_tls_get_data(void);
 
+LRESULT CALLBACK
+stw_call_window_proc(
+   int nCode,
+   WPARAM wParam,
+   LPARAM lParam );
+
 #endif /* STW_TLS_H */