]> git.sesse.net Git - vlc/blobdiff - src/misc/objects.c
Allow setting the name of an object at any time...
[vlc] / src / misc / objects.c
index 6b0f5696c73882786a6f585be6fd7762565bc10a..36f8537d24cc8d5a0437156e6fda224e78dc0923 100644 (file)
 # include <errno.h> /* ENOSYS */
 #endif
 
+#include <limits.h>
 #include <assert.h>
 
 #if defined (HAVE_SYS_EVENTFD_H)
 # include <sys/eventfd.h>
-# define HAVE_EVENTFD 1
 #endif
 
 
@@ -122,7 +122,7 @@ void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
 
     p_priv->i_object_type = i_type;
     p_new->psz_object_type = psz_type;
-    p_new->psz_object_name = NULL;
+    p_priv->psz_name = NULL;
 
     p_new->b_die = false;
     p_new->b_error = false;
@@ -160,10 +160,7 @@ void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size,
     p_priv->pp_children = NULL;
     p_priv->i_children = 0;
 
-    p_new->p_private = NULL;
-
     /* Initialize mutexes and condvars */
-    vlc_mutex_init( &p_priv->lock );
     vlc_mutex_init( &p_priv->var_lock );
     vlc_cond_init( &p_priv->var_wait );
     p_priv->pipes[0] = p_priv->pipes[1] = -1;
@@ -206,18 +203,10 @@ void * __vlc_object_create( vlc_object_t *p_this, int i_type )
 
     switch( i_type )
     {
-        case VLC_OBJECT_INTF:
-            i_size = sizeof(intf_thread_t);
-            psz_type = "interface";
-            break;
         case VLC_OBJECT_DECODER:
             i_size = sizeof(decoder_t);
             psz_type = "decoder";
             break;
-        case VLC_OBJECT_PACKETIZER:
-            i_size = sizeof(decoder_t);
-            psz_type = "packetizer";
-            break;
         case VLC_OBJECT_AOUT:
             i_size = sizeof(aout_instance_t);
             psz_type = "audio output";
@@ -246,7 +235,41 @@ void __vlc_object_set_destructor( vlc_object_t *p_this,
                                   vlc_destructor_t pf_destructor )
 {
     vlc_object_internals_t *p_priv = vlc_internals(p_this );
+
+    vlc_spin_lock( &p_priv->ref_spin );
     p_priv->pf_destructor = pf_destructor;
+    vlc_spin_unlock( &p_priv->ref_spin );
+}
+
+static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
+
+#undef vlc_object_set_name
+int vlc_object_set_name(vlc_object_t *obj, const char *name)
+{
+    vlc_object_internals_t *priv = vlc_internals(obj);
+    char *newname = name ? strdup (name) : NULL;
+    char *oldname;
+
+    vlc_mutex_lock (&name_lock);
+    oldname = priv->psz_name;
+    priv->psz_name = newname;
+    vlc_mutex_unlock (&name_lock);
+
+    free (oldname);
+    return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM;
+}
+
+#undef vlc_object_get_name
+char *vlc_object_get_name(const vlc_object_t *obj)
+{
+    vlc_object_internals_t *priv = vlc_internals(obj);
+    char *name;
+
+    vlc_mutex_lock (&name_lock);
+    name = priv->psz_name ? strdup (priv->psz_name) : NULL;
+    vlc_mutex_unlock (&name_lock);
+
+    return name;
 }
 
 /**
@@ -289,10 +312,9 @@ static void vlc_object_destroy( vlc_object_t *p_this )
 
     free( p_this->psz_header );
 
-    FREENULL( p_this->psz_object_name );
+    free( p_priv->psz_name );
 
     vlc_spin_destroy( &p_priv->ref_spin );
-    vlc_mutex_destroy( &p_priv->lock );
     if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
         close( p_priv->pipes[1] );
     if( p_priv->pipes[0] != -1 )
@@ -304,19 +326,6 @@ static void vlc_object_destroy( vlc_object_t *p_this )
 }
 
 
-/** Inter-object signaling */
-
-void __vlc_object_lock( vlc_object_t *obj )
-{
-    vlc_mutex_lock( &(vlc_internals(obj)->lock) );
-}
-
-void __vlc_object_unlock( vlc_object_t *obj )
-{
-    vlc_assert_locked( &(vlc_internals(obj)->lock) );
-    vlc_mutex_unlock( &(vlc_internals(obj)->lock) );
-}
-
 #ifdef WIN32
 # include <winsock2.h>
 # include <ws2tcpip.h>
@@ -475,6 +484,18 @@ void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode )
     return p_found;
 }
 
+
+static int objnamecmp(const vlc_object_t *obj, const char *name)
+{
+    char *objname = vlc_object_get_name(obj);
+    if (objname == NULL)
+        return INT_MIN;
+
+    int ret = strcmp (objname, name);
+    free (objname);
+    return ret;
+}
+
 #undef vlc_object_find_name
 /**
  * Finds a named object and increment its reference count.
@@ -497,10 +518,11 @@ vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
 {
     vlc_object_t *p_found;
 
+    /* Reading psz_object_name from a separate inhibits thread-safety.
+     * Use a libvlc address variable instead for that sort of things! */
+    msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name );
     /* If have the requested name ourselves, don't look further */
-    if( !(i_mode & FIND_STRICT)
-        && p_this->psz_object_name
-        && !strcmp( p_this->psz_object_name, psz_name ) )
+    if( !(i_mode & FIND_STRICT) && !objnamecmp(p_this, psz_name) )
     {
         vlc_object_hold( p_this );
         return p_this;
@@ -587,8 +609,6 @@ void __vlc_object_release( vlc_object_t *p_this )
 
     if( b_should_destroy )
     {
-        /* We have no children */
-        assert (internals->i_children == 0);
         parent = p_this->p_parent;
 
 #ifndef NDEBUG
@@ -600,9 +620,8 @@ void __vlc_object_release( vlc_object_t *p_this )
             {
                 /* We are leaking this object */
                 fprintf( stderr,
-                         "ERROR: leaking object (%p, type:%s, name:%s)\n",
-                         leaked, leaked->psz_object_type,
-                         leaked->psz_object_name );
+                         "ERROR: leaking object (%p, type:%s)\n",
+                         leaked, leaked->psz_object_type );
                 /* Dump object to ease debugging */
                 vlc_object_dump( leaked );
                 fflush(stderr);
@@ -622,6 +641,9 @@ void __vlc_object_release( vlc_object_t *p_this )
         if (parent)
             /* Detach from parent to protect against FIND_CHILDREN */
             vlc_object_detach_unlocked (p_this);
+
+        /* We have no children */
+        assert (internals->i_children == 0);
     }
     libvlc_unlock (p_this->p_libvlc);
 
@@ -1032,8 +1054,7 @@ static vlc_object_t * FindObjectName( vlc_object_t *p_this,
         p_tmp = p_this->p_parent;
         if( p_tmp )
         {
-            if( p_tmp->psz_object_name
-                && !strcmp( p_tmp->psz_object_name, psz_name ) )
+            if( !objnamecmp(p_tmp, psz_name) )
             {
                 vlc_object_hold( p_tmp );
                 return p_tmp;
@@ -1049,8 +1070,7 @@ static vlc_object_t * FindObjectName( vlc_object_t *p_this,
         for( i = vlc_internals( p_this )->i_children; i--; )
         {
             p_tmp = vlc_internals( p_this )->pp_children[i];
-            if( p_tmp->psz_object_name
-                && !strcmp( p_tmp->psz_object_name, psz_name ) )
+            if( !objnamecmp(p_tmp, psz_name ) )
             {
                 vlc_object_hold( p_tmp );
                 return p_tmp;
@@ -1082,9 +1102,11 @@ static void PrintObject( vlc_object_t *p_this, const char *psz_prefix )
 
     int canc = vlc_savecancel ();
     memset( &psz_name, 0, sizeof(psz_name) );
-    if( p_this->psz_object_name )
+    char *name = vlc_object_get_name(p_this);
+    if( name )
     {
-        snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name );
+        snprintf( psz_name, 49, " \"%s\"", name );
+        free( name );
         if( psz_name[48] )
             psz_name[48] = '\"';
     }