]> git.sesse.net Git - vlc/blobdiff - src/misc/objects.c
Win32: use Win32DebugOutputMsgW instead of ANSI version
[vlc] / src / misc / objects.c
index a0b90d0bfbca4982ce8ee08a7cf395f946353b2d..3e365bda3c66cc936d5f74eb4bdd9b4bed557ef1 100644 (file)
@@ -1,24 +1,24 @@
 /*****************************************************************************
  * objects.c: vlc_object_t handling
  *****************************************************************************
- * Copyright (C) 2004-2008 the VideoLAN team
+ * Copyright (C) 2004-2008 VLC authors and VideoLAN
  * Copyright (C) 2006-2010 RĂ©mi Denis-Courmont
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /**
 # include <search.h>
 #endif
 
-#ifndef WIN32
-# include <vlc_fs.h>
-# include <unistd.h>
-#else
+#ifdef __OS2__
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <unistd.h>    // close(), write()
+#elif defined(WIN32)
 # include <io.h>
 # include <winsock2.h>
 # include <ws2tcpip.h>
@@ -65,6 +66,9 @@
 # define write( a, b, c ) send (a, b, c, 0)
 # undef  close
 # define close( a )       closesocket (a)
+#else
+# include <vlc_fs.h>
+# include <unistd.h>
 #endif
 
 #include <limits.h>
@@ -85,7 +89,7 @@
 static int  DumpCommand( vlc_object_t *, char const *,
                          vlc_value_t, vlc_value_t, void * );
 
-static vlc_object_t * FindChildName ( vlc_object_internals_t *, const char * );
+static vlc_object_t * FindName ( vlc_object_internals_t *, const char * );
 static void PrintObject( vlc_object_internals_t *, const char * );
 static void DumpStructure( vlc_object_internals_t *, unsigned, char * );
 
@@ -108,7 +112,7 @@ static void libvlc_unlock (libvlc_int_t *p_libvlc)
 
 #undef vlc_custom_create
 void *vlc_custom_create (vlc_object_t *parent, size_t length,
-                         int type, const char *typename)
+                         const char *typename)
 {
     /* NOTE:
      * VLC objects are laid out as follow:
@@ -124,14 +128,13 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length,
     vlc_object_internals_t *priv = malloc (sizeof (*priv) + length);
     if (unlikely(priv == NULL))
         return NULL;
-    priv->i_object_type = type;
     priv->psz_name = NULL;
     priv->var_root = NULL;
     vlc_mutex_init (&priv->var_lock);
     vlc_cond_init (&priv->var_wait);
     priv->pipes[0] = priv->pipes[1] = -1;
-    vlc_spin_init (&priv->ref_spin);
-    priv->i_refcount = 1;
+    atomic_init (&priv->alive, true);
+    atomic_init (&priv->refs, 1);
     priv->pf_destructor = NULL;
     priv->prev = NULL;
     priv->first = NULL;
@@ -139,8 +142,8 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length,
     vlc_object_t *obj = (vlc_object_t *)(priv + 1);
     obj->psz_object_type = typename;
     obj->psz_header = NULL;
-    obj->b_die = false;
     obj->b_force = false;
+    memset (obj + 1, 0, length - sizeof (*obj)); /* type-specific stuff */
 
     if (likely(parent != NULL))
     {
@@ -179,7 +182,6 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length,
         vlc_restorecancel (canc);
     }
 
-    memset (obj + 1, 0, length - sizeof (*obj));
     return obj;
 }
 
@@ -193,7 +195,7 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length,
  */
 void *vlc_object_create( vlc_object_t *p_this, size_t i_size )
 {
-    return vlc_custom_create( p_this, i_size, VLC_OBJECT_GENERIC, "generic" );
+    return vlc_custom_create( p_this, i_size, "generic" );
 }
 
 #undef vlc_object_set_destructor
