[vlc-devel] [PATCH v2 2/2] os2: implement vlc_once()

KO Myung-Hun komh78 at gmail.com
Tue Apr 17 13:41:42 CEST 2018


---
 include/vlc_threads.h |  6 ++++++
 src/os2/thread.c      | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 37054a3ded..b7505da266 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -125,6 +125,12 @@ typedef struct
 #define VLC_STATIC_COND { NULLHANDLE, 0, NULLHANDLE, 0 }
 #define LIBVLC_NEED_SEMAPHORE
 #define LIBVLC_NEED_RWLOCK
+typedef struct
+{
+    unsigned done;
+    vlc_mutex_t mutex;
+} vlc_once_t;
+#define VLC_STATIC_ONCE { 0, VLC_STATIC_MUTEX }
 typedef struct vlc_threadvar *vlc_threadvar_t;
 typedef struct vlc_timer *vlc_timer_t;
 
diff --git a/src/os2/thread.c b/src/os2/thread.c
index 3ecf24dc0d..9f64fcc3b3 100644
--- a/src/os2/thread.c
+++ b/src/os2/thread.c
@@ -38,6 +38,7 @@
 #include <limits.h>
 #include <errno.h>
 #include <time.h>
+#include <stdatomic.h>
 
 #include <sys/types.h>
 #ifdef HAVE_SYS_SOCKET_H
@@ -429,6 +430,23 @@ int vlc_cond_timedwait_daytime (vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex,
     return vlc_cond_wait_common (p_condvar, p_mutex, ulTimeout);
 }
 
+void vlc_once(vlc_once_t *once, void (*cb)(void))
+{
+    if( atomic_load_explicit( &once->done, memory_order_acquire ) == 0 )
+    {
+        vlc_mutex_lock( &once->mutex );
+
+        if( atomic_load_explicit( &once->done, memory_order_acquire ) == 0 )
+        {
+            cb();
+
+            atomic_store_explicit( &once->done, 1, memory_order_release );
+        }
+
+        vlc_mutex_unlock( &once->mutex );
+    }
+}
+
 /*** Thread-specific variables (TLS) ***/
 struct vlc_threadvar
 {
-- 
2.13.3



More information about the vlc-devel mailing list