20 #if !defined(__MITSUBA_CORE_ATOMIC_H_)
21 #define __MITSUBA_CORE_ATOMIC_H_
23 #include <mitsuba/mitsuba.h>
26 #include <libkern/OSAtomic.h>
27 #elif defined(_MSC_VER)
54 return _InterlockedCompareExchangePointer(
55 reinterpret_cast<void * volatile *>(v), newValue, oldValue) == oldValue;
57 return _InterlockedCompareExchange(
58 reinterpret_cast<long volatile *>(v),
59 reinterpret_cast<long>(newValue), reinterpret_cast<long>(oldValue)) ==
reinterpret_cast<long>(oldValue);
62 #if !defined(__clang__) && !defined(__INTEL_COMPILER)
63 return __sync_bool_compare_and_swap(v, oldValue, newValue);
66 #if __SIZEOF_POINTER__ == 8 || defined(__LP64__)
67 return __sync_bool_compare_and_swap(
68 reinterpret_cast<long long volatile *>(v), reinterpret_cast<long long>(oldValue),
69 reinterpret_cast<long long>(newValue));
71 return __sync_bool_compare_and_swap(
72 reinterpret_cast<long volatile *>(v), reinterpret_cast<long>(oldValue),
73 reinterpret_cast<long>(newValue));
91 return _InterlockedCompareExchange(
92 reinterpret_cast<volatile long *>(v), newValue, oldValue) == oldValue;
94 return __sync_bool_compare_and_swap(v, oldValue, newValue);
109 #if defined(_MSC_VER)
110 return _InterlockedCompareExchange64(
111 reinterpret_cast<volatile __int64 *>(v), newValue, oldValue) == oldValue;
113 return __sync_bool_compare_and_swap(v, oldValue, newValue);
122 inline float atomicAdd(
volatile float *dst,
float delta) {
124 union bits {
float f; int32_t i; };
129 #if (defined(__i386__) || defined(__amd64__))
130 __asm__ __volatile__ (
"pause\n");
133 newVal.f = oldVal.f + delta;
143 inline double atomicAdd(
volatile double *dst,
double delta) {
145 union bits {
double f; int64_t i; };
150 #if (defined(__i386__) || defined(__amd64__))
151 __asm__ __volatile__ (
"pause\n");
154 newVal.f = oldVal.f + delta;
165 inline int32_t
atomicAdd(
volatile int32_t *dst, int32_t delta) {
166 #if defined(_MSC_VER)
167 return _InterlockedExchangeAdd(reinterpret_cast<volatile long *>(dst), delta) + delta;
169 return __sync_add_and_fetch(dst, delta);
179 inline int64_t
atomicAdd(
volatile int64_t *dst, int64_t delta) {
180 #if defined(_MSC_VER)
182 return _InterlockedExchangeAdd64(reinterpret_cast<volatile __int64 *>(dst), delta) + delta;
184 SLog(
EError,
"atomicAdd() cannot handle 64-bit integers on WIN32");
188 return __sync_add_and_fetch(dst, delta);
201 if (value <= current)
203 #if (defined(__i386__) || defined(__amd64__))
204 __asm__ __volatile__ (
"pause\n");
220 if (value <= current)
222 #if (defined(__i386__) || defined(__amd32__))
223 __asm__ __volatile__ (
"pause\n");
int64_t atomicAdd(volatile int64_t *dst, int64_t delta)
Atomically add delta to the 64-bit integer destination dst.
Definition: atomic.h:179
bool atomicCompareAndExchange(volatile int64_t *v, int64_t newValue, int64_t oldValue)
Atomically attempt to exchange a 64-bit integer with another value.
Definition: atomic.h:108
#define SLog(level, fmt,...)
Write a Log message to the console (static version - to be used outside of classes that derive from O...
Definition: logger.h:49
Error message, causes an exception to be thrown.
Definition: formatter.h:33
int32_t atomicMaximum(volatile int32_t *dst, int32_t value)
Definition: atomic.h:216
bool atomicCompareAndExchangePtr(T **v, T *newValue, T *oldValue)
Atomically attempt to exchange a pointer with another value.
Definition: atomic.h:51