[vlc-commits] win32: clean thread-local storage also on non-LibVLC threads

Rémi Denis-Courmont git at videolan.org
Mon Jun 29 22:05:45 CEST 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Jun 28 18:52:25 2015 +0300| [2a6f431522040bebb751f0eca05a085235f5f738] | committer: Rémi Denis-Courmont

win32: clean thread-local storage also on non-LibVLC threads

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=2a6f431522040bebb751f0eca05a085235f5f738
---

 src/win32/thread.c |   57 ++++++++++++++++++++++++++++------------------------
 1 file changed, 31 insertions(+), 26 deletions(-)

diff --git a/src/win32/thread.c b/src/win32/thread.c
index 903915e..c61a7b8 100644
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -420,6 +420,26 @@ void *vlc_threadvar_get (vlc_threadvar_t key)
     return value;
 }
 
+static void vlc_threadvars_cleanup(void)
+{
+    vlc_threadvar_t key;
+retry:
+    /* TODO: use RW lock or something similar */
+    vlc_mutex_lock(&super_mutex);
+    for (key = vlc_threadvar_last; key != NULL; key = key->prev)
+    {
+        void *value = vlc_threadvar_get(key);
+        if (value != NULL && key->destroy != NULL)
+        {
+            vlc_mutex_unlock(&super_mutex);
+            vlc_threadvar_set(key, NULL);
+            key->destroy(value);
+            goto retry;
+        }
+    }
+    vlc_mutex_unlock(&super_mutex);
+}
+
 /*** Threads ***/
 static DWORD thread_key;
 
@@ -451,30 +471,6 @@ static bool isCancelled(void)
 }
 #endif
 
-static void vlc_thread_cleanup (struct vlc_thread *th)
-{
-    vlc_threadvar_t key;
-
-retry:
-    /* TODO: use RW lock or something similar */
-    vlc_mutex_lock (&super_mutex);
-    for (key = vlc_threadvar_last; key != NULL; key = key->prev)
-    {
-        void *value = vlc_threadvar_get (key);
-        if (value != NULL && key->destroy != NULL)
-        {
-            vlc_mutex_unlock (&super_mutex);
-            vlc_threadvar_set (key, NULL);
-            key->destroy (value);
-            goto retry;
-        }
-    }
-    vlc_mutex_unlock (&super_mutex);
-
-    if (th->id == NULL) /* Detached thread */
-        free (th);
-}
-
 static unsigned __stdcall vlc_entry (void *p)
 {
     struct vlc_thread *th = p;
@@ -482,7 +478,10 @@ static unsigned __stdcall vlc_entry (void *p)
     TlsSetValue(thread_key, th);
     th->killable = true;
     th->data = th->entry (th->data);
-    vlc_thread_cleanup (th);
+    TlsSetValue(thread_key, NULL);
+
+    if (th->id == NULL) /* Detached thread */
+        free(th);
     return 0;
 }
 
@@ -630,7 +629,9 @@ void vlc_testcancel (void)
         p->proc (p->data);
 
     th->data = NULL; /* TODO: special value? */
-    vlc_thread_cleanup (th);
+    TlsSetValue(thread_key, NULL);
+    if (th->id == NULL) /* Detached thread */
+        free(th);
     _endthreadex(0);
 }
 
@@ -1039,6 +1040,10 @@ BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
             DeleteCriticalSection (&clock_lock);
             TlsFree(thread_key);
             break;
+
+        case DLL_THREAD_DETACH:
+            vlc_threadvars_cleanup();
+            break;
     }
     return TRUE;
 }



More information about the vlc-commits mailing list