[vlc-commits] [Git][videolan/vlc][master] 2 commits: posix: always provide a thread ID

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Sat Oct 9 13:22:57 UTC 2021



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


Commits:
377a1c01 by Rémi Denis-Courmont at 2021-10-09T13:01:53+00:00
posix: always provide a thread ID

- - - - -
eae968d8 by Rémi Denis-Courmont at 2021-10-09T13:01:53+00:00
threads: use thread ID for mutex owner

- - - - -


3 changed files:

- include/vlc_threads.h
- src/misc/threads.c
- src/posix/thread.c


Changes:

=====================================
include/vlc_threads.h
=====================================
@@ -226,13 +226,13 @@ typedef struct
         struct {
             atomic_uint value;
             atomic_uint recursion;
-            _Atomic (const void *) owner;
+            atomic_ulong owner;
         };
 #endif
         struct {
             unsigned int value;
             unsigned int recursion;
-            const void *owner;
+            unsigned long owner;
         } dummy;
     };
 } vlc_mutex_t;
@@ -246,7 +246,7 @@ typedef struct
 #define VLC_STATIC_MUTEX { \
     .value = ATOMIC_VAR_INIT(0), \
     .recursion = ATOMIC_VAR_INIT(0), \
-    .owner = ATOMIC_VAR_INIT(NULL), \
+    .owner = ATOMIC_VAR_INIT(0), \
 }
 
 /**
@@ -734,19 +734,23 @@ VLC_API void vlc_control_cancel(vlc_cleanup_t *);
 /**
  * Thread identifier.
  *
- * This function returns the identifier of the calling thread. The identifier
- * cannot change for the entire duration of the thread, and no other thread can
- * have the same identifier at the same time in the same process. Typically,
- * the identifier is also unique across all running threads of all existing
- * processes, but that depends on the operating system.
+ * This function returns a non-zero unique identifier of the calling thread.
+ * The identifier cannot change for the entire lifetime of the thread, and two
+ * concurrent threads cannot have the same identifier.
  *
- * There are no particular semantics to the thread ID with LibVLC.
+ * The thread identifier has no defined semantics other than uniqueness,
+ * and no particular purposes within LibVLC.
  * It is provided mainly for tracing and debugging.
  *
- * \warning This function is not currently implemented on all supported
- * platforms. Where not implemented, it returns (unsigned long)-1.
+ * On some but not all supported platforms, the thread identifier is in fact
+ * the OS/kernel thread identifier (a.k.a. task PID), and is temporally unique
+ * not only within the process but across the entire system.
  *
- * \return the thread identifier (or -1 if unimplemented)
+ * \note
+ * The `main()` thread identifier is typically identical to the process
+ * identifier, but this is not portable.
+ *
+ * \return the thread identifier (cannot fail)
  */
 VLC_API unsigned long vlc_thread_id(void) VLC_USED;
 


=====================================
src/misc/threads.c
=====================================
@@ -67,7 +67,7 @@ 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->owner, NULL);
+    atomic_init(&mtx->owner, 0);
 }
 
 void vlc_mutex_init(vlc_mutex_t *mtx)
@@ -80,9 +80,6 @@ void vlc_mutex_init_recursive(vlc_mutex_t *mtx)
     vlc_mutex_init_common(mtx, true);
 }
 
-static _Thread_local char thread_self[1];
-#define THREAD_SELF ((const void *)thread_self)
-
 bool vlc_mutex_held(const vlc_mutex_t *mtx)
 {
 #if defined(__clang__) && !defined(__apple_build_version__) && (__clang_major__ < 8)
@@ -101,8 +98,8 @@ bool vlc_mutex_held(const vlc_mutex_t *mtx)
      * Even though other threads may modify the owner field at any time,
      * they will never make it compare equal to the calling thread.
      */
-    return THREAD_SELF == atomic_load_explicit(&tmp_mtx->owner,
-                                               memory_order_relaxed);
+    return vlc_thread_id() == atomic_load_explicit(&tmp_mtx->owner,
+                                                   memory_order_relaxed);
 }
 
 void vlc_mutex_lock(vlc_mutex_t *mtx)
@@ -122,7 +119,7 @@ void vlc_mutex_lock(vlc_mutex_t *mtx)
         vlc_atomic_wait(&mtx->value, 2);
 
     vlc_restorecancel(canc);
-    atomic_store_explicit(&mtx->owner, THREAD_SELF, memory_order_relaxed);
+    atomic_store_explicit(&mtx->owner, vlc_thread_id(), memory_order_relaxed);
 }
 
 int vlc_mutex_trylock(vlc_mutex_t *mtx)
@@ -149,7 +146,8 @@ int vlc_mutex_trylock(vlc_mutex_t *mtx)
     if (atomic_compare_exchange_strong_explicit(&mtx->value, &value, 1,
                                                 memory_order_acquire,
                                                 memory_order_relaxed)) {
-        atomic_store_explicit(&mtx->owner, THREAD_SELF, memory_order_relaxed);
+        atomic_store_explicit(&mtx->owner, vlc_thread_id(),
+                              memory_order_relaxed);
         return 0;
     }
 
@@ -169,7 +167,7 @@ void vlc_mutex_unlock(vlc_mutex_t *mtx)
         return;
     }
 
-    atomic_store_explicit(&mtx->owner, NULL, memory_order_relaxed);
+    atomic_store_explicit(&mtx->owner, 0, memory_order_relaxed);
 
     switch (atomic_exchange_explicit(&mtx->value, 0, memory_order_release)) {
         case 2:


=====================================
src/posix/thread.c
=====================================
@@ -36,6 +36,7 @@
 #include <stdnoreturn.h>
 #include <signal.h>
 #include <errno.h>
+#include <limits.h>
 #include <time.h>
 #include <assert.h>
 
@@ -199,7 +200,10 @@ void vlc_join(vlc_thread_t th, void **result)
 
 VLC_WEAK unsigned long vlc_thread_id(void)
 {
-     return -1;
+     static thread_local unsigned char dummy;
+
+     static_assert (UINTPTR_MAX <= ULONG_MAX, "Type size mismatch");
+     return (uintptr_t)(void *)&dummy;
 }
 
 int vlc_set_priority (vlc_thread_t th, int priority)



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/a781992ff132389dcf5feeb7dee602fd4b2d3510...eae968d889280f38798cd9cf3a40aa3f0a0d3984

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




More information about the vlc-commits mailing list