[vlc-devel] [PATCH 4/4] cpu: remove initialization lock

RĂ©mi Denis-Courmont remi at remlab.net
Sun Mar 29 22:43:55 CEST 2020


In the unlikely event that multiple threads want to check the CPU
flags concurrently, simply compute them as many times.
---
 src/misc/cpu.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/misc/cpu.c b/src/misc/cpu.c
index 7f3a89a3cb..7053a391e7 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,18 @@ out:
     return i_capabilities;
 }
 
-static uint32_t cpu_flags;
-
-static void vlc_CPU_init(void)
-{
-    cpu_flags = vlc_CPU_raw();
-}
+static atomic_uint cpu_flags = ATOMIC_VAR_INIT(-1);
 
 VLC_WEAK unsigned vlc_CPU(void)
 {
-    static vlc_once_t once = VLC_STATIC_ONCE;
-    vlc_once(&once, vlc_CPU_init);
-    return cpu_flags;
+    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)
-- 
2.26.0



More information about the vlc-devel mailing list