Commit 704980f3 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

libvlc: small simplification of thread initialization

parent 952370a4
......@@ -40,12 +40,12 @@ static const char nomemstr[] = "Insufficient memory";
libvlc_instance_t * libvlc_new( int argc, const char *const *argv )
{
libvlc_threads_init ();
libvlc_instance_t *p_new = malloc (sizeof (*p_new));
if (unlikely(p_new == NULL))
return NULL;
libvlc_init_threads ();
const char *my_argv[argc + 2];
my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
for( int i = 0; i < argc; i++ )
......@@ -74,8 +74,8 @@ libvlc_instance_t * libvlc_new( int argc, const char *const *argv )
return p_new;
error:
libvlc_deinit_threads ();
free (p_new);
libvlc_threads_deinit ();
return NULL;
}
......@@ -107,7 +107,7 @@ void libvlc_release( libvlc_instance_t *p_instance )
libvlc_InternalCleanup( p_instance->p_libvlc_int );
libvlc_InternalDestroy( p_instance->p_libvlc_int );
free( p_instance );
libvlc_deinit_threads ();
libvlc_threads_deinit ();
}
}
......
......@@ -30,34 +30,24 @@ static const char oom[] = "Out of memory";
/* TODO: use only one thread-specific key for whole libvlc */
static vlc_threadvar_t context;
static void libvlc_setup_threads (bool init)
{
static vlc_mutex_t lock = VLC_STATIC_MUTEX;
static uintptr_t refs = 0;
static vlc_mutex_t lock = VLC_STATIC_MUTEX;
static uintptr_t refs = 0;
void libvlc_threads_init (void)
{
vlc_mutex_lock (&lock);
if (init)
{
if (refs++ == 0)
vlc_threadvar_create (&context, free);
}
else
{
assert (refs > 0);
if (--refs == 0)
vlc_threadvar_delete (&context);
}
if (refs++ == 0)
vlc_threadvar_create (&context, free);
vlc_mutex_unlock (&lock);
}
void libvlc_init_threads (void)
void libvlc_threads_deinit (void)
{
libvlc_setup_threads (true);
}
void libvlc_deinit_threads (void)
{
libvlc_setup_threads (false);
vlc_mutex_lock (&lock);
assert (refs > 0);
if (--refs == 0)
vlc_threadvar_delete (&context);
vlc_mutex_unlock (&lock);
}
static char *get_error (void)
......
......@@ -80,8 +80,8 @@ struct libvlc_instance_t
***************************************************************************/
/* Thread context */
void libvlc_init_threads (void);
void libvlc_deinit_threads (void);
void libvlc_threads_init (void);
void libvlc_threads_deinit (void);
/* Events */
libvlc_event_manager_t * libvlc_event_manager_new(
......
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