From e5640463ca1c7e1fa7bf2a1e87e0d5136c264e5b Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Denis-Courmont?= Date: Mon, 28 Jun 2010 02:19:22 +0300 Subject: [PATCH] Rewrite GC code on top of atomic ops (This saves one useless spin lock per GC object) --- include/vlc_common.h | 4 ++-- src/libvlc.c | 56 ++++---------------------------------------- 2 files changed, 7 insertions(+), 53 deletions(-) diff --git a/include/vlc_common.h b/include/vlc_common.h index 7a549c231a..5e48e418f1 100644 --- a/include/vlc_common.h +++ b/include/vlc_common.h @@ -558,10 +558,10 @@ typedef int ( * vlc_callback_t ) ( vlc_object_t *, /* variable's object */ # define VLC_OBJECT( x ) ((vlc_object_t *)(x)) #endif +#include typedef struct gc_object_t { - vlc_spinlock_t spin; - uintptr_t refs; + vlc_atomic_t refs; void (*pf_destructor) (struct gc_object_t *); } gc_object_t; diff --git a/src/libvlc.c b/src/libvlc.c index 1651430820..47c61a3c10 100644 --- a/src/libvlc.c +++ b/src/libvlc.c @@ -80,6 +80,7 @@ #include #include #include +#include #include "libvlc.h" @@ -118,18 +119,7 @@ void *vlc_gc_init (gc_object_t *p_gc, void (*pf_destruct) (gc_object_t *)) assert (pf_destruct); p_gc->pf_destructor = pf_destruct; - p_gc->refs = 1; -#if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) - __sync_synchronize (); -#elif defined (WIN32) && defined (__GNUC__) -#elif defined(__APPLE__) - OSMemoryBarrier (); -#else - /* Nobody else can possibly lock the spin - it's there as a barrier */ - vlc_spin_init (&p_gc->spin); - vlc_spin_lock (&p_gc->spin); - vlc_spin_unlock (&p_gc->spin); -#endif + vlc_atomic_set (&p_gc->refs, 1); return p_gc; } @@ -141,22 +131,9 @@ void *vlc_gc_init (gc_object_t *p_gc, void (*pf_destruct) (gc_object_t *)) void *vlc_hold (gc_object_t * p_gc) { uintptr_t refs; + assert( p_gc ); - assert ((((uintptr_t)&p_gc->refs) & (sizeof (void *) - 1)) == 0); /* alignment */ - -#if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) - refs = __sync_add_and_fetch (&p_gc->refs, 1); -#elif defined (WIN64) - refs = InterlockedIncrement64 (&p_gc->refs); -#elif defined (WIN32) - refs = InterlockedIncrement (&p_gc->refs); -#elif defined(__APPLE__) - refs = OSAtomicIncrement32Barrier((int*)&p_gc->refs); -#else - vlc_spin_lock (&p_gc->spin); - refs = ++p_gc->refs; - vlc_spin_unlock (&p_gc->spin); -#endif + refs = vlc_atomic_inc (&p_gc->refs); assert (refs != 1); /* there had to be a reference already */ return p_gc; } @@ -170,33 +147,10 @@ void vlc_release (gc_object_t *p_gc) unsigned refs; assert( p_gc ); - assert ((((uintptr_t)&p_gc->refs) & (sizeof (void *) - 1)) == 0); /* alignment */ - -#if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) - refs = __sync_sub_and_fetch (&p_gc->refs, 1); -#elif defined (WIN64) - refs = InterlockedDecrement64 (&p_gc->refs); -#elif defined (WIN32) - refs = InterlockedDecrement (&p_gc->refs); -#elif defined(__APPLE__) - refs = OSAtomicDecrement32Barrier((int*)&p_gc->refs); -#else - vlc_spin_lock (&p_gc->spin); - refs = --p_gc->refs; - vlc_spin_unlock (&p_gc->spin); -#endif - + refs = vlc_atomic_dec (&p_gc->refs); assert (refs != (uintptr_t)(-1)); /* reference underflow?! */ if (refs == 0) - { -#if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4) -#elif defined (WIN32) && defined (__GNUC__) -#elif defined(__APPLE__) -#else - vlc_spin_destroy (&p_gc->spin); -#endif p_gc->pf_destructor (p_gc); - } } /***************************************************************************** -- 2.39.2