From fa68d2641bda7ffd01b66e253a049fdee94e9ccd Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 6 Apr 2021 14:04:22 +0200 Subject: [PATCH] CLEANUP: atomic: use the __atomic variant of BTS/BTR on modern compilers Probably due to the result of an old copy-paste, HA_ATOMIC_BTS/BTR were still implemented using the __sync_* builtins instead of the more modern __atomic_* which allow to specify the memory model. Let's update this to use the newer there and also implement the relaxed variants (which are not used for now). --- include/haproxy/atomic.h | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/include/haproxy/atomic.h b/include/haproxy/atomic.h index 722e918fb..bb99e8db3 100644 --- a/include/haproxy/atomic.h +++ b/include/haproxy/atomic.h @@ -330,13 +330,13 @@ #define HA_ATOMIC_BTS(val, bit) \ ({ \ typeof(*(val)) __b_bts = (1UL << (bit)); \ - __sync_fetch_and_or((val), __b_bts) & __b_bts; \ + __atomic_fetch_or((val), __b_bts, __ATOMIC_SEQ_CST) & __b_bts; \ }) #define HA_ATOMIC_BTR(val, bit) \ ({ \ typeof(*(val)) __b_btr = (1UL << (bit)); \ - __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \ + __atomic_fetch_and((val), ~__b_btr, __ATOMIC_SEQ_CST) & __b_btr; \ }) #define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) @@ -394,6 +394,18 @@ #define _HA_ATOMIC_FETCH_ADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED) #define _HA_ATOMIC_FETCH_SUB(val, i) __atomic_fetch_sub(val, i, __ATOMIC_RELAXED) +#define _HA_ATOMIC_BTS(val, bit) \ + ({ \ + typeof(*(val)) __b_bts = (1UL << (bit)); \ + __atomic_fetch_or((val), __b_bts, __ATOMIC_RELAXED) & __b_bts; \ + }) + +#define _HA_ATOMIC_BTR(val, bit) \ + ({ \ + typeof(*(val)) __b_btr = (1UL << (bit)); \ + __atomic_fetch_and((val), ~__b_btr, __ATOMIC_RELAXED) & __b_btr; \ + }) + #define _HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED) /* warning, n is a pointer to the double value for dwcas */ #define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n) @@ -689,6 +701,14 @@ static inline void __ha_compiler_barrier(void) /* fallbacks to remap all undefined _HA_ATOMIC_* on to their safe equivalent */ +#ifndef _HA_ATOMIC_BTR +#define _HA_ATOMIC_BTR HA_ATOMIC_BTR +#endif /* !_HA_ATOMIC_BTR */ + +#ifndef _HA_ATOMIC_BTS +#define _HA_ATOMIC_BTS HA_ATOMIC_BTS +#endif /* !_HA_ATOMIC_BTS */ + #ifndef _HA_ATOMIC_CAS #define _HA_ATOMIC_CAS HA_ATOMIC_CAS #endif /* !_HA_ATOMIC_CAS */