[vlc-commits] thread: optimize the one-time initializer

Rémi Denis-Courmont git at videolan.org
Thu Apr 2 18:14:37 CEST 2020


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Mon Feb 24 21:50:12 2020 +0200| [d76fb9cf3980e305c06777dbbdbbdc11b54d84a5] | committer: Rémi Denis-Courmont

thread: optimize the one-time initializer

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

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

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 87d0691402..657d53ab4e 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -642,6 +642,14 @@ 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))
+{
+    /* Fast path: check if already initialized */
+    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;
 



More information about the vlc-commits mailing list