[vlc-devel] [PATCH 3/6] thread: use the common mutex implementation

RĂ©mi Denis-Courmont remi at remlab.net
Thu Feb 20 20:58:20 CET 2020


---
 include/vlc_threads.h | 42 --------------------
 src/android/thread.c  | 57 ---------------------------
 src/darwin/thread.c   | 89 ------------------------------------------
 src/misc/threads.c    |  2 -
 src/os2/thread.c      | 91 -------------------------------------------
 src/posix/thread.c    | 62 -----------------------------
 src/win32/thread.c    | 84 ---------------------------------------
 7 files changed, 427 deletions(-)

diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index deea618917..0aa1dbca05 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -63,21 +63,6 @@ typedef unsigned long vlc_osthread_t;
 #define vlc_thread_equal(a,b) ((a) == (b))
 
 # define LIBVLC_NEED_SLEEP
-typedef struct
-{
-    bool dynamic;
-    union
-    {
-        struct
-        {
-            bool locked;
-            unsigned long contention;
-        };
-        CRITICAL_SECTION mutex;
-    };
-} vlc_mutex_t;
-#define VLC_STATIC_MUTEX { false, { { false, 0 } } }
-#define LIBVLC_DONT_WANT_MUTEX
 #define LIBVLC_NEED_RWLOCK
 typedef INIT_ONCE vlc_once_t;
 #define VLC_STATIC_ONCE INIT_ONCE_STATIC_INIT
@@ -112,21 +97,6 @@ typedef struct vlc_thread *vlc_thread_t;
 typedef unsigned long vlc_osthread_t;
 #define vlc_thread_equal(a,b) ((a) == (b))
 
