os2: implement vlc_once()
authorKO Myung-Hun <komh78@gmail.com>
Thu, 10 May 2018 12:41:46 +0000 (21:41 +0900)
committerThomas Guillem <thomas@gllm.fr>
Fri, 18 May 2018 09:27:36 +0000 (11:27 +0200)
Signed-off-by: Thomas Guillem <thomas@gllm.fr>
include/vlc_threads.h
src/os2/thread.c

index 508ddbc..b0891ac 100644 (file)
@@ -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;
 
index 42dba0a..e4afb5b 100644 (file)
@@ -429,6 +429,34 @@ 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))
+{
+    unsigned done;
+
+    /* load once->done */
+    __atomic_xchg( &done, once->done );
+
+    /* not initialized ? */
+    if( done == 0 )
+    {
+        vlc_mutex_lock( &once->mutex );
+
+        /* load once->done */
+        __atomic_xchg( &done, once->done );
+
+        /* still not initialized ? */
+        if( done == 0 )
+        {
+            cb();
+
+            /* set once->done to 1 */
+            __atomic_xchg( &once->done, 1 );
+        }
+
+        vlc_mutex_unlock( &once->mutex );
+    }
+}
+
 /*** Thread-specific variables (TLS) ***/
 struct vlc_threadvar
 {