Skip to content

Commit

Permalink
Add the ability to compile without atomic support
Browse files Browse the repository at this point in the history
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 <christophe@dinechin.org>
  • Loading branch information
c3d committed Sep 4, 2022
1 parent c7bd888 commit f37893b
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions recorder_ring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -120,19 +144,16 @@ 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"
#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)

#define RECORDER_RING_MAYBE_UNUSED

#endif

#endif // RECORDER_NO_ATOMICS


// ============================================================================
Expand Down

0 comments on commit f37893b

Please sign in to comment.