[vlc-commits] [Git][videolan/vlc][master] 3 commits: threads: forbid condition wait with recursively locked mutex

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Thu Apr 7 13:12:20 UTC 2022



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
173822d5 by Steve Lhomme at 2022-04-07T12:57:50+00:00
threads: forbid condition wait with recursively locked mutex

It is currently not implemented.

- - - - -
172f3746 by Steve Lhomme at 2022-04-07T12:57:50+00:00
threads: avoid dual vlc_mutex_held call on non-recursevely locked mutex

- - - - -
828ee7e6 by Steve Lhomme at 2022-04-07T12:57:50+00:00
threads: avoid storing a boolean value in a counter

- - - - -


1 changed file:

- src/misc/threads.c


Changes:

=====================================
src/misc/threads.c
=====================================
@@ -66,7 +66,7 @@ void vlc_global_mutex (unsigned n, bool acquire)
 static void vlc_mutex_init_common(vlc_mutex_t *mtx, bool recursive)
 {
     atomic_init(&mtx->value, 0);
-    atomic_init(&mtx->recursion, recursive);
+    atomic_init(&mtx->recursion, recursive ? 1 : 0);
     atomic_init(&mtx->owner, 0);
 }
 
@@ -131,13 +131,15 @@ int vlc_mutex_trylock(vlc_mutex_t *mtx)
      */
     unsigned recursion = atomic_load_explicit(&mtx->recursion,
                                               memory_order_relaxed);
-    if (unlikely(recursion) && vlc_mutex_held(mtx)) {
-        /* This thread already owns the mutex, locks recursively.
-         * Other threads shall not have modified the recursion or owner fields.
-         */
-        atomic_store_explicit(&mtx->recursion, recursion + 1,
-                              memory_order_relaxed);
-        return 0;
+    if (unlikely(recursion)) {
+        if (vlc_mutex_held(mtx)) {
+            /* This thread already owns the mutex, locks recursively.
+             * Other threads shall not have modified the recursion or owner fields.
+             */
+            atomic_store_explicit(&mtx->recursion, recursion + 1,
+                                  memory_order_relaxed);
+            return 0;
+        }
     } else
         assert(!vlc_mutex_held(mtx));
 
@@ -289,6 +291,9 @@ void vlc_cond_wait(vlc_cond_t *cond, vlc_mutex_t *mutex)
 {
     struct vlc_cond_waiter waiter;
 
+    // wait on a multiply locked mutex not supported
+    assert(atomic_load_explicit(&mutex->recursion, memory_order_relaxed) <= 1);
+
     vlc_cond_wait_prepare(&waiter, cond, mutex);
     vlc_atomic_wait(&waiter.value, 0);
     vlc_cond_wait_finish(&waiter, cond, mutex);
@@ -300,6 +305,9 @@ int vlc_cond_timedwait(vlc_cond_t *cond, vlc_mutex_t *mutex,
     struct vlc_cond_waiter waiter;
     int ret;
 
+    // wait on a multiply locked mutex not supported
+    assert(atomic_load_explicit(&mutex->recursion, memory_order_relaxed) <= 1);
+
     vlc_cond_wait_prepare(&waiter, cond, mutex);
     ret = vlc_atomic_timedwait(&waiter.value, 0, deadline);
     vlc_cond_wait_finish(&waiter, cond, mutex);



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/caf66c9129b7e56b8fb4e96697e49652c591e1bb...828ee7e601eed001470a94d99717ab1527106dcb

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/caf66c9129b7e56b8fb4e96697e49652c591e1bb...828ee7e601eed001470a94d99717ab1527106dcb
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list