/*****************************************************************************
* 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;
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 );
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));
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 ();
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;
}
{
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 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
}
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!!! */