1// This is a non-atomic fake implementation of stdatomic.h for when 2// compiler C11 stdatomic support is missing and only single threaded 3// operation is required 4 5#ifndef FAKE_STD_ATOMICS_H 6#define FAKE_STD_ATOMICS_H 7 8#include <inttypes.h> 9 10#define _Atomic volatile 11#define ATOMIC_FLAG_INIT 0 12 13typedef uint8_t atomic_bool; 14typedef uint8_t atomic_flag; 15 16#define atomic_compare_and_exchange_strong(x, y, z) return ((*(x) == *(y)) ? ((*(x) = z), true) : ((*(y) = *(x)),false)) 17 18#define atomic_fetch_add(x, y) *(x) += (y), (*(x) - (y)) 19 20static inline void atomic_flag_clear(volatile atomic_flag* flag) 21{ 22 *flag = ATOMIC_FLAG_INIT; 23} 24 25static inline atomic_bool atomic_flag_test_and_set( volatile atomic_flag* flag ) 26{ 27 atomic_bool result = *flag; 28 *flag = 1; 29 return result; 30} 31 32#define atomic_load(x) (*(x)) 33#define atomic_store(x, y) do { *(x) = (y); } while (0) 34 35 36#endif // FAKE_STD_ATOMICS_H 37