From: Willy Tarreau Date: Tue, 6 Apr 2021 12:04:22 +0000 (+0200) Subject: CLEANUP: atomic: use the __atomic variant of BTS/BTR on modern compilers X-Git-Tag: v2.4-dev16~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fa68d2641bda7ffd01b66e253a049fdee94e9ccd;p=thirdparty%2Fhaproxy.git 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). --- diff --git a/include/haproxy/atomic.h b/include/haproxy/atomic.h index 722e918fb1..bb99e8db3e 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 */