[vlc-commits] libvlc: small simplification of thread initialization

Rémi Denis-Courmont git at videolan.org
Wed Apr 18 22:11:19 CEST 2012


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Apr 18 23:07:14 2012 +0300| [704980f3cecb564703cb159e1971a2f2c5405756] | committer: Rémi Denis-Courmont

libvlc: small simplification of thread initialization

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

 lib/core.c            |    8 ++++----
 lib/error.c           |   34 ++++++++++++----------------------
 lib/libvlc_internal.h |    4 ++--
 3 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/lib/core.c b/lib/core.c
index b115d37..a340d11 100644
--- a/lib/core.c
+++ b/lib/core.c
@@ -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 ();
     }
 }
 
diff --git a/lib/error.c b/lib/error.c
index ef2ecdc..f942f4d 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -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)
diff --git a/lib/libvlc_internal.h b/lib/libvlc_internal.h
index c3a081d..27c1dc5 100644
--- a/lib/libvlc_internal.h
+++ b/lib/libvlc_internal.h
@@ -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(



More information about the vlc-commits mailing list