[vlc-commits] stats: use atomic values where applicable

Rémi Denis-Courmont git at videolan.org
Tue Dec 12 19:36:55 CET 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Dec 12 19:33:03 2017 +0200| [b2642fe4499465371afa2d50d7e04ed1794d95b0] | committer: Rémi Denis-Courmont

stats: use atomic values where applicable

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

 src/input/decoder.c        | 22 ++++++++++++----------
 src/input/input_internal.h | 15 ++++++++-------
 src/input/stats.c          | 28 +++++++++++++++++++---------
 3 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/src/input/decoder.c b/src/input/decoder.c
index a8070b2325..f5dbbe6d61 100644
--- a/src/input/decoder.c
+++ b/src/input/decoder.c
@@ -1097,11 +1097,12 @@ static void DecoderUpdateStatVideo( decoder_owner_sys_t *p_owner,
 
     if( stats != NULL )
     {
-        vlc_mutex_lock( &stats->lock );
-        stats->decoded_video += decoded;
-        stats->lost_pictures += lost;
-        stats->displayed_pictures += displayed;
-        vlc_mutex_unlock( &stats->lock );
+        atomic_fetch_add_explicit(&stats->decoded_video, decoded,
+                                  memory_order_relaxed);
+        atomic_fetch_add_explicit(&stats->lost_pictures, lost,
+                                  memory_order_relaxed);
+        atomic_fetch_add_explicit(&stats->displayed_pictures, displayed,
+                                  memory_order_relaxed);
     }
 }
 
@@ -1222,11 +1223,12 @@ static void DecoderUpdateStatAudio( decoder_owner_sys_t *p_owner,
 
     if( stats != NULL )
     {
-        vlc_mutex_lock( &stats->lock );
-        stats->lost_abuffers += lost;
-        stats->played_abuffers += played;
-        stats->decoded_audio += decoded;
-        vlc_mutex_unlock( &stats->lock );
+        atomic_fetch_add_explicit(&stats->lost_abuffers, lost,
+                                  memory_order_relaxed);
+        atomic_fetch_add_explicit(&stats->played_abuffers, played,
+                                  memory_order_relaxed);
+        atomic_fetch_add_explicit(&stats->decoded_audio, decoded,
+                                  memory_order_relaxed);
     }
 }
 
diff --git a/src/input/input_internal.h b/src/input/input_internal.h
index f00bd9ff9f..79cadb7af3 100644
--- a/src/input/input_internal.h
+++ b/src/input/input_internal.h
@@ -25,6 +25,7 @@
 #define LIBVLC_INPUT_INTERNAL_H 1
 
 #include <stddef.h>
+#include <stdatomic.h>
 
 #include <vlc_access.h>
 #include <vlc_demux.h>
@@ -275,6 +276,7 @@ typedef struct input_rate_t
 } input_rate_t;
 
 struct input_stats {
+    vlc_mutex_t lock;
     uintmax_t read_packets;
     uintmax_t read_bytes;
     input_rate_t input_bitrate;
@@ -282,13 +284,12 @@ struct input_stats {
     input_rate_t demux_bitrate;
     uintmax_t demux_corrupted;
     uintmax_t demux_discontinuity;
-    uintmax_t decoded_audio;
-    uintmax_t decoded_video;
-    uintmax_t played_abuffers;
-    uintmax_t lost_abuffers;
-    uintmax_t displayed_pictures;
-    uintmax_t lost_pictures;
-    vlc_mutex_t lock;
+    atomic_uintmax_t decoded_audio;
+    atomic_uintmax_t decoded_video;
+    atomic_uintmax_t played_abuffers;
+    atomic_uintmax_t lost_abuffers;
+    atomic_uintmax_t displayed_pictures;
+    atomic_uintmax_t lost_pictures;
 };
 
 struct input_stats *input_stats_Create(void);
diff --git a/src/input/stats.c b/src/input/stats.c
index 0f30d0ae92..4e02888ebd 100644
--- a/src/input/stats.c
+++ b/src/input/stats.c
@@ -58,6 +58,12 @@ struct input_stats *input_stats_Create(void)
     memset(stats, 0, sizeof (*stats));
     input_rate_Init(&stats->input_bitrate);
     input_rate_Init(&stats->demux_bitrate);
+    atomic_init(&stats->decoded_audio, 0);
+    atomic_init(&stats->decoded_video, 0);
+    atomic_init(&stats->played_abuffers, 0);
+    atomic_init(&stats->lost_abuffers, 0);
+    atomic_init(&stats->displayed_pictures, 0);
+    atomic_init(&stats->lost_pictures, 0);
     vlc_mutex_init(&stats->lock);
     return stats;
 }
@@ -81,19 +87,23 @@ void input_stats_Compute(struct input_stats *stats, input_stats_t *st)
     st->i_demux_corrupted = stats->demux_corrupted;
     st->i_demux_discontinuity = stats->demux_discontinuity;
 
-    /* Decoders */
-    st->i_decoded_video = stats->decoded_video;
-    st->i_decoded_audio = stats->decoded_audio;
+    vlc_mutex_unlock(&stats->lock);
 
     /* Aout */
-    st->i_played_abuffers = stats->played_abuffers;
-    st->i_lost_abuffers = stats->lost_abuffers;
+    st->i_decoded_audio = atomic_load_explicit(&stats->decoded_audio,
+                                               memory_order_relaxed);
+    st->i_played_abuffers = atomic_load_explicit(&stats->played_abuffers,
+                                                 memory_order_relaxed);
+    st->i_lost_abuffers = atomic_load_explicit(&stats->lost_abuffers,
+                                               memory_order_relaxed);
 
     /* Vouts */
-    st->i_displayed_pictures = stats->displayed_pictures;
-    st->i_lost_pictures = stats->lost_pictures;
-
-    vlc_mutex_unlock(&stats->lock);
+    st->i_decoded_video = atomic_load_explicit(&stats->decoded_video,
+                                               memory_order_relaxed);
+    st->i_displayed_pictures = atomic_load_explicit(&stats->displayed_pictures,
+                                                    memory_order_relaxed);
+    st->i_lost_pictures = atomic_load_explicit(&stats->lost_pictures,
+                                               memory_order_relaxed);
 }
 
 /** Update a counter element with new values



More information about the vlc-commits mailing list