Merge remote branch 'origin/master' into gallium_draw_llvm
[mesa.git] / src / gallium / state_trackers / wgl / stw_device.c
index 7785aba46775b9c910503dc4ac8dd64f01dd84d3..ea300f27cb56ed912dbba39a2b01a64e34b4ec96 100644 (file)
 #include "pipe/p_screen.h"
 #include "state_tracker/st_public.h"
 
-#ifdef DEBUG
-#include "trace/tr_screen.h"
-#include "trace/tr_texture.h"
-#endif
-
 #include "stw_device.h"
 #include "stw_winsys.h"
 #include "stw_pixelformat.h"
@@ -47,7 +42,6 @@
 
 #ifdef WIN32_THREADS
 extern _glthread_Mutex OneTimeLock;
-extern void FreeAllTSD(void);
 #endif
 
 
@@ -108,13 +102,10 @@ stw_init(const struct stw_winsys *stw_winsys)
    if(stw_winsys->get_adapter_luid)
       stw_winsys->get_adapter_luid(screen, &stw_dev->AdapterLuid);
 
-#ifdef DEBUG
-   stw_dev->screen = trace_screen_create(screen);
-   stw_dev->trace_running = stw_dev->screen != screen ? TRUE : FALSE;
-#else
    stw_dev->screen = screen;
-#endif
-   
+
+   /* XXX
+    */
    stw_dev->screen->flush_frontbuffer = &stw_flush_frontbuffer;
    
    pipe_mutex_init( stw_dev->ctx_mutex );
@@ -152,24 +143,27 @@ stw_cleanup_thread(void)
 void
 stw_cleanup(void)
 {
-   unsigned i;
+   DHGLRC dhglrc;
 
    debug_printf("%s\n", __FUNCTION__);
 
    if (!stw_dev)
       return;
    
+   /*
+    * Abort cleanup if there are still active contexts. In some situations
+    * this DLL may be unloaded before the DLL that is using GL contexts is.
+    */
    pipe_mutex_lock( stw_dev->ctx_mutex );
-   {
-      /* Ensure all contexts are destroyed */
-      i = handle_table_get_first_handle(stw_dev->ctx_table);
-      while (i) {
-         DrvDeleteContext(i);
-         i = handle_table_get_next_handle(stw_dev->ctx_table, i);
-      }
-      handle_table_destroy(stw_dev->ctx_table);
-   }
+   dhglrc = handle_table_get_first_handle(stw_dev->ctx_table);
    pipe_mutex_unlock( stw_dev->ctx_mutex );
+   if (dhglrc) {
+      debug_printf("%s: contexts still active -- cleanup aborted\n", __FUNCTION__);
+      stw_dev = NULL;
+      return;
+   }
+
+   handle_table_destroy(stw_dev->ctx_table);
 
    stw_framebuffer_cleanup();
    
@@ -180,7 +174,8 @@ stw_cleanup(void)
 
 #ifdef WIN32_THREADS
    _glthread_DESTROY_MUTEX(OneTimeLock);
-   FreeAllTSD();
+
+   _glapi_destroy_multithread();
 #endif
 
 #ifdef DEBUG