#ifdef HAVE_STDATOMIC_H
#include <stdatomic.h>
+/* These are available in gcc, but not in stdatomic */
+#define atomic_add_fetch_explicit __atomic_add_fetch
+#define atomic_sub_fetch_explicit __atomic_sub_fetch
+#define atomic_and_fetch_explicit __atomic_and_fetch
+#define atomic_or_fetch_explicit __atomic_or_fetch
+
/* gcc 4.7 and newer */
#elif defined(HAVE___ATOMIC)
#define atomic_exchange_explicit __atomic_exchange_n
#define atomic_fetch_add_explicit __atomic_fetch_add
#define atomic_fetch_sub_explicit __atomic_fetch_sub
+#define atomic_fetch_and_explicit __atomic_fetch_and
+#define atomic_fetch_or_explicit __atomic_fetch_or
+
+#define atomic_add_fetch_explicit __atomic_add_fetch
+#define atomic_sub_fetch_explicit __atomic_sub_fetch
+#define atomic_and_fetch_explicit __atomic_and_fetch
+#define atomic_or_fetch_explicit __atomic_or_fetch
#define atomic_compare_exchange_weak_explicit(atom, expect, desire, mem1, \
mem2) \
ret; \
})
+#define atomic_fetch_and_explicit(ptr, val, mem) \
+ ({ \
+ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_fetch_and_and(ptr, val); \
+ __sync_synchronize(); \
+ rval; \
+ })
+#define atomic_fetch_or_explicit(ptr, val, mem) \
+ ({ \
+ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_fetch_and_or(ptr, val); \
+ __sync_synchronize(); \
+ rval; \
+ })
+
+#define atomic_add_fetch_explicit(ptr, val, mem) \
+ ({ \
+ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_add_and_fetch((ptr), (val)); \
+ __sync_synchronize(); \
+ rval; \
+ })
+#define atomic_sub_fetch_explicit(ptr, val, mem) \
+ ({ \
+ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_sub_and_fetch((ptr), (val)); \
+ __sync_synchronize(); \
+ rval; \
+ })
+
+#define atomic_and_fetch_explicit(ptr, val, mem) \
+ ({ \
+ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_and_and_fetch(ptr, val); \
+ __sync_synchronize(); \
+ rval; \
+ })
+#define atomic_or_fetch_explicit(ptr, val, mem) \
+ ({ \
+ __sync_synchronize(); \
+ typeof(*ptr) rval = __sync_or_and_fetch(ptr, val); \
+ __sync_synchronize(); \
+ rval; \
+ })
+
#else /* !HAVE___ATOMIC && !HAVE_STDATOMIC_H */
#error no atomic functions...
#endif