@@ -210,9 +212,7 @@ void vlc_object_set_destructor( vlc_object_t *p_this,
 {
     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;
@@ -259,6 +259,13 @@ static void vlc_object_destroy( vlc_object_t *p_this )
     if( p_priv->pf_destructor )
         p_priv->pf_destructor( p_this );
 
+    if (unlikely(p_this == VLC_OBJECT(p_this->p_libvlc)))
+    {
+        /* TODO: should be in src/libvlc.c */
+        var_DelCallback (p_this, "tree", DumpCommand, p_this);
+        var_DelCallback (p_this, "vars", DumpCommand, p_this);
+    }
+
     /* Destroy the associated variables. */
     var_DestroyAll( p_this );
 
@@ -269,7 +276,6 @@ static void vlc_object_destroy( vlc_object_t *p_this )
 
     free( p_priv->psz_name );
 
-    vlc_spin_destroy( &p_priv->ref_spin );
     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 )
@@ -281,35 +287,35 @@ static void vlc_object_destroy( vlc_object_t *p_this )
 }
 
 
-#ifdef WIN32
+#if defined(WIN32) || defined(__OS2__)
 /**
  * select()-able pipes emulated using Winsock
  */
 # define vlc_pipe selectable_pipe
 static int selectable_pipe (int fd[2])
 {
-    SOCKADDR_IN addr;
+    struct sockaddr_in addr;
     int addrlen = sizeof (addr);
 
-    SOCKET l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP), a,
-           c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
-    if ((l == INVALID_SOCKET) || (c == INVALID_SOCKET))
+    int l = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP),
+        c = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
+    if (l == -1 || c == -1)
         goto error;
 
     memset (&addr, 0, sizeof (addr));
     addr.sin_family = AF_INET;
     addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
-    if (bind (l, (PSOCKADDR)&addr, sizeof (addr))
-     || getsockname (l, (PSOCKADDR)&addr, &addrlen)
+    if (bind (l, (struct sockaddr *)&addr, sizeof (addr))
+     || getsockname (l, (struct sockaddr *)&addr, &addrlen)
      || listen (l, 1)
-     || connect (c, (PSOCKADDR)&addr, addrlen))
+     || connect (c, (struct sockaddr *)&addr, addrlen))
         goto error;
 
-    a = accept (l, NULL, NULL);
-    if (a == INVALID_SOCKET)
+    int a = accept (l, NULL, NULL);
+    if (a == -1)
         goto error;
 
-    closesocket (l);
+    close (l);
     //shutdown (a, 0);
     //shutdown (c, 1);
     fd[0] = c;
@@ -317,13 +323,13 @@ static int selectable_pipe (int fd[2])
     return 0;
 
 error:
-    if (l != INVALID_SOCKET)
-        closesocket (l);
-    if (c != INVALID_SOCKET)
-        closesocket (c);
+    if (l != -1)
+        close (l);
+    if (c != -1)
+        close (c);
     return -1;
 }
-#endif /* WIN32 */
+#endif /* WIN32 || __OS2__ */
 
 static vlc_mutex_t pipe_lock = VLC_STATIC_MUTEX;
 
@@ -361,7 +367,7 @@ int vlc_object_waitpipe( vlc_object_t *obj )
                 internals->pipes[0] = internals->pipes[1] = -1;
         }
 
-        if (internals->pipes[0] != -1 && obj->b_die)
+        if (internals->pipes[0] != -1 && !atomic_load (&internals->alive))
         {   /* Race condition: vlc_object_kill() already invoked! */
             msg_Dbg (obj, "waitpipe: object already dying");
             write (internals->pipes[1], &(uint64_t){ 1 }, sizeof (uint64_t));
@@ -382,16 +388,13 @@ void vlc_object_kill( vlc_object_t *p_this )
     vlc_object_internals_t *priv = vlc_internals( p_this );
     int fd = -1;
 
-    vlc_mutex_lock( &pipe_lock );
-    if( !p_this->b_die )
+    if (atomic_exchange (&priv->alive, false))
     {
+        vlc_mutex_lock (&pipe_lock);
         fd = priv->pipes[1];
-        p_this->b_die = true;
+        vlc_mutex_unlock (&pipe_lock);
     }
 
-    /* This also serves as a memory barrier toward vlc_object_alive(): */
-    vlc_mutex_unlock( &pipe_lock );
-
     if (fd != -1)
     {
         int canc = vlc_savecancel ();
@@ -403,17 +406,6 @@ void vlc_object_kill( vlc_object_t *p_this )
     }
 }
 
