Commit 2a6f4315 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

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

parent f3d4fe1e
...@@ -420,6 +420,26 @@ void *vlc_threadvar_get (vlc_threadvar_t key) ...@@ -420,6 +420,26 @@ void *vlc_threadvar_get (vlc_threadvar_t key)
return value; 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 ***/ /*** Threads ***/
static DWORD thread_key; static DWORD thread_key;
...@@ -451,30 +471,6 @@ static bool isCancelled(void) ...@@ -451,30 +471,6 @@ static bool isCancelled(void)
} }
#endif #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) static unsigned __stdcall vlc_entry (void *p)
{ {
struct vlc_thread *th = p; struct vlc_thread *th = p;
...@@ -482,7 +478,10 @@ static unsigned __stdcall vlc_entry (void *p) ...@@ -482,7 +478,10 @@ static unsigned __stdcall vlc_entry (void *p)
TlsSetValue(thread_key, th); TlsSetValue(thread_key, th);
th->killable = true; th->killable = true;
th->data = th->entry (th->data); th->data = th->entry (th->data);
vlc_thread_cleanup (th); TlsSetValue(thread_key, NULL);
if (th->id == NULL) /* Detached thread */
free(th);
return 0; return 0;
} }
...@@ -630,7 +629,9 @@ void vlc_testcancel (void) ...@@ -630,7 +629,9 @@ void vlc_testcancel (void)
p->proc (p->data); p->proc (p->data);
th->data = NULL; /* TODO: special value? */ th->data = NULL; /* TODO: special value? */
vlc_thread_cleanup (th); TlsSetValue(thread_key, NULL);
if (th->id == NULL) /* Detached thread */
free(th);
_endthreadex(0); _endthreadex(0);
} }
...@@ -1039,6 +1040,10 @@ BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved) ...@@ -1039,6 +1040,10 @@ BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
DeleteCriticalSection (&clock_lock); DeleteCriticalSection (&clock_lock);
TlsFree(thread_key); TlsFree(thread_key);
break; break;
case DLL_THREAD_DETACH:
vlc_threadvars_cleanup();
break;
} }
return TRUE; return TRUE;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment