[vlc-devel] [PATCH 3/3] thread: optimize the one-time initializer

RĂ©mi Denis-Courmont remi at remlab.net
Thu Apr 2 16:28:38 CEST 2020


---
 include/vlc_threads.h | 7 +++++++
 src/misc/threads.c    | 4 +++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 87d0691402..98ee823197 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -642,6 +642,13 @@ typedef struct
  * \param cb callback to execute (the first time)
  */
 VLC_API void vlc_once(vlc_once_t *restrict once, void (*cb)(void));
+
+static inline void vlc_once_inline(vlc_once_t *restrict once, void (*cb)(void))
+{
+    if (unlikely(atomic_load_explicit(&once->value, memory_order_acquire) < 3))
+        vlc_once(once, cb);
+}
+#define vlc_once(once, cb) vlc_once_inline(once, cb)
 #endif
 
 /**
diff --git a/src/misc/threads.c b/src/misc/threads.c
index 945b9568f6..e26a98f7cd 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -536,7 +536,9 @@ int vlc_sem_timedwait(vlc_sem_t *sem, vlc_tick_t deadline)
 
 enum { VLC_ONCE_UNDONE, VLC_ONCE_DOING, VLC_ONCE_CONTEND, VLC_ONCE_DONE };
 
-void vlc_once(vlc_once_t *restrict once, void (*cb)(void))
+static_assert (VLC_ONCE_DONE == 3, "Check vlc_once in header file");
+
+void (vlc_once)(vlc_once_t *restrict once, void (*cb)(void))
 {
     unsigned int value = VLC_ONCE_UNDONE;
 
-- 
2.26.0



More information about the vlc-devel mailing list