Use a different set of macros and types for atomics when compiled in C++

Previously, the `atomics.h` header relied solely on the presence of
    the C11 specific header `stdatomic.h`. Now, when compiled in C++,
    we use the `std::atomic` type family provided by the STL in C++11.
This commit is contained in:
Raphaël Londeix 2015-12-05 13:21:32 +00:00
parent ff1af0e4cb
commit af98891b4f

View file

@ -11,6 +11,29 @@
// Simple wrappers around atomic values so that the compiler will catch it if // Simple wrappers around atomic values so that the compiler will catch it if
// I accidentally use operators such as +, -, += on them. // I accidentally use operators such as +, -, += on them.
#ifdef __cplusplus
#include <atomic>
struct SoundIoAtomicLong {
std::atomic<long> x;
};
struct SoundIoAtomicInt {
std::atomic<int> x;
};
struct SoundIoAtomicBool {
std::atomic<bool> x;
};
#define SOUNDIO_ATOMIC_LOAD(a) (a.x.load())
#define SOUNDIO_ATOMIC_FETCH_ADD(a, delta) (a.x.fetch_add(delta))
#define SOUNDIO_ATOMIC_STORE(a, value) (a.x.store(value))
#define SOUNDIO_ATOMIC_EXCHANGE(a, value) (a.x.exchange(value))
#else
#include <stdatomic.h> #include <stdatomic.h>
struct SoundIoAtomicLong { struct SoundIoAtomicLong {
@ -31,3 +54,5 @@ struct SoundIoAtomicBool {
#define SOUNDIO_ATOMIC_EXCHANGE(a, value) atomic_exchange(&a.x, value) #define SOUNDIO_ATOMIC_EXCHANGE(a, value) atomic_exchange(&a.x, value)
#endif #endif
#endif