-typedef struct
-{
-    bool dynamic;
-    union
-    {
-        struct
-        {
-            bool locked;
-            unsigned long contention;
-        };
-        HMTX hmtx;
-    };
-} vlc_mutex_t;
-#define VLC_STATIC_MUTEX { false, { { false, 0 } } }
-#define LIBVLC_DONT_WANT_MUTEX
 #define LIBVLC_NEED_RWLOCK
 typedef struct
 {
@@ -180,9 +150,6 @@ typedef struct vlc_thread *vlc_thread_t;
 #define VLC_THREAD_CANCELED NULL
 typedef pthread_t vlc_osthread_t;
 #define vlc_thread_equal(a,b) pthread_equal(a,b)
-typedef pthread_mutex_t vlc_mutex_t;
-#define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
-#define LIBVLC_DONT_WANT_MUTEX
 typedef pthread_once_t  vlc_once_t;
 #define VLC_STATIC_ONCE   PTHREAD_ONCE_INIT
 typedef pthread_key_t   vlc_threadvar_t;
@@ -228,9 +195,6 @@ typedef pthread_t       vlc_thread_t;
 #define VLC_THREAD_CANCELED PTHREAD_CANCELED
 typedef pthread_t       vlc_osthread_t;
 #define vlc_thread_equal(a,b) pthread_equal(a,b)
-typedef pthread_mutex_t vlc_mutex_t;
-#define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
-#define LIBVLC_DONT_WANT_MUTEX
 typedef pthread_rwlock_t vlc_rwlock_t;
 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
 typedef pthread_once_t  vlc_once_t;
@@ -275,10 +239,6 @@ typedef struct
 typedef pthread_t vlc_osthread_t;
 #define vlc_thread_equal(a,b) pthread_equal(a,b)
 
-typedef pthread_mutex_t vlc_mutex_t;
-#define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
-#define LIBVLC_DONT_WANT_MUTEX
-
 /**
  * Read/write lock.
  *
@@ -335,7 +295,6 @@ typedef struct vlc_timer *vlc_timer_t;
  * \defgroup mutex Mutual exclusion locks
  * @{
  */
-#ifndef LIBVLC_DONT_WANT_MUTEX
 /**
  * Mutex.
  *
@@ -370,7 +329,6 @@ typedef struct
     .recursion = ATOMIC_VAR_INIT(0), \
     .owner = ATOMIC_VAR_INIT(NULL), \
 }
-#endif
 
 /**
  * Initializes a fast mutex.
diff --git a/src/android/thread.c b/src/android/thread.c
index fd5be51965..24eea70ae9 100644
--- a/src/android/thread.c
+++ b/src/android/thread.c
@@ -67,63 +67,6 @@ vlc_thread_fatal_print (const char *action, int error,
 # define VLC_THREAD_ASSERT( action ) ((void)val)
 #endif
 
-/* mutexes */
-void vlc_mutex_init( vlc_mutex_t *p_mutex )
-{
-    pthread_mutexattr_t attr;
-
-    pthread_mutexattr_init (&attr);
-#ifdef NDEBUG
-    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_DEFAULT);
-#else
-    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
-#endif
-    pthread_mutex_init (p_mutex, &attr);
-    pthread_mutexattr_destroy( &attr );
-}
-
-void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
-{
-    pthread_mutexattr_t attr;
-
-    pthread_mutexattr_init (&attr);
-    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
-    pthread_mutex_init (p_mutex, &attr);
-    pthread_mutexattr_destroy( &attr );
-}
-
-
-void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
-{
-    int val = pthread_mutex_destroy( p_mutex );
-    VLC_THREAD_ASSERT ("destroying mutex");
-}
-
-void vlc_mutex_lock (vlc_mutex_t *p_mutex)
-{
-    int val = pthread_mutex_lock( p_mutex );
-    VLC_THREAD_ASSERT ("locking mutex");
-    vlc_mutex_mark(p_mutex);
-}
-
-int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
-{
-    int val = pthread_mutex_trylock( p_mutex );
-
-    if (val != EBUSY) {
-        VLC_THREAD_ASSERT ("locking mutex");
-        vlc_mutex_mark(p_mutex);
-    }
-    return val;
-}
-
-void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
-{
-    int val = pthread_mutex_unlock( p_mutex );
-    VLC_THREAD_ASSERT ("unlocking mutex");
-    vlc_mutex_unmark(p_mutex);
-}
-
 void vlc_once(vlc_once_t *once, void (*cb)(void))
 {
     int val = pthread_once(once, cb);
diff --git a/src/darwin/thread.c b/src/darwin/thread.c
index eb0a25fd8c..3c796fc06e 100644
--- a/src/darwin/thread.c
+++ b/src/darwin/thread.c
@@ -115,95 +115,6 @@ vlc_thread_fatal (const char *action, int error,
 # define VLC_THREAD_ASSERT( action ) ((void)val)
 #endif
 
-/* Initializes a fast mutex. */
-void vlc_mutex_init( vlc_mutex_t *p_mutex )
-{
-    pthread_mutexattr_t attr;
-
-    if (unlikely(pthread_mutexattr_init (&attr)))
-        abort();
-#ifdef NDEBUG
-    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_DEFAULT);
-#else
-    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
-#endif
-    if (unlikely(pthread_mutex_init (p_mutex, &attr)))
-        abort();
-    pthread_mutexattr_destroy( &attr );
-}
-
-void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
-{
-    pthread_mutexattr_t attr;
-
-    if (unlikely(pthread_mutexattr_init (&attr)))
-        abort();
-    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
-    if (unlikely(pthread_mutex_init (p_mutex, &attr)))
-        abort();
-    pthread_mutexattr_destroy( &attr );
-}
-
-
-void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
-{
-    int val = pthread_mutex_destroy( p_mutex );
-    VLC_THREAD_ASSERT ("destroying mutex");
-}
-
-void vlc_mutex_lock (vlc_mutex_t *p_mutex)
-{
-    int val = pthread_mutex_lock( p_mutex );
-    VLC_THREAD_ASSERT ("locking mutex");
-    vlc_mutex_mark(p_mutex);
-}
-
-int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
-{
-    int val = pthread_mutex_trylock( p_mutex );
-
-    if (val != EBUSY) {
-        VLC_THREAD_ASSERT ("locking mutex");
-        vlc_mutex_mark(p_mutex);
-    }
-    return val;
-}
-
-void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
-{
-    int val = pthread_mutex_unlock( p_mutex );
-    /* FIXME: We can't check for the success of the unlock
-     * here as due to a bug in Apple pthread implementation.
-     * The `pthread_cond_wait` function does not behave like
-     * it should According to POSIX, pthread_cond_wait is a
-     * cancellation point and when a thread is cancelled while
-     * in a condition wait, the mutex is re-acquired before
-     * calling the first cancellation cleanup handler:
-     *
-     * > The effect is as if the thread were unblocked, allowed
-     * > to execute up to the point of returning from the call to
-     * > pthread_cond_timedwait() or pthread_cond_wait(), but at
-     * > that point notices the cancellation request and instead
-     * > of returning to the caller of pthread_cond_timedwait()
-     * > or pthread_cond_wait(), starts the thread cancellation
-     * > activities, which includes calling cancellation cleanup
-     * > handlers.
-     *
-     * Unfortunately the mutex is not locked sometimes, causing
-     * the call to `pthread_mutex_unlock` to fail.
-     * Until this is fixed, enabling this assertion would lead to
-     * spurious test failures and VLC crashes when compiling with
-     * debug enabled, which would make it nearly impossible to
-     * proeprly test with debug builds on macOS.
-     * This was reported to Apple as FB6152751.
-     */
-#ifndef NDEBUG
-    if (val != EPERM)
-        VLC_THREAD_ASSERT ("unlocking mutex");
-#endif
-    vlc_mutex_unmark(p_mutex);
-}
-
 void vlc_rwlock_init (vlc_rwlock_t *lock)
 {
     if (unlikely(pthread_rwlock_init (lock, NULL)))
diff --git a/src/misc/threads.c b/src/misc/threads.c
index 357687407d..eddf06d348 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -201,7 +201,6 @@ void (vlc_tick_sleep)(vlc_tick_t delay)
 }
 #endif
 
-#ifndef LIBVLC_DONT_WANT_MUTEX
 static void vlc_mutex_init_common(vlc_mutex_t *mtx, bool recursive)
 {
     atomic_init(&mtx->value, 0);
@@ -323,7 +322,6 @@ void vlc_mutex_unlock(vlc_mutex_t *mtx)
             vlc_assert_unreachable();
     }
 }
-#endif
 
 void vlc_cond_init(vlc_cond_t *cond)
 {
diff --git a/src/os2/thread.c b/src/os2/thread.c
index 6b04c47610..c30cdad210 100644
--- a/src/os2/thread.c
+++ b/src/os2/thread.c
@@ -170,97 +170,6 @@ unsigned long _System _DLL_InitTerm(unsigned long hmod, unsigned long flag)
     return 0;   /* Failed */
 }
 
-/*** Mutexes ***/
-void vlc_mutex_init( vlc_mutex_t *p_mutex )
-{
-    /* This creates a recursive mutex. This is OK as fast mutexes have
-     * no defined behavior in case of recursive locking. */
-    DosCreateMutexSem( NULL, &p_mutex->hmtx, 0, FALSE );
-    p_mutex->dynamic = true;
-}
-
-void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
-{
-    DosCreateMutexSem( NULL, &p_mutex->hmtx, 0, FALSE );
-    p_mutex->dynamic = true;
-}
-
-
-void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
-{
-    assert (p_mutex->dynamic);
-    DosCloseMutexSem( p_mutex->hmtx );
-}
-
-void vlc_mutex_lock (vlc_mutex_t *p_mutex)
-{
-    if (!p_mutex->dynamic)
-    {   /* static mutexes */
-        int canc = vlc_savecancel ();
-        assert (p_mutex != &super_mutex); /* this one cannot be static */
-
-        vlc_mutex_lock (&super_mutex);
-        while (p_mutex->locked)
-        {
-            p_mutex->contention++;
-            vlc_cond_wait (&super_variable, &super_mutex);
-            p_mutex->contention--;
-        }
-        p_mutex->locked = true;
-        vlc_mutex_unlock (&super_mutex);
-        vlc_restorecancel (canc);
-    }
-    else
-        DosRequestMutexSem(p_mutex->hmtx, SEM_INDEFINITE_WAIT);
-
-    vlc_mutex_mark(p_mutex);
-}
-
-int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
-{
-    int ret;
-
-    if (!p_mutex->dynamic)
-    {   /* static mutexes */
-        assert (p_mutex != &super_mutex); /* this one cannot be static */
-        vlc_mutex_lock (&super_mutex);
-        if (!p_mutex->locked)
-        {
-            p_mutex->locked = true;
-            ret = 0;
-        }
-        else
-            ret = EBUSY;
-        vlc_mutex_unlock (&super_mutex);
-    }
-    else
-        ret = DosRequestMutexSem( p_mutex->hmtx, 0 ) ? EBUSY : 0;
-
-    if (ret == 0)
-        vlc_mutex_mark(p_mutex);
-
-    return ret;
-}
-
-void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
-{
-    if (!p_mutex->dynamic)
-    {   /* static mutexes */
-        assert (p_mutex != &super_mutex); /* this one cannot be static */
-
-        vlc_mutex_lock (&super_mutex);
-        assert (p_mutex->locked);
-        p_mutex->locked = false;
-        if (p_mutex->contention)
-            vlc_cond_broadcast (&super_variable);
-        vlc_mutex_unlock (&super_mutex);
-    }
-    else
-        DosReleaseMutexSem( p_mutex->hmtx );
-
-    vlc_mutex_unmark(p_mutex);
-}
-
 void vlc_once(vlc_once_t *once, void (*cb)(void))
 {
     unsigned done;
diff --git a/src/posix/thread.c b/src/posix/thread.c
index 1756e43800..32d48b5cc2 100644
--- a/src/posix/thread.c
+++ b/src/posix/thread.c
@@ -103,68 +103,6 @@ vlc_thread_fatal (const char *action, int error,
 # define VLC_THREAD_ASSERT( action ) ((void)val)
 #endif
 
-void vlc_mutex_init( vlc_mutex_t *p_mutex )
-{
-    pthread_mutexattr_t attr;
-
-    if (unlikely(pthread_mutexattr_init (&attr)))
-        abort();
-#ifdef NDEBUG
-    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_DEFAULT);
-#else
-    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK);
-#endif
-    if (unlikely(pthread_mutex_init (p_mutex, &attr)))
-        abort();
-    pthread_mutexattr_destroy( &attr );
-}
-
-void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
-{
-    pthread_mutexattr_t attr;
-
-    if (unlikely(pthread_mutexattr_init (&attr)))
-        abort();
-    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
-    if (unlikely(pthread_mutex_init (p_mutex, &attr)))
-        abort();
-    pthread_mutexattr_destroy( &attr );
-}
-
-void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
-{
-    int val = pthread_mutex_destroy( p_mutex );
-    VLC_THREAD_ASSERT ("destroying mutex");
-}
-
-void vlc_mutex_lock(vlc_mutex_t *mutex)
-{
-    int val = pthread_mutex_lock(mutex);
-
-    VLC_THREAD_ASSERT("locking mutex");
-    vlc_mutex_mark(mutex);
-}
-
-int vlc_mutex_trylock(vlc_mutex_t *mutex)
-{
-    int val = pthread_mutex_trylock(mutex);
-
-    if (val != EBUSY) {
-        VLC_THREAD_ASSERT("locking mutex");
-        vlc_mutex_mark(mutex);
-    }
-
-    return val;
-}
-
-void vlc_mutex_unlock(vlc_mutex_t *mutex)
-{
-    int val = pthread_mutex_unlock(mutex);
-
-    VLC_THREAD_ASSERT("unlocking mutex");
-    vlc_mutex_unmark(mutex);
-}
-
 void vlc_rwlock_init (vlc_rwlock_t *lock)
 {
     if (unlikely(pthread_rwlock_init (lock, NULL)))
diff --git a/src/win32/thread.c b/src/win32/thread.c
index 933b7ee9d2..3292936321 100644
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -70,90 +70,6 @@ struct vlc_thread
     } wait;
 };
 
-/*** Mutexes ***/
-void vlc_mutex_init( vlc_mutex_t *p_mutex )
-{
-    /* This creates a recursive mutex. This is OK as fast mutexes have
-     * no defined behavior in case of recursive locking. */
-    InitializeCriticalSection (&p_mutex->mutex);
-    p_mutex->dynamic = true;
-}
-
-void vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
-{
-    InitializeCriticalSection( &p_mutex->mutex );
-    p_mutex->dynamic = true;
-}
-
-
-void vlc_mutex_destroy (vlc_mutex_t *p_mutex)
-{
-    assert (p_mutex->dynamic);
-    DeleteCriticalSection (&p_mutex->mutex);
-}
-
-void vlc_mutex_lock (vlc_mutex_t *p_mutex)
-{
-    if (!p_mutex->dynamic)
-    {   /* static mutexes */
-        EnterCriticalSection(&super_mutex);
-        while (p_mutex->locked)
-        {
-            p_mutex->contention++;
-            SleepConditionVariableCS(&super_variable, &super_mutex, INFINITE);
-            p_mutex->contention--;
-        }
-        p_mutex->locked = true;
-        LeaveCriticalSection(&super_mutex);
-    }
-    else
-        EnterCriticalSection (&p_mutex->mutex);
-
-    vlc_mutex_mark(p_mutex);
-}
-
-int vlc_mutex_trylock (vlc_mutex_t *p_mutex)
-{
-    int ret;
-
-    if (!p_mutex->dynamic)
-    {   /* static mutexes */
-        EnterCriticalSection(&super_mutex);
-        if (!p_mutex->locked)
-        {
-            p_mutex->locked = true;
-            ret = 0;
-        }
-        else
-            ret = EBUSY;
-        LeaveCriticalSection(&super_mutex);
-    }
-    else
-        ret = TryEnterCriticalSection (&p_mutex->mutex) ? 0 : EBUSY;
-
-    if (ret == 0)
-        vlc_mutex_mark(p_mutex);
-
-    return ret;
-}
-
-void vlc_mutex_unlock (vlc_mutex_t *p_mutex)
-{
-    if (!p_mutex->dynamic)
-    {   /* static mutexes */
-        EnterCriticalSection(&super_mutex);
-        assert (p_mutex->locked);
-        p_mutex->locked = false;
-        if (p_mutex->contention)
-            WakeAllConditionVariable(&super_variable);
-        LeaveCriticalSection(&super_mutex);
-    }
-    else
-        LeaveCriticalSection (&p_mutex->mutex);
-
-    vlc_mutex_unmark(p_mutex);
-}
-
 /*** One-time initialization ***/
 static BOOL CALLBACK vlc_once_callback(INIT_ONCE *once, void *parm, void **ctx)
 {
-- 
2.25.0



More information about the vlc-devel mailing list