[vlc-devel] [PATCH 1/2] os2: thread: reimplement vlc_once() without mutex

KO Myung-Hun komh78 at gmail.com
Tue Mar 31 11:17:14 CEST 2020


This fixes compilation failure due to vlc_mutex_t declared later.
---
 include/vlc_threads.h |  8 ++------
 src/os2/thread.c      | 28 ++++++++++------------------
 2 files changed, 12 insertions(+), 24 deletions(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index 131e319622..91c84aa299 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -92,12 +92,8 @@ typedef struct vlc_thread *vlc_thread_t;
 #define VLC_THREAD_CANCELED NULL
 
 #define LIBVLC_NEED_RWLOCK
-typedef struct
-{
-    unsigned done;
-    vlc_mutex_t mutex;
-} vlc_once_t;
-#define VLC_STATIC_ONCE { 0, VLC_STATIC_MUTEX }
+typedef unsigned vlc_once_t;
+#define VLC_STATIC_ONCE 0
 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 531bdd101b..66932bd2a1 100644
--- a/src/os2/thread.c
+++ b/src/os2/thread.c
@@ -170,30 +170,22 @@ unsigned long _System _DLL_InitTerm(unsigned long hmod, unsigned long flag)
 
 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 )
+    if( __atomic_cmpxchg32( once, 1, 0 ) == 1 /* changed */ )
     {
-        vlc_mutex_lock( &once->mutex );
-
-        /* load once->done */
-        __atomic_xchg( &done, once->done );
+        /* initializing... */
 
-        /* still not initialized ? */
-        if( done == 0 )
-        {
-            cb();
+        cb();
 
-            /* set once->done to 1 */
-            __atomic_xchg( &once->done, 1 );
-        }
+        /* initialized! */
+        __atomic_xchg( once, 2 );
 
-        vlc_mutex_unlock( &once->mutex );
+        return;
     }
+
+    /* wait to be initialized */
+    while( __atomic_cmpxchg32( once, 2, 2 ) == 0 /* not changed */ )
+        DosSleep( 1 );  /* yield */
 }
 
 /*** Thread-specific variables (TLS) ***/
-- 
2.22.0



More information about the vlc-devel mailing list