-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.
@@ -426,35 +418,21 @@ static int objnamecmp(const vlc_object_t *obj, const char *name)
  *
  * @param p_this object to search from
  * @param psz_name name of the object to search for
- * @param i_mode search direction: FIND_PARENT, FIND_CHILD or FIND_ANYWHERE.
  *
  * @return a matching object (must be released by the caller),
  * or NULL on error.
  */
-vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
-                                    const char *psz_name, int i_mode )
+vlc_object_t *vlc_object_find_name( vlc_object_t *p_this, const char *psz_name )
 {
     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( !objnamecmp(p_this, psz_name) )
-    {
-        vlc_object_hold( p_this );
-        return p_this;
-    }
-
-    /* Otherwise, recursively look for the object */
-    if (i_mode == FIND_ANYWHERE)
-        return vlc_object_find_name (VLC_OBJECT(p_this->p_libvlc), psz_name,
-                                     FIND_CHILD);
+    msg_Err( p_this, "%s(\"%s\") is not safe!", __func__, psz_name );
 
-    assert (i_mode == FIND_CHILD);
     libvlc_lock (p_this->p_libvlc);
     vlc_mutex_lock (&name_lock);
-    p_found = FindChildName (vlc_internals (p_this), psz_name);
+    p_found = FindName (vlc_internals (p_this), psz_name);
     vlc_mutex_unlock (&name_lock);
     libvlc_unlock (p_this->p_libvlc);
     return p_found;
@@ -467,13 +445,9 @@ vlc_object_t *vlc_object_find_name( vlc_object_t *p_this,
 void * vlc_object_hold( vlc_object_t *p_this )
 {
     vlc_object_internals_t *internals = vlc_internals( p_this );
+    unsigned refs = atomic_fetch_add (&internals->refs, 1);
 
-    vlc_spin_lock( &internals->ref_spin );
-    /* Avoid obvious freed object uses */
-    assert( internals->i_refcount > 0 );
-    /* Increment the counter */
-    internals->i_refcount++;
-    vlc_spin_unlock( &internals->ref_spin );
+    assert (refs > 0); /* Avoid obvious freed object uses */ (void) refs;
     return p_this;
 }
 
@@ -486,43 +460,38 @@ void vlc_object_release( vlc_object_t *p_this )
 {
     vlc_object_internals_t *internals = vlc_internals( p_this );
     vlc_object_t *parent = NULL;
-    bool b_should_destroy;
+    unsigned refs = atomic_load (&internals->refs);
 
-    vlc_spin_lock( &internals->ref_spin );
-    assert( internals->i_refcount > 0 );
-
-    if( internals->i_refcount > 1 )
+    /* Fast path */
+    while (refs > 1)
     {
-        /* Fast path */
-        /* There are still other references to the object */
-        internals->i_refcount--;
-        vlc_spin_unlock( &internals->ref_spin );
-        return;
+        if (atomic_compare_exchange_weak (&internals->refs, &refs, refs - 1))
+            return; /* There are still other references to the object */
+
+        assert (refs > 0);
     }
-    vlc_spin_unlock( &internals->ref_spin );
 
     /* Slow path */
-    /* Remember that we cannot hold the spin while waiting on the mutex */
     libvlc_lock (p_this->p_libvlc);
-    /* Take the spin again. Note that another thread may have held the
-     * object in the (very short) mean time. */
-    vlc_spin_lock( &internals->ref_spin );
-    b_should_destroy = --internals->i_refcount == 0;
-    vlc_spin_unlock( &internals->ref_spin );
+    refs = atomic_fetch_sub (&internals->refs, 1);
+    assert (refs > 0);
 
-    if( b_should_destroy )
+    if (likely(refs == 1))
     {
-        /* Detach from parent to protect against FIND_CHILDREN */
+        /* Detach from parent to protect against vlc_object_find_name() */
         parent = p_this->p_parent;
         if (likely(parent))
         {
            /* Unlink */
-           if (internals->prev != NULL)
-               internals->prev->next = internals->next;
+           vlc_object_internals_t *prev = internals->prev;
+           vlc_object_internals_t *next = internals->next;
+
+           if (prev != NULL)
+               prev->next = next;
            else
-               vlc_internals(parent)->first = internals->next;
-           if (internals->next != NULL)
-               internals->next->prev = internals->prev;
+               vlc_internals (parent)->first = next;
+           if (next != NULL)
+               next->prev = prev;
         }
 
         /* We have no children */
@@ -530,11 +499,9 @@ void vlc_object_release( vlc_object_t *p_this )
     }
     libvlc_unlock (p_this->p_libvlc);
 
-    if( b_should_destroy )
+    if (likely(refs == 1))
     {
-        int canc;
-
-        canc = vlc_savecancel ();
+        int canc = vlc_savecancel ();
         vlc_object_destroy( p_this );
         vlc_restorecancel (canc);
         if (parent)
@@ -542,6 +509,17 @@ void vlc_object_release( vlc_object_t *p_this )
     }
 }
 
+#undef vlc_object_alive
+/**
+ * This function returns true, except when it returns false.
+ * \warning Do not use this function. Ever. You were warned.
+ */
+bool vlc_object_alive(vlc_object_t *obj)
+{
+    vlc_object_internals_t *internals = vlc_internals (obj);
+    return atomic_load (&internals->alive);
+}
+
 #undef vlc_list_children
 /**
  * Gets the list of children of an objects, and increment their reference
@@ -594,7 +572,6 @@ static void DumpVariable (const void *data, const VISIT which, const int depth)
         MYCASE( TIME, "time" );
         MYCASE( COORDS, "coords" );
         MYCASE( ADDRESS, "address" );
-        MYCASE( MUTEX, "mutex" );
 #undef MYCASE
     }
     printf( " *-o \"%s\" (%s", p_var->psz_name, psz_type );
@@ -610,7 +587,6 @@ static void DumpVariable (const void *data, const VISIT which, const int depth)
     switch( p_var->i_type & VLC_VAR_CLASS )
     {
         case VLC_VAR_VOID:
-        case VLC_VAR_MUTEX:
             break;
         case VLC_VAR_BOOL:
             printf( ": %s", p_var->val.b_bool ? "true" : "false" );
@@ -654,7 +630,7 @@ static int DumpCommand( vlc_object_t *p_this, char const *psz_cmd,
     if( *newval.psz_string )
     {
         /* try using the object's name to find it */
-        p_object = vlc_object_find_name( p_data, newval.psz_string, FIND_CHILD );
+        p_object = vlc_object_find_name( p_data, newval.psz_string );
         if( !p_object )
             return VLC_ENOOBJ;
     }
@@ -713,15 +689,14 @@ void vlc_list_release( vlc_list_t *p_list )
 
 /* Following functions are local */
 
-static vlc_object_t *FindChildName (vlc_object_internals_t *priv,
-                                    const char *name)
+static vlc_object_t *FindName (vlc_object_internals_t *priv, const char *name)
 {
+    if (priv->psz_name != NULL && !strcmp (priv->psz_name, name))
+        return vlc_object_hold (vlc_externals (priv));
+
     for (priv = priv->first; priv != NULL; priv = priv->next)
     {
-        if (priv->psz_name && !strcmp (priv->psz_name, name))
-            return vlc_object_hold (vlc_externals (priv));
-
-        vlc_object_t *found = FindChildName (priv, name);
+        vlc_object_t *found = FindName (priv, name);
         if (found != NULL)
             return found;
     }
@@ -745,9 +720,7 @@ static void PrintObject( vlc_object_internals_t *priv,
     }
     vlc_mutex_unlock (&name_lock);
 
-    psz_refcount[0] = '\0';
-    if( priv->i_refcount > 0 )
-        snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
+    snprintf( psz_refcount, 19, ", %u refs", atomic_load( &priv->refs ) );
 
     psz_parent[0] = '\0';
     /* FIXME: need structure lock!!! */