xref: /netbsd-src/external/bsd/libc++/dist/libcxxrt/src/atomic.h (revision d0b6b5d51b97caaa2da64e931b8ef552f455854b)
1ccec91a1Sjoerg 
2ccec91a1Sjoerg #ifndef __has_builtin
3ccec91a1Sjoerg #define __has_builtin(x) 0
4ccec91a1Sjoerg #endif
5ccec91a1Sjoerg #ifndef __has_feature
6ccec91a1Sjoerg #define __has_feature(x) 0
7ccec91a1Sjoerg #endif
8ccec91a1Sjoerg /**
9ccec91a1Sjoerg  * Swap macro that enforces a happens-before relationship with a corresponding
10ccec91a1Sjoerg  * ATOMIC_LOAD.
11ccec91a1Sjoerg  */
12ccec91a1Sjoerg #if __has_builtin(__c11_atomic_exchange)
13ccec91a1Sjoerg #define ATOMIC_SWAP(addr, val)\
14*d0b6b5d5Sjoerg 	__c11_atomic_exchange(reinterpret_cast<_Atomic(__typeof__(val))*>(addr), val, __ATOMIC_ACQ_REL)
15ccec91a1Sjoerg #elif __has_builtin(__sync_swap)
16ccec91a1Sjoerg #define ATOMIC_SWAP(addr, val)\
17ccec91a1Sjoerg 	__sync_swap(addr, val)
18ccec91a1Sjoerg #else
19ccec91a1Sjoerg #define ATOMIC_SWAP(addr, val)\
20ccec91a1Sjoerg 	__sync_lock_test_and_set(addr, val)
21ccec91a1Sjoerg #endif
22ccec91a1Sjoerg 
23ccec91a1Sjoerg #if __has_builtin(__c11_atomic_load)
24ccec91a1Sjoerg #define ATOMIC_LOAD(addr)\
25*d0b6b5d5Sjoerg 	__c11_atomic_load(reinterpret_cast<_Atomic(__typeof__(*addr))*>(addr), __ATOMIC_ACQUIRE)
26ccec91a1Sjoerg #else
27ccec91a1Sjoerg #define ATOMIC_LOAD(addr)\
28ccec91a1Sjoerg 	(__sync_synchronize(), *addr)
29ccec91a1Sjoerg #endif
305067d178Sjoerg 
31