Commit 7e66602c authored by Mark Andrews's avatar Mark Andrews

back port atomic and / or support

parent c712f406
Pipeline #26935 passed with stages
in 26 minutes and 15 seconds
......@@ -41,6 +41,8 @@
#endif
#endif
#define ATOMIC_VAR_INIT(x) x
#ifndef __ATOMIC_RELAXED
#define __ATOMIC_RELAXED 0
#endif
......@@ -78,6 +80,8 @@ typedef int_fast64_t atomic_int_fast64_t;
typedef uint_fast64_t atomic_uint_fast64_t;
typedef bool atomic_bool;
typedef uint_fast64_t atomic_uintptr_t;
#if defined(__CLANG_ATOMICS) /* __c11_atomic builtins */
#define atomic_init(obj, desired) \
__c11_atomic_init(obj, desired)
......@@ -89,10 +93,16 @@ typedef bool atomic_bool;
__c11_atomic_fetch_add(obj, arg, order)
#define atomic_fetch_sub_explicit(obj, arg, order) \
__c11_atomic_fetch_sub(obj, arg, order)
#define atomic_fetch_and_explicit(obj, arg, order) \
__c11_atomic_fetch_and(obj, arg, order)
#define atomic_fetch_or_explicit(obj, arg, order) \
__c11_atomic_fetch_or(obj, arg, order)
#define atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail) \
__c11_atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail)
#define atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail) \
__c11_atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail)
#define atomic_exchange_explicit(obj, desired, order) \
__c11_atomic_exchange_explicit(obj, expected, order)
#elif defined(__GNUC_ATOMICS) /* __atomic builtins */
#define atomic_init(obj, desired) \
(*obj = desired)
......@@ -104,10 +114,16 @@ typedef bool atomic_bool;
__atomic_fetch_add(obj, arg, order)
#define atomic_fetch_sub_explicit(obj, arg, order) \
__atomic_fetch_sub(obj, arg, order)
#define atomic_fetch_and_explicit(obj, arg, order) \
__atomic_fetch_and(obj, arg, order)
#define atomic_fetch_or_explicit(obj, arg, order) \
__atomic_fetch_or(obj, arg, order)
#define atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail) \
__atomic_compare_exchange_n(obj, expected, desired, 0, succ, fail)
#define atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail) \
__atomic_compare_exchange_n(obj, expected, desired, 1, succ, fail)
#define atomic_exchange_explicit(obj, desired, order) \
__atomic_exchange_n(obj, desired, order)
#else /* __sync builtins */
#define atomic_init(obj, desired) \
(*obj = desired)
......@@ -119,10 +135,14 @@ typedef bool atomic_bool;
*obj = desired; \
__sync_synchronize(); \
} while (0);
#define atomic_fetch_add_explicit(obj, arg, order) \
#define atomic_fetch_add_explicit(obj, arg, order) \
__sync_fetch_and_add(obj, arg)
#define atomic_fetch_sub_explicit(obj, arg, order) \
#define atomic_fetch_sub_explicit(obj, arg, order) \
__sync_fetch_and_sub(obj, arg, order)
#define atomic_fetch_and_explicit(obj, arg, order) \
__sync_fetch_and_and(obj, arg, order)
#define atomic_fetch_or_explicit(obj, arg, order) \
__sync_fetch_and_or(obj, arg, order)
#define atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail) \
({ \
__typeof__(obj) __v; \
......@@ -136,6 +156,9 @@ typedef bool atomic_bool;
})
#define atomic_compare_exchange_weak_explicit(obj, expected, desired, succ, fail) \
atomic_compare_exchange_strong_explicit(obj, expected, desired, succ, fail)
#define atomic_exchange_explicit(obj, desired, order) \
__sync_lock_test_and_set(obj, desired)
#endif
#define atomic_load(obj) \
......@@ -146,7 +169,13 @@ typedef bool atomic_bool;
atomic_fetch_add_explicit(obj, arg, memory_order_seq_cst)
#define atomic_fetch_sub(obj, arg) \
atomic_fetch_sub_explicit(obj, arg, memory_order_seq_cst)
#define atomic_fetch_and(obj, arg) \
atomic_fetch_and_explicit(obj, arg, memory_order_seq_cst)
#define atomic_fetch_or(obj, arg) \
atomic_fetch_or_explicit(obj, arg, memory_order_seq_cst)
#define atomic_compare_exchange_strong(obj, expected, desired) \
atomic_compare_exchange_strong_explicit(obj, expected, desired, memory_order_seq_cst, memory_order_seq_cst)
#define atomic_compare_exchange_weak(obj, expected, desired) \
atomic_compare_exchange_weak_explicit(obj, expected, desired, memory_order_seq_cst, memory_order_seq_cst)
#define atomic_exchange(obj, desired) \
atomic_exchange_explicit(obj, desired, memory_order_seq_cst)
......@@ -26,6 +26,8 @@
#include <isc/util.h>
#define ATOMIC_VAR_INIT(x) x
#ifndef __ATOMIC_RELAXED
#define __ATOMIC_RELAXED 0
#endif
......@@ -213,6 +215,96 @@ atomic_add_abort() {
#define atomic_fetch_sub(obj, arg) \
atomic_fetch_sub_explicit(obj, arg, memory_order_seq_cst)
#define atomic_fetch_and_explicit8(obj, arg, order) \
InterlockedAnd8((atomic_int_fast8_t)obj, arg)
#define atomic_fetch_and_explicit32(obj, arg, order) \
(order == memory_order_relaxed \
? InterlockedAndNoFence((atomic_int_fast32_t *)obj, arg) \
: (order == memory_order_acquire \
? InterlockedAndAcquire((atomic_int_fast32_t *)obj, arg) \
: (order == memory_order_release \
? InterlockedAndRelease((atomic_int_fast32_t *)obj, arg) \
: InterlockedAnd((atomic_int_fast32_t *)obj, arg))))
#ifdef _WIN64
#define atomic_fetch_and_explicit64(obj, arg, order) \
(order == memory_order_relaxed \
? InterlockedAnd64NoFence((atomic_int_fast64_t *)obj, arg) \
: (order == memory_order_acquire \
? InterlockedAnd64Acquire((atomic_int_fast64_t *)obj, arg) \
: (order == memory_order_release \
? InterlockedAnd64Release((atomic_int_fast64_t *)obj, arg) \
: InterlockedAnd64((atomic_int_fast64_t *)obj, arg))))
#else
#define atomic_fetch_and_explicit64(obj, arg, order) \
InterlockedAnd64((atomic_int_fast64_t *)obj, arg)
#endif
static inline
int8_t
atomic_and_abort() {
INSIST(0);
ISC_UNREACHABLE();
}
#define atomic_fetch_and_explicit(obj, arg, order) \
(sizeof(*(obj)) == 8 \
? atomic_fetch_and_explicit64(obj, arg, order) \
: (sizeof(*(obj)) == 4 \
? atomic_fetch_and_explicit32(obj, arg, order) \
: (sizeof(*(obj)) == 1 \
? atomic_fetch_and_explicit8(obj, arg, order) \
: atomic_and_abort())))
#define atomic_fetch_and(obj, arg) \
atomic_fetch_and_explicit(obj, arg, memory_order_seq_cst)
#define atomic_fetch_or_explicit8(obj, arg, order) \
InterlockedOr8((atomic_int_fast8_t)obj, arg)
#define atomic_fetch_or_explicit32(obj, arg, order) \
(order == memory_order_relaxed \
? InterlockedOrNoFence((atomic_int_fast32_t *)obj, arg) \
: (order == memory_order_acquire \
? InterlockedOrAcquire((atomic_int_fast32_t *)obj, arg) \
: (order == memory_order_release \
? InterlockedOrRelease((atomic_int_fast32_t *)obj, arg) \
: InterlockedOr((atomic_int_fast32_t *)obj, arg))))
#ifdef _WIN64
#define atomic_fetch_or_explicit64(obj, arg, order) \
(order == memory_order_relaxed \
? InterlockedOr64NoFence((atomic_int_fast64_t *)obj, arg) \
: (order == memory_order_acquire \
? InterlockedOr64Acquire((atomic_int_fast64_t *)obj, arg) \
: (order == memory_order_release \
? InterlockedOr64Release((atomic_int_fast64_t *)obj, arg) \
: InterlockedOr64((atomic_int_fast64_t *)obj, arg))))
#else
#define atomic_fetch_or_explicit64(obj, arg, order) \
InterlockedOr64((atomic_int_fast64_t *)obj, arg)
#endif
static inline
int8_t
atomic_or_abort() {
INSIST(0);
ISC_UNREACHABLE();
}
#define atomic_fetch_or_explicit(obj, arg, order) \
(sizeof(*(obj)) == 8 \
? atomic_fetch_or_explicit64(obj, arg, order) \
: (sizeof(*(obj)) == 4 \
? atomic_fetch_or_explicit32(obj, arg, order) \
: (sizeof(*(obj)) == 1 \
? atomic_fetch_or_explicit8(obj, arg, order) \
: atomic_or_abort())))
#define atomic_fetch_or(obj, arg) \
atomic_fetch_or_explicit(obj, arg, memory_order_seq_cst)
static inline bool
atomic_compare_exchange_strong_explicit8(atomic_int_fast8_t *obj,
int8_t *expected,
......@@ -295,8 +387,7 @@ atomic_compare_exchange_strong_explicit64(atomic_int_fast64_t *obj,
return (__r);
}
static inline
bool
static inline bool
atomic_compare_exchange_abort() {
INSIST(0);
ISC_UNREACHABLE();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment