[vlc-devel] [PATCH] lib: media: Use an atomic refcounter

Hugo Beauzée-Luyssen hugo at beauzee.fr
Wed Oct 2 10:35:57 CEST 2019


Instead of an unprotected plain integer
---
 lib/media.c          | 8 +++-----
 lib/media_internal.h | 3 ++-
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/lib/media.c b/lib/media.c
index 61cb82a889..dd0513633f 100644
--- a/lib/media.c
+++ b/lib/media.c
@@ -394,7 +394,7 @@ libvlc_media_t * libvlc_media_new_from_input_item(
 
     p_md->p_libvlc_instance = p_instance;
     p_md->p_input_item      = p_input_item;
-    p_md->i_refcount        = 1;
+    vlc_atomic_rc_init(&p_md->refcount);
 
     vlc_cond_init(&p_md->parsed_cond);
     vlc_mutex_init(&p_md->parsed_lock);
@@ -547,9 +547,7 @@ void libvlc_media_release( libvlc_media_t *p_md )
     if (!p_md)
         return;
 
-    p_md->i_refcount--;
-
-    if( p_md->i_refcount > 0 )
+    if(!vlc_atomic_rc_dec( &p_md->refcount ))
         return;
 
     uninstall_input_item_observer( p_md );
@@ -585,7 +583,7 @@ void libvlc_media_release( libvlc_media_t *p_md )
 void libvlc_media_retain( libvlc_media_t *p_md )
 {
     assert (p_md);
-    p_md->i_refcount++;
+    vlc_atomic_rc_inc(&p_md->refcount);
 }
 
 /**************************************************************************
diff --git a/lib/media_internal.h b/lib/media_internal.h
index aefc731c68..cfeb056178 100644
--- a/lib/media_internal.h
+++ b/lib/media_internal.h
@@ -29,12 +29,13 @@
 
 #include <vlc_common.h>
 #include <vlc_input.h>
+#include <vlc_atomic.h>
 
 struct libvlc_media_t
 {
     libvlc_event_manager_t event_manager;
     input_item_t      *p_input_item;
-    int                i_refcount;
+    vlc_atomic_rc_t   refcount;
     libvlc_instance_t *p_libvlc_instance;
     libvlc_state_t     state;
     VLC_FORWARD_DECLARE_OBJECT(libvlc_media_list_t*) p_subitems; /* A media descriptor can have Sub items. This is the only dependancy we really have on media_list */
-- 
2.20.1



More information about the vlc-devel mailing list