From f37893b4f87fcac134a987b738eceddefa36b4ef Mon Sep 17 00:00:00 2001 From: Christophe de Dinechin Date: Sun, 4 Sep 2022 20:19:53 +0200 Subject: [PATCH] Add the ability to compile without atomic support Some very low-end platforms may not have/need atomics (single-thread, single core execution, no interrupts). In that case, the recorder can work with cheaper non-atomic operations. Signed-off-by: Christophe de Dinechin --- recorder_ring.h | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/recorder_ring.h b/recorder_ring.h index f419553..7856b40 100644 --- a/recorder_ring.h +++ b/recorder_ring.h @@ -101,12 +101,36 @@ extern "C" { #endif // __cplusplus +// ============================================================================ +// +// Compiler dependencies +// +// ============================================================================ + +#ifdef __GNUC__ +#define RECORDER_RING_MAYBE_UNUSED __attribute__((unused)) +#else // !__GNUC__ +#define RECORDER_RING_MAYBE_UNUSED +#endif // __GNUC__ + + + // ============================================================================ // // Atomic built-ins // // ============================================================================ + + +#ifdef RECORDER_NO_ATOMICS + +#define recorder_ring_fetch_add(Value, Offset) (Value += Offset) +#define recorder_ring_add_fetch(Value, Offset) ((Value += Offset), Value) +#define recorder_ring_compare_exchange(Val, Exp, New) ((Val = New), true) + +#else + #ifdef __GNUC__ // GCC-compatible compiler: use built-in atomic operations @@ -120,8 +144,6 @@ extern "C" { __atomic_compare_exchange_n(&Value, &Expected, New, \ 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED) -#define RECORDER_RING_MAYBE_UNUSED __attribute__((unused)) - #else // ! __GNUC__ #warning "Compiler not supported yet" @@ -129,10 +151,9 @@ extern "C" { #define recorder_ring_add_fetch(Value, Offset) ((Value += Offset), Value) #define recorder_ring_compare_exchange(Val, Exp, New) ((Val = New), true) -#define RECORDER_RING_MAYBE_UNUSED - #endif +#endif // RECORDER_NO_ATOMICS // ============================================================================