[vlc-commits] commit: Rewrite GC code on top of atomic ops ( Rémi Denis-Courmont )

git at videolan.org git at videolan.org
Mon Jun 28 01:31:55 CEST 2010


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Mon Jun 28 02:19:22 2010 +0300| [e5640463ca1c7e1fa7bf2a1e87e0d5136c264e5b] | committer: Rémi Denis-Courmont 

Rewrite GC code on top of atomic ops

(This saves one useless spin lock per GC object)

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=e5640463ca1c7e1fa7bf2a1e87e0d5136c264e5b
---

 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 7a549c2..5e48e41 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 <vlc_atomic.h>
 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 1651430..47c61a3 100644
--- a/src/libvlc.c
+++ b/src/libvlc.c
@@ -80,6 +80,7 @@
 #include <vlc_fs.h>
 #include <vlc_cpu.h>
 #include <vlc_url.h>
+#include <vlc_atomic.h>
 
 #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);
-    }
 }
 
 /*****************************************************************************



More information about the vlc-commits mailing list