<html><head></head><body>Hi,<br><br>It's a very peculiar and disagreeable opinion that the existing code is more readable. This patch follows the same logic as normal single thread lazy init would, which is more readable to me than the pthread_once() (anti)pattern.<br><br>As for why, it's much faster than pthread_once since it removes unnecessary barriers, and it removes the last (non-Linux) vlc_once usage.<br><br><div class="gmail_quote">Le 30 mars 2020 10:37:01 GMT+03:00, Thomas Guillem <thomas@gllm.fr> a écrit :<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail">Hello,<br><br>On Sun, Mar 29, 2020, at 22:43, Rémi Denis-Courmont wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;">In the unlikely event that multiple threads want to check the CPU<br>flags concurrently, simply compute them as many times.<hr> src/misc/cpu.c | 20 +++++++++++---------<br> 1 file changed, 11 insertions(+), 9 deletions(-)<br><br>diff --git a/src/misc/cpu.c b/src/misc/cpu.c<br>index 7f3a89a3cb..7053a391e7 100644<br>--- a/src/misc/cpu.c<br>+++ b/src/misc/cpu.c<br>@@ -29,6 +29,8 @@<br> # include "config.h"<br> #endif<br> <br>+#include <stdatomic.h><br>+<br> #include <vlc_common.h><br> #include <vlc_cpu.h><br> #include <vlc_memstream.h><br>@@ -247,18 +249,18 @@ out:<br>     return i_capabilities;<br> }<br> <br>-static uint32_t cpu_flags;<br>-<br>-static void vlc_CPU_init(void)<br>-{<br>-    cpu_flags = vlc_CPU_raw();<br>-}<br>+static atomic_uint cpu_flags = ATOMIC_VAR_INIT(-1);<br> <br> VLC_WEAK unsigned vlc_CPU(void)<br> {<br>-    static vlc_once_t once = VLC_STATIC_ONCE;<br>-    vlc_once(&once, vlc_CPU_init);<br>-    return cpu_flags;<br>+    unsigned flags = atomic_load_explicit(&cpu_flags, memory_order_relaxed);<br>+<br>+    if (unlikely(flags == -1U)) {<br>+        flags = vlc_CPU_raw();<br>+        atomic_store_explicit(&cpu_flags, flags, memory_order_relaxed);<br>+    }<br>+<br>+    return flags;<br></blockquote><br>I don't understand why we should need this optimization. pthread_once will very likely use an atomic to do the check.<br>Furthermore, the previous code is more readable for me.<br><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;"> }<br> <br> void vlc_CPU_dump (vlc_object_t *obj)<br>-- <br>2.26.0<hr>vlc-devel mailing list<br>To unsubscribe or modify your subscription options:<br><a href="https://mailman.videolan.org/listinfo/vlc-devel">https://mailman.videolan.org/listinfo/vlc-devel</a><br></blockquote><hr>vlc-devel mailing list<br>To unsubscribe or modify your subscription options:<br><a href="https://mailman.videolan.org/listinfo/vlc-devel">https://mailman.videolan.org/listinfo/vlc-devel</a></pre></blockquote></div><br>-- <br>Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.</body></html>