base: use <stdatomic.h> for atomics, where present

C11 introduces a new set of atomic APIs in <stdatomic.h>. If available, use
those in preference to compiler- or platform-specific intrinsics.
This commit is contained in:
Luke Howard
2020-07-13 09:47:02 +10:00
parent 932605c01e
commit cbb2ceb018
2 changed files with 15 additions and 1 deletions

View File

@@ -412,6 +412,7 @@ AC_CHECK_HEADERS([\
signal.h \
strings.h \
stropts.h \
stdatomic.h \
sys/bitypes.h \
sys/category.h \
sys/file.h \

View File

@@ -42,7 +42,20 @@
* Atomic operations
*/
#if defined(__GNUC__) && defined(HAVE___SYNC_ADD_AND_FETCH)
#if defined(HAVE_STDATOMIC_H)
#include <stdatomic.h>
#define heim_base_atomic_inc(x) (atomic_fetch_add((x), 1) + 1)
#define heim_base_atomic_dec(x) (atomic_fetch_sub((x), 1) - 1)
#define heim_base_atomic_type atomic_uint
#define heim_base_atomic_max UINT_MAX
#define heim_base_exchange_pointer(t,v) atomic_exchange((t), (v))
#define heim_base_exchange_32(t,v) atomic_exchange((t), (v))
#define heim_base_exchange_64(t,v) atomic_exchange((t), (v))
#elif defined(__GNUC__) && defined(HAVE___SYNC_ADD_AND_FETCH)
#define heim_base_atomic_inc(x) __sync_add_and_fetch((x), 1)
#define heim_base_atomic_dec(x) __sync_sub_and_fetch((x), 1)