[vlc-commits] cpu: remove initialization lock

Rémi Denis-Courmont git at videolan.org
Mon Mar 30 15:51:29 CEST 2020


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Mar 29 17:34:10 2020 +0300| [fd7857126a2a59bda6ad930461b61fff1114b9d4] | committer: Rémi Denis-Courmont

cpu: remove initialization lock

In the unlikely event that multiple threads want to check the CPU
flags concurrently, simply compute them as many times.

This removes the useless barriers, and the only usage of vlc_once() in
the core.

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

 src/misc/cpu.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/src/misc/cpu.c b/src/misc/cpu.c
index c73a513607..3bf4f8c63f 100644
--- a/src/misc/cpu.c
+++ b/src/misc/cpu.c
@@ -29,6 +29,8 @@
 # include "config.h"
 #endif
 
+#include <stdatomic.h>
+
 #include <vlc_common.h>
 #include <vlc_cpu.h>
 #include <vlc_memstream.h>
@@ -247,18 +249,17 @@ out:
     return i_capabilities;
 }
 
-static unsigned cpu_flags;
-
-static void vlc_CPU_init(void)
-{
-    cpu_flags = vlc_CPU_raw();
-}
-
 unsigned vlc_CPU(void)
 {
-    static vlc_once_t once = VLC_STATIC_ONCE;
-    vlc_once(&once, vlc_CPU_init);
-    return cpu_flags;
+    static atomic_uint cpu_flags = ATOMIC_VAR_INIT(-1);
+    unsigned flags = atomic_load_explicit(&cpu_flags, memory_order_relaxed);
+
+    if (unlikely(flags == -1U)) {
+        flags = vlc_CPU_raw();
+        atomic_store_explicit(&cpu_flags, flags, memory_order_relaxed);
+    }
+
+    return flags;
 }
 
 void vlc_CPU_dump (vlc_object_t *obj)



More information about the vlc-commits mailing list