]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: atomic: add a read-specific variant of __ha_cpu_relax()
authorWilly Tarreau <w@1wt.eu>
Fri, 15 Mar 2024 16:31:35 +0000 (17:31 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 25 Mar 2024 17:34:19 +0000 (17:34 +0000)
Tests on various systems show that x86 prefers not to wait at all inside
read loops while aarch64 prefers to wait a little bit. Instead of having
to stuff ifdefs around __ha_cpu_relax() inside plenty of such loops
waiting for a condition to appear, better implement a new variant that
we call __ha_cpu_relax_for_read() which honors each architecture's
preferences and is the same as __ha_cpu_relax() for other ones.

include/haproxy/atomic.h

index d64e192890fc3a3662f435c5831c76b27b498b0a..93b143c94dc17d5fc0b19d2d2ca1fcae51c1452f 100644 (file)
 #define __ha_barrier_full()         do { } while (0)
 #define __ha_compiler_barrier()     do { } while (0)
 #define __ha_cpu_relax()            ({ 1; })
+#define __ha_cpu_relax_for_read()   ({ 1; })
 
 #else /* !USE_THREAD */
 
@@ -586,6 +587,9 @@ __ha_cas_dw(void *target, void *compare, const void *set)
 /* short-lived CPU relaxation */
 #define __ha_cpu_relax() ({ asm volatile("rep;nop\n"); 1; })
 
+/* dummy relaxation: x86 prefers not to wait at all in read loops */
+#define __ha_cpu_relax_for_read() ({ 1; })
+
 #elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
 
 static __inline void
@@ -651,6 +655,9 @@ static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
 /* short-lived CPU relaxation */
 #define __ha_cpu_relax() ({ asm volatile(""); 1; })
 
+/* short wait in read loops */
+#define __ha_cpu_relax_for_read() ({ asm volatile(""); 1; })
+
 #elif defined (__aarch64__)
 
 static __inline void
@@ -697,6 +704,9 @@ __ha_barrier_atomic_full(void)
  */
 #define __ha_cpu_relax() ({ asm volatile("isb" ::: "memory"); 1; })
 
+/* aarch64 prefers to wait for real in read loops */
+#define __ha_cpu_relax_for_read() ({ asm volatile("isb" ::: "memory"); 1; })
+
 #if defined(__ARM_FEATURE_ATOMICS) && !defined(__clang__) // ARMv8.1-A atomics
 
 /* returns 0 on failure, non-zero on success */
@@ -799,6 +809,9 @@ static __inline int __ha_cas_dw(void *target, void *compare, void *set)
 /* short-lived CPU relaxation */
 #define __ha_cpu_relax() ({ asm volatile(""); 1; })
 
+/* default wait in read loops */
+#define __ha_cpu_relax_for_read() ({ asm volatile(""); 1; })
+
 #endif /* end of arch-specific barrier/dwcas */
 
 static inline void __ha_compiler_barrier(void)