Commit c5b09080 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont
Browse files

Revert "Win32: use the destructor function for TLS (fixes #2398)"

This reverts commit 3cda36f4.
parent 7156d7e3
......@@ -128,7 +128,7 @@ typedef struct
#define VLC_STATIC_MUTEX { 0, }
typedef HANDLE vlc_cond_t;
typedef struct vlc_threadvar *vlc_threadvar_t;
typedef DWORD vlc_threadvar_t;
#endif
......
......@@ -244,8 +244,7 @@ DWORD WaitForMultipleObjectsEx (DWORD nCount, const HANDLE *lpHandles,
#endif
#ifdef WIN32
static vlc_mutex_t super_mutex, tls_mutex;
static struct vlc_threadvar *tls_list = NULL;
static vlc_mutex_t super_mutex;
BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
{
......@@ -256,14 +255,11 @@ BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
{
case DLL_PROCESS_ATTACH:
vlc_mutex_init (&super_mutex);
vlc_mutex_init (&tls_mutex);
vlc_threadvar_create (&cancel_key, free);
break;
case DLL_PROCESS_DETACH:
vlc_threadvar_delete( &cancel_key );
assert (tls_list == NULL);
vlc_mutex_destroy (&tls_mutex);
vlc_mutex_destroy (&super_mutex);
break;
}
......@@ -663,83 +659,31 @@ int vlc_cond_timedwait (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
#endif
}
#if defined (LIBVLC_USE_PTHREAD)
#elif defined (WIN32)
struct vlc_threadvar
{
DWORD handle;
void (*cleanup) (void *);
struct vlc_threadvar *prev, *next;
};
static void vlc_threadvar_cleanup (void)
{
struct vlc_threadvar *p;
vlc_mutex_lock (&tls_mutex);
for (p = tls_list; p != NULL; p = p->next)
{
void *value = TlsGetValue (p->handle);
if (value)
p->cleanup (value);
}
vlc_mutex_unlock (&tls_mutex);
}
#endif
/*****************************************************************************
* vlc_tls_create: create a thread-local variable
*****************************************************************************/
int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) )
{
#if defined( LIBVLC_USE_PTHREAD )
return pthread_key_create( p_tls, destr );
int i_ret;
#if defined( LIBVLC_USE_PTHREAD )
i_ret = pthread_key_create( p_tls, destr );
#elif defined( WIN32 )
struct vlc_threadvar *tls = malloc (sizeof (*tls));
if (tls == NULL)
return ENOMEM;
tls->handle = TlsAlloc();
if (tls->handle == TLS_OUT_OF_INDEXES)
{
free (tls);
return EAGAIN;
}
tls->cleanup = destr;
tls->prev = NULL;
vlc_mutex_lock (&tls_mutex);
tls->next = tls_list;
if (tls_list)
tls_list->prev = tls;
tls_list = tls;
vlc_mutex_unlock (&tls_mutex);
*p_tls = tls;
return 0;
/* FIXME: remember/use the destr() callback and stop leaking whatever */
*p_tls = TlsAlloc();
i_ret = (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0;
#else
# error Unimplemented!
#endif
return i_ret;
}
void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
{
#if defined( LIBVLC_USE_PTHREAD )
pthread_key_delete (*p_tls);
#elif defined( WIN32 )
struct vlc_threadvar *tls = *p_tls;
TlsFree (tls->handle);
vlc_mutex_lock (&tls_mutex);
if (tls->prev)
tls->prev->next = tls->next;
if (tls->next)
tls->next->prev = tls->prev;
vlc_mutex_unlock (&tls_mutex);
free (tls);
TlsFree (*p_tls);
#else
# error Unimplemented!
#endif
......@@ -756,7 +700,7 @@ int vlc_threadvar_set (vlc_threadvar_t key, void *value)
#if defined(LIBVLC_USE_PTHREAD)
return pthread_setspecific (key, value);
#elif defined( UNDER_CE ) || defined( WIN32 )
return TlsSetValue (key->handle, value) ? ENOMEM : 0;
return TlsSetValue (key, p_value) ? ENOMEM : 0;
#else
# error Unimplemented!
#endif
......@@ -773,7 +717,7 @@ void *vlc_threadvar_get (vlc_threadvar_t key)
#if defined(LIBVLC_USE_PTHREAD)
return pthread_getspecific (key);
#elif defined( UNDER_CE ) || defined( WIN32 )
return TlsGetValue (key->handle);
return TlsGetValue (key);
#else
# error Unimplemented!
#endif
......@@ -791,7 +735,6 @@ static unsigned __stdcall vlc_entry (void *data)
vlc_threadvar_set (cancel_key, &cancel_data);
self->data = self->entry (self->data);
vlc_threadvar_cleanup ();
return 0;
}
#endif
......@@ -1080,10 +1023,8 @@ void vlc_testcancel (void)
# if defined (LIBVLC_USE_PTHREAD)
pthread_exit (PTHREAD_CANCELLED);
# elif defined (UNDER_CE)
vlc_threadvar_cleanup ();
ExitThread(0);
# elif defined (WIN32)
vlc_threadvar_cleanup ();
_endthread ();
# else
# error Not implemented!
......
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