16 #ifndef _RTEMS_SCORE_CPUSTDATOMIC_H 17 #define _RTEMS_SCORE_CPUSTDATOMIC_H 32 #if defined(__cplusplus) \ 33 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) 43 #define _RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC 45 #include <stdatomic.h> 46 #define _RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC 52 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 54 typedef std::atomic_uint CPU_atomic_Uint;
56 typedef std::atomic_ulong CPU_atomic_Ulong;
58 typedef std::atomic_uintptr_t CPU_atomic_Uintptr;
60 typedef std::atomic_flag CPU_atomic_Flag;
62 typedef std::memory_order CPU_atomic_Order;
64 #define CPU_ATOMIC_ORDER_RELAXED std::memory_order_relaxed 66 #define CPU_ATOMIC_ORDER_ACQUIRE std::memory_order_acquire 68 #define CPU_ATOMIC_ORDER_RELEASE std::memory_order_release 70 #define CPU_ATOMIC_ORDER_ACQ_REL std::memory_order_acq_rel 72 #define CPU_ATOMIC_ORDER_SEQ_CST std::memory_order_seq_cst 74 #define CPU_ATOMIC_INITIALIZER_UINT( value ) ATOMIC_VAR_INIT( value ) 76 #define CPU_ATOMIC_INITIALIZER_ULONG( value ) ATOMIC_VAR_INIT( value ) 78 #define CPU_ATOMIC_INITIALIZER_UINTPTR( value ) ATOMIC_VAR_INIT( value ) 80 #define CPU_ATOMIC_INITIALIZER_FLAG ATOMIC_FLAG_INIT 82 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 84 typedef atomic_uint CPU_atomic_Uint;
86 typedef atomic_ulong CPU_atomic_Ulong;
88 typedef atomic_uintptr_t CPU_atomic_Uintptr;
90 typedef atomic_flag CPU_atomic_Flag;
92 typedef memory_order CPU_atomic_Order;
94 #define CPU_ATOMIC_ORDER_RELAXED memory_order_relaxed 96 #define CPU_ATOMIC_ORDER_ACQUIRE memory_order_acquire 98 #define CPU_ATOMIC_ORDER_RELEASE memory_order_release 100 #define CPU_ATOMIC_ORDER_ACQ_REL memory_order_acq_rel 102 #define CPU_ATOMIC_ORDER_SEQ_CST memory_order_seq_cst 104 #define CPU_ATOMIC_INITIALIZER_UINT( value ) ATOMIC_VAR_INIT( value ) 106 #define CPU_ATOMIC_INITIALIZER_ULONG( value ) ATOMIC_VAR_INIT( value ) 108 #define CPU_ATOMIC_INITIALIZER_UINTPTR( value ) ATOMIC_VAR_INIT( value ) 110 #define CPU_ATOMIC_INITIALIZER_FLAG ATOMIC_FLAG_INIT 114 typedef unsigned int CPU_atomic_Uint;
116 typedef unsigned long CPU_atomic_Ulong;
118 typedef uintptr_t CPU_atomic_Uintptr;
120 typedef bool CPU_atomic_Flag;
122 typedef int CPU_atomic_Order;
124 #define CPU_ATOMIC_ORDER_RELAXED 0 126 #define CPU_ATOMIC_ORDER_ACQUIRE 2 128 #define CPU_ATOMIC_ORDER_RELEASE 3 130 #define CPU_ATOMIC_ORDER_ACQ_REL 4 132 #define CPU_ATOMIC_ORDER_SEQ_CST 5 134 #define CPU_ATOMIC_INITIALIZER_UINT( value ) ( value ) 136 #define CPU_ATOMIC_INITIALIZER_ULONG( value ) ( value ) 138 #define CPU_ATOMIC_INITIALIZER_UINTPTR( value ) ( value ) 140 #define CPU_ATOMIC_INITIALIZER_FLAG false 151 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 152 std::atomic_thread_fence( order );
153 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 154 atomic_thread_fence( order );
169 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 170 obj->store( desired );
171 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 172 atomic_init( obj, desired );
186 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 187 obj->store( desired );
188 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 189 atomic_init( obj, desired );
203 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 204 obj->store( desired );
205 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 206 atomic_init( obj, desired );
222 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 223 return obj->load( order );
224 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 225 return atomic_load_explicit( obj, order );
247 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 248 return obj->load( order );
249 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 250 return atomic_load_explicit( obj, order );
272 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 273 return obj->load( order );
274 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 275 return atomic_load_explicit( obj, order );
296 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 297 obj->store( desired );
298 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 299 atomic_store_explicit( obj, desired, order );
316 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 317 obj->store( desired );
318 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 319 atomic_store_explicit( obj, desired, order );
336 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 337 obj->store( desired );
338 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 339 atomic_store_explicit( obj, desired, order );
358 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 359 return obj->fetch_add( arg, order );
360 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 361 return atomic_fetch_add_explicit( obj, arg, order );
387 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 388 return obj->fetch_add( arg, order );
389 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 390 return atomic_fetch_add_explicit( obj, arg, order );
416 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 417 return obj->fetch_add( arg, order );
418 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 419 return atomic_fetch_add_explicit( obj, arg, order );
445 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 446 return obj->fetch_sub( arg, order );
447 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 448 return atomic_fetch_sub_explicit( obj, arg, order );
474 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 475 return obj->fetch_sub( arg, order );
476 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 477 return atomic_fetch_sub_explicit( obj, arg, order );
503 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 504 return obj->fetch_sub( arg, order );
505 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 506 return atomic_fetch_sub_explicit( obj, arg, order );
532 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 533 return obj->fetch_or( arg, order );
534 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 535 return atomic_fetch_or_explicit( obj, arg, order );
561 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 562 return obj->fetch_or( arg, order );
563 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 564 return atomic_fetch_or_explicit( obj, arg, order );
590 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 591 return obj->fetch_or( arg, order );
592 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 593 return atomic_fetch_or_explicit( obj, arg, order );
619 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 620 return obj->fetch_and( arg, order );
621 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 622 return atomic_fetch_and_explicit( obj, arg, order );
648 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 649 return obj->fetch_and( arg, order );
650 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 651 return atomic_fetch_and_explicit( obj, arg, order );
677 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 678 return obj->fetch_and( arg, order );
679 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 680 return atomic_fetch_and_explicit( obj, arg, order );
706 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 707 return obj->exchange( desired, order );
708 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 709 return atomic_exchange_explicit( obj, desired, order );
735 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 736 return obj->exchange( desired, order );
737 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 738 return atomic_exchange_explicit( obj, desired, order );
764 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 765 return obj->exchange( desired, order );
766 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 767 return atomic_exchange_explicit( obj, desired, order );
801 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 802 return obj->compare_exchange_strong( *expected, desired, succ, fail );
803 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 804 return atomic_compare_exchange_strong_explicit( obj, expected, desired, succ, fail );
814 success = ( actual == *expected );
845 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 846 return obj->compare_exchange_strong( *expected, desired, succ, fail );
847 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 848 return atomic_compare_exchange_strong_explicit( obj, expected, desired, succ, fail );
852 unsigned long actual;
858 success = ( actual == *expected );
889 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 890 return obj->compare_exchange_strong( *expected, desired, succ, fail );
891 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 892 return atomic_compare_exchange_strong_explicit( obj, expected, desired, succ, fail );
902 success = ( actual == *expected );
922 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 924 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 925 atomic_flag_clear_explicit( obj, order );
943 #if defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_ATOMIC) 944 return obj->test_and_set( order );
945 #elif defined(_RTEMS_SCORE_CPUSTDATOMIC_USE_STDATOMIC) 946 return atomic_flag_test_and_set_explicit( obj, order );
static unsigned long _CPU_atomic_Fetch_and_ulong(CPU_atomic_Ulong *obj, unsigned long arg, CPU_atomic_Order order)
Fetches current value of Ulong and ANDs a value with the stored value.
static unsigned long _CPU_atomic_Load_ulong(const CPU_atomic_Ulong *obj, CPU_atomic_Order order)
Loads value of Ulong considering the order.
static bool _CPU_atomic_Compare_exchange_uint(CPU_atomic_Uint *obj, unsigned int *expected, unsigned int desired, CPU_atomic_Order succ, CPU_atomic_Order fail)
Checks if value of Uint is as expected.
#define _ISR_Local_disable(_level)
Disables interrupts on this processor.
#define RTEMS_COMPILER_MEMORY_BARRIER()
This macro forbids the compiler to reorder read and write commands around it.
static unsigned int _CPU_atomic_Load_uint(const CPU_atomic_Uint *obj, CPU_atomic_Order order)
Loads value of Uint considering the order.
static unsigned long _CPU_atomic_Fetch_sub_ulong(CPU_atomic_Ulong *obj, unsigned long arg, CPU_atomic_Order order)
Fetches current value of Ulong and subtracts a value from the stored value.
static bool _CPU_atomic_Flag_test_and_set(CPU_atomic_Flag *obj, CPU_atomic_Order order)
Returns current flag state and sets it.
static void _CPU_atomic_Fence(CPU_atomic_Order order)
Sets up a cpu fence.
static void _CPU_atomic_Store_uint(CPU_atomic_Uint *obj, unsigned int desired, CPU_atomic_Order order)
Stores a value to Uint considering the order.
static unsigned int _CPU_atomic_Fetch_add_uint(CPU_atomic_Uint *obj, unsigned int arg, CPU_atomic_Order order)
Fetches current value of Uint and adds a value to the stored value.
static void _CPU_atomic_Store_uintptr(CPU_atomic_Uintptr *obj, uintptr_t desired, CPU_atomic_Order order)
Stores a value to Uintptr considering the order.
static unsigned int _CPU_atomic_Fetch_sub_uint(CPU_atomic_Uint *obj, unsigned int arg, CPU_atomic_Order order)
Fetches current value of Uint and subtracts a value from the stored value.
static uintptr_t _CPU_atomic_Fetch_or_uintptr(CPU_atomic_Uintptr *obj, uintptr_t arg, CPU_atomic_Order order)
Fetches current value of Uintptr and ORs a value with the stored value.
static void _CPU_atomic_Init_uintptr(CPU_atomic_Uintptr *obj, uintptr_t desired)
Initializes Uintptr.
static uintptr_t _CPU_atomic_Fetch_add_uintptr(CPU_atomic_Uintptr *obj, uintptr_t arg, CPU_atomic_Order order)
Fetches current value of Uintptr and adds a value to the stored value.
static uintptr_t _CPU_atomic_Fetch_sub_uintptr(CPU_atomic_Uintptr *obj, uintptr_t arg, CPU_atomic_Order order)
Fetches current value of Uintptr and subtracts a value from the stored value.
static unsigned long _CPU_atomic_Fetch_add_ulong(CPU_atomic_Ulong *obj, unsigned long arg, CPU_atomic_Order order)
Fetches current value of Ulong and adds a value to the stored value.
static unsigned long _CPU_atomic_Exchange_ulong(CPU_atomic_Ulong *obj, unsigned long desired, CPU_atomic_Order order)
Fetches current value of Ulong and sets its value.
static unsigned int _CPU_atomic_Fetch_or_uint(CPU_atomic_Uint *obj, unsigned int arg, CPU_atomic_Order order)
Fetches current value of Uint and ORs a value with the stored value.
static unsigned int _CPU_atomic_Exchange_uint(CPU_atomic_Uint *obj, unsigned int desired, CPU_atomic_Order order)
Fetches current value of Uint and sets its value.
#define _ISR_Local_enable(_level)
Enables interrupts on this processor.
static bool _CPU_atomic_Compare_exchange_uintptr(CPU_atomic_Uintptr *obj, uintptr_t *expected, uintptr_t desired, CPU_atomic_Order succ, CPU_atomic_Order fail)
Checks if value of Uintptr is as expected.
static unsigned int _CPU_atomic_Fetch_and_uint(CPU_atomic_Uint *obj, unsigned int arg, CPU_atomic_Order order)
Fetches current value of Uint and ANDs a value with the stored value.
static unsigned long _CPU_atomic_Fetch_or_ulong(CPU_atomic_Ulong *obj, unsigned long arg, CPU_atomic_Order order)
Fetches current value of Ulong and ORs a value with the stored value.
static uintptr_t _CPU_atomic_Exchange_uintptr(CPU_atomic_Uintptr *obj, uintptr_t desired, CPU_atomic_Order order)
Fetches current value of Uintptr and sets its value.
static void _CPU_atomic_Store_ulong(CPU_atomic_Ulong *obj, unsigned long desired, CPU_atomic_Order order)
Stores a value to Ulong considering the order.
static bool _CPU_atomic_Compare_exchange_ulong(CPU_atomic_Ulong *obj, unsigned long *expected, unsigned long desired, CPU_atomic_Order succ, CPU_atomic_Order fail)
Checks if value of Ulong is as expected.
static uintptr_t _CPU_atomic_Fetch_and_uintptr(CPU_atomic_Uintptr *obj, uintptr_t arg, CPU_atomic_Order order)
Fetches current value of Uintptr and ANDs a value with the stored value.
static void _CPU_atomic_Flag_clear(CPU_atomic_Flag *obj, CPU_atomic_Order order)
Clears the atomic flag.
static void _CPU_atomic_Init_ulong(CPU_atomic_Ulong *obj, unsigned long desired)
Initializes Ulong.
static uintptr_t _CPU_atomic_Load_uintptr(const CPU_atomic_Uintptr *obj, CPU_atomic_Order order)
Loads value of Uintptr considering the order.
This header file provides basic definitions used by the API and the implementation.
static void _CPU_atomic_Init_uint(CPU_atomic_Uint *obj, unsigned int desired)
Initializes Uint.