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

Marvin Scholz epirat07 at gmail.com
Mon Feb 24 16:45:07 CET 2020


According to the standard, C11's atomic_load_explicit did not allow
a const qualified first argument, even though it never modifies it.
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

C-standard wise this is fixed in C17, see DR459
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2244.htm#dr_459
---
 src/misc/threads.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/misc/threads.c b/src/misc/threads.c
index ea78e37453..6f5255505f 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -139,7 +139,14 @@ 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(&mtx->owner,
+
+    /* The explicit cast to non-const is needed to workaround a clang
+     * error with clang 7 or lower, as atomic_load_explicit in C11 does not
+     * allow its first argument to be const-qualified, and clang implmented
+     * this like that.
+     * This is fixed in clang version 8 or newer, and in the C17 standard.
+     */
+    return THREAD_SELF == atomic_load_explicit(&((vlc_mutex_t *)mtx)->owner,
                                                memory_order_relaxed);
 }
 
-- 
2.21.1 (Apple Git-122.3)



More information about the vlc-devel mailing list