/*****************************************************************************
* 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>
# 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>
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;
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 */
{
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;
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 )
}
-#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;
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;
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));
return internals->pipes[0];
}
-#undef vlc_object_kill
/**
- * Requests termination of an object, cancels the object thread, and make the
- * object wait pipe (if it exists) readable. Not a cancellation point.
+ * Hack for input objects. Should be removed eventually.
*/
-void vlc_object_kill( vlc_object_t *p_this )
+void ObjectKillChildrens( vlc_object_t *p_obj )
{
- vlc_object_internals_t *priv = vlc_internals( p_this );
- int fd = -1;
+ /* FIXME ObjectKillChildrens seems a very bad idea in fact */
+ /*if( p_obj == VLC_OBJECT(p_input->p->p_sout) ) return;*/
- vlc_mutex_lock( &pipe_lock );
- if( !p_this->b_die )
+ vlc_object_internals_t *priv = vlc_internals (p_obj);
+ if (atomic_exchange (&priv->alive, false))
{
+ int fd;
+
+ vlc_mutex_lock (&pipe_lock);
fd = priv->pipes[1];
- p_this->b_die = true;
+ vlc_mutex_unlock (&pipe_lock);
+ if (fd != -1)
+ {
+ write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
+ msg_Dbg (p_obj, "object waitpipe triggered");
+ }
}
- /* This also serves as a memory barrier toward vlc_object_alive(): */
- vlc_mutex_unlock( &pipe_lock );
-
- if (fd != -1)
- {
- int canc = vlc_savecancel ();
-
- /* write _after_ setting b_die, so vlc_object_alive() returns false */
- write (fd, &(uint64_t){ 1 }, sizeof (uint64_t));
- msg_Dbg (p_this, "waitpipe: object killed");
- vlc_restorecancel (canc);
- }
+ vlc_list_t *p_list = vlc_list_children( p_obj );
+ for( int i = 0; i < p_list->i_count; i++ )
+ ObjectKillChildrens( p_list->p_values[i].p_object );
+ vlc_list_release( p_list );
}
+
#undef vlc_object_find_name
/**
* Finds a named object and increment its reference count.
{
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_Err( p_this, "%s(\"%s\") is not safe!", __func__, psz_name );
+ /* The object name is not thread-safe, provides no warranty that the
+ * object is fully initialized and still active, and that its owner can
+ * deal with asynchronous and external state changes. There may be multiple
+ * objects with the same name, and the function may fail even if a matching
+ * object exists. DO NOT USE THIS IN NEW CODE. */
+#ifndef NDEBUG
+ /* This was officially deprecated on August 19 2009. For the convenience of
+ * wannabe code janitors, this is the list of names that remain used
+ * and unfixed since then. */
+ static const char bad[][11] = { "adjust", "clone", "colorthres",
+ "erase", "extract", "gradient", "logo", "marq", "motionblur", "puzzle",
+ "rotate", "sharpen", "transform", "v4l2", "wall" };
+ static const char poor[][13] = { "invert", "magnify", "motiondetect",
+ "psychedelic", "ripple", "wave" };
+ if( bsearch( psz_name, bad, 15, 11, (void *)strcmp ) == NULL
+ && bsearch( psz_name, poor, 6, 13, (void *)strcmp ) == NULL )
+ return NULL;
+ msg_Err( p_this, "looking for object \"%s\"... FIXME XXX", psz_name );
+#endif
libvlc_lock (p_this->p_libvlc);
vlc_mutex_lock (&name_lock);
void * vlc_object_hold( vlc_object_t *p_this )
{
vlc_object_internals_t *internals = vlc_internals( p_this );
-
- 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 );
+#ifndef NDEBUG
+ unsigned refs = atomic_fetch_add (&internals->refs, 1);
+ assert (refs > 0); /* Avoid obvious freed object uses */
+#else
+ atomic_fetch_add (&internals->refs, 1);
+#endif
return p_this;
}
{
vlc_object_internals_t *internals = vlc_internals( p_this );
vlc_object_t *parent = NULL;
- bool b_should_destroy;
-
- vlc_spin_lock( &internals->ref_spin );
- assert( internals->i_refcount > 0 );
+ unsigned refs = atomic_load (&internals->refs);
- 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 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 */
}
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)
}
}
+#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
+ * Gets the list of children of an object, and increment their reference
* count.
* @return a list (possibly empty) or NULL in case of error.
*/
}
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!!! */