Commit 95b0fabc authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

objects: use atomic reference counter instead of spin lock + counter

parent fd82bdcb
...@@ -682,13 +682,7 @@ void libvlc_InternalDestroy( libvlc_int_t *p_libvlc ) ...@@ -682,13 +682,7 @@ void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
vlc_ExitDestroy( &priv->exit ); vlc_ExitDestroy( &priv->exit );
vlc_mutex_destroy( &priv->ml_lock ); vlc_mutex_destroy( &priv->ml_lock );
#ifndef NDEBUG /* Hack to dump leaked objects tree */ assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
if( vlc_internals( p_libvlc )->i_refcount > 1 )
while( vlc_internals( p_libvlc )->i_refcount > 0 )
vlc_object_release( p_libvlc );
#endif
assert( vlc_internals( p_libvlc )->i_refcount == 1 );
vlc_object_release( p_libvlc ); vlc_object_release( p_libvlc );
} }
......
...@@ -133,8 +133,7 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length, ...@@ -133,8 +133,7 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length,
vlc_mutex_init (&priv->var_lock); vlc_mutex_init (&priv->var_lock);
vlc_cond_init (&priv->var_wait); vlc_cond_init (&priv->var_wait);
priv->pipes[0] = priv->pipes[1] = -1; priv->pipes[0] = priv->pipes[1] = -1;
vlc_spin_init (&priv->ref_spin); atomic_init (&priv->refs, 1);
priv->i_refcount = 1;
priv->pf_destructor = NULL; priv->pf_destructor = NULL;
priv->prev = NULL; priv->prev = NULL;
priv->first = NULL; priv->first = NULL;
...@@ -213,9 +212,7 @@ void vlc_object_set_destructor( vlc_object_t *p_this, ...@@ -213,9 +212,7 @@ void vlc_object_set_destructor( vlc_object_t *p_this,
{ {
vlc_object_internals_t *p_priv = vlc_internals(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; p_priv->pf_destructor = pf_destructor;
vlc_spin_unlock( &p_priv->ref_spin );
} }
static vlc_mutex_t name_lock = VLC_STATIC_MUTEX; static vlc_mutex_t name_lock = VLC_STATIC_MUTEX;
...@@ -279,7 +276,6 @@ static void vlc_object_destroy( vlc_object_t *p_this ) ...@@ -279,7 +276,6 @@ static void vlc_object_destroy( vlc_object_t *p_this )
free( p_priv->psz_name ); 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] ) if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] )
close( p_priv->pipes[1] ); close( p_priv->pipes[1] );
if( p_priv->pipes[0] != -1 ) if( p_priv->pipes[0] != -1 )
...@@ -452,13 +448,9 @@ vlc_object_t *vlc_object_find_name( vlc_object_t *p_this, const char *psz_name ) ...@@ -452,13 +448,9 @@ vlc_object_t *vlc_object_find_name( vlc_object_t *p_this, const char *psz_name )
void * vlc_object_hold( vlc_object_t *p_this ) void * vlc_object_hold( vlc_object_t *p_this )
{ {
vlc_object_internals_t *internals = vlc_internals( 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 ); assert (refs > 0); /* Avoid obvious freed object uses */ (void) refs;
/* Avoid obvious freed object uses */
assert( internals->i_refcount > 0 );
/* Increment the counter */
internals->i_refcount++;
vlc_spin_unlock( &internals->ref_spin );
return p_this; return p_this;
} }
...@@ -471,43 +463,38 @@ void vlc_object_release( vlc_object_t *p_this ) ...@@ -471,43 +463,38 @@ void vlc_object_release( vlc_object_t *p_this )
{ {
vlc_object_internals_t *internals = vlc_internals( p_this ); vlc_object_internals_t *internals = vlc_internals( p_this );
vlc_object_t *parent = NULL; vlc_object_t *parent = NULL;
bool b_should_destroy; unsigned refs = atomic_load (&internals->refs);
vlc_spin_lock( &internals->ref_spin ); /* Fast path */
assert( internals->i_refcount > 0 ); while (refs > 1)
if( internals->i_refcount > 1 )
{ {
/* Fast path */ if (atomic_compare_exchange_weak (&internals->refs, &refs, refs - 1))
/* There are still other references to the object */ return; /* There are still other references to the object */
internals->i_refcount--;
vlc_spin_unlock( &internals->ref_spin ); assert (refs > 0);
return;
} }
vlc_spin_unlock( &internals->ref_spin );
/* Slow path */ /* Slow path */
/* Remember that we cannot hold the spin while waiting on the mutex */
libvlc_lock (p_this->p_libvlc); libvlc_lock (p_this->p_libvlc);
/* Take the spin again. Note that another thread may have held the refs = atomic_fetch_sub (&internals->refs, 1);
* object in the (very short) mean time. */ assert (refs > 0);
vlc_spin_lock( &internals->ref_spin );
b_should_destroy = --internals->i_refcount == 0;
vlc_spin_unlock( &internals->ref_spin );
if( b_should_destroy ) if (likely(refs == 1))
{ {
/* Detach from parent to protect against vlc_object_find_name() */ /* Detach from parent to protect against vlc_object_find_name() */
parent = p_this->p_parent; parent = p_this->p_parent;
if (likely(parent)) if (likely(parent))
{ {
/* Unlink */ /* Unlink */
if (internals->prev != NULL) vlc_object_internals_t *prev = internals->prev;
internals->prev->next = internals->next; vlc_object_internals_t *next = internals->next;
if (prev != NULL)
prev->next = next;
else else
vlc_internals(parent)->first = internals->next; vlc_internals (parent)->first = next;
if (internals->next != NULL) if (next != NULL)
internals->next->prev = internals->prev; next->prev = prev;
} }
/* We have no children */ /* We have no children */
...@@ -515,11 +502,9 @@ void vlc_object_release( vlc_object_t *p_this ) ...@@ -515,11 +502,9 @@ void vlc_object_release( vlc_object_t *p_this )
} }
libvlc_unlock (p_this->p_libvlc); libvlc_unlock (p_this->p_libvlc);
if( b_should_destroy ) if (likely(refs == 1))
{ {
int canc; int canc = vlc_savecancel ();
canc = vlc_savecancel ();
vlc_object_destroy( p_this ); vlc_object_destroy( p_this );
vlc_restorecancel (canc); vlc_restorecancel (canc);
if (parent) if (parent)
...@@ -727,9 +712,7 @@ static void PrintObject( vlc_object_internals_t *priv, ...@@ -727,9 +712,7 @@ static void PrintObject( vlc_object_internals_t *priv,
} }
vlc_mutex_unlock (&name_lock); vlc_mutex_unlock (&name_lock);
psz_refcount[0] = '\0'; snprintf( psz_refcount, 19, ", %u refs", atomic_load( &priv->refs ) );
if( priv->i_refcount > 0 )
snprintf( psz_refcount, 19, ", %u refs", priv->i_refcount );
psz_parent[0] = '\0'; psz_parent[0] = '\0';
/* FIXME: need structure lock!!! */ /* FIXME: need structure lock!!! */
......
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
#ifndef LIBVLC_VARIABLES_H #ifndef LIBVLC_VARIABLES_H
# define LIBVLC_VARIABLES_H 1 # define LIBVLC_VARIABLES_H 1
# include <vlc_atomic.h>
/** /**
* Private LibVLC data for each object. * Private LibVLC data for each object.
*/ */
...@@ -41,8 +43,7 @@ struct vlc_object_internals ...@@ -41,8 +43,7 @@ struct vlc_object_internals
int pipes[2]; int pipes[2];
/* Objects management */ /* Objects management */
vlc_spinlock_t ref_spin; atomic_uint refs;
unsigned i_refcount;
vlc_destructor_t pf_destructor; vlc_destructor_t pf_destructor;
/* Objects tree structure */ /* Objects tree structure */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment