Browse Source

easy_lock: fix the #ifdef conditional for ia32_pause

To work better with new and old clang compilers.

Reported-by: Ryan Schmidt
Assisted-by: Joshua Root

Fixes #9058
Closes #9062
Daniel Stenberg 1 year ago
parent
commit
33fd57b8ff
1 changed files with 13 additions and 1 deletions
  1. 13 1
      lib/easy_lock.h

+ 13 - 1
lib/easy_lock.h

@@ -43,6 +43,18 @@
 #define curl_simple_lock atomic_int
 #define CURL_SIMPLE_LOCK_INIT 0
 
+/* a clang-thing */
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
+
+/* if GCC on i386/x86_64 or if the built-in is present */
+#if ( (defined(__GNUC__) && !defined(__clang__)) &&     \
+      (defined(__i386__) || defined(__x86_64__))) ||    \
+  __has_builtin(__builtin_ia32_pause)
+#define HAVE_BUILTIN_IA32_PAUSE
+#endif
+
 static inline void curl_simple_lock_lock(curl_simple_lock *lock)
 {
   for(;;) {
@@ -51,7 +63,7 @@ static inline void curl_simple_lock_lock(curl_simple_lock *lock)
     /* Reduce cache coherency traffic */
     while(atomic_load_explicit(lock, memory_order_relaxed)) {
       /* Reduce load (not mandatory) */
-#if defined(__i386__) || defined(__x86_64__)
+#ifdef HAVE_BUILTIN_IA32_PAUSE
       __builtin_ia32_pause();
 #elif defined(__aarch64__)
       __asm__ volatile("yield" ::: "memory");