[vlc-devel] [PATCH v2] thread: fix compilation with clang 7 or older

Marvin Scholz epirat07 at gmail.com
Mon Mar 2 08:53:45 CET 2020


C11's atomic_load_explicit did not allow a const qualified first
argument, even though it never modifies it (see DR459).
Clang versions 7 or older did implement this quite strictly and
therefore errors when compiling this code:

  address argument to atomic operation must be a pointer to non-const
  _Atomic type ('const _Atomic(const void *) *' invalid)

See https://reviews.llvm.org/D47618 and https://reviews.llvm.org/D47613
---
 src/misc/threads.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/misc/threads.c b/src/misc/threads.c
index ea78e37453..db90a27240 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -135,11 +135,21 @@ static _Thread_local char thread_self[1];
 
 bool vlc_mutex_held(const vlc_mutex_t *mtx)
 {
+#if defined(__clang__) && !defined(__apple_build_version__) && (__clang_major__ < 8)
+    /* The explicit cast to non-const is needed to workaround a clang
+     * error with clang 7 or lower, as atomic_load_explicit in C11 did not
+     * allow its first argument to be const-qualified (see DR459)
+     */
+    vlc_mutex_t *tmp_mtx = (vlc_mutex_t *)mtx;
+#else
+    const vlc_mutex_t *tmp_mtx = mtx;
+#endif
+
     /* This comparison is thread-safe:
      * 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(&mtx->owner,
+    return THREAD_SELF == atomic_load_explicit(&tmp_mtx->owner,
                                                memory_order_relaxed);
 }
 
-- 
2.21.1 (Apple Git-122.3)



More information about the vlc-devel mailing list