[vlc-commits] thread: fix compilation with clang 7 or older

Marvin Scholz git at videolan.org
Mon Mar 2 17:16:40 CET 2020


vlc | branch: master | Marvin Scholz <epirat07 at gmail.com> | Mon Mar  2 10:27:10 2020 +0100| [db4e0a65faeeae359e3e79f20996699378934e70] | committer: Steve Lhomme

thread: fix compilation with clang 7 or older

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

Signed-off-by: Steve Lhomme <robux4 at ycbcr.xyz>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=db4e0a65faeeae359e3e79f20996699378934e70
---

 src/misc/threads.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/misc/threads.c b/src/misc/threads.c
index ea78e37453..5a63e6a3ce 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -135,11 +135,23 @@ 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).
+     * Apple Clang is not checked as it uses a different versioning and
+     * oldest supported Xcode/Apple Clang version is not affected.
+     */
+    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);
 }
 



More information about the vlc-commits mailing list