diff options
-rw-r--r-- | include/Atomic.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/include/Atomic.h b/include/Atomic.h new file mode 100644 index 0000000..0f3e8d3 --- /dev/null +++ b/include/Atomic.h @@ -0,0 +1,67 @@ +#ifndef __ATOMIC_H__ +#define __ATOMIC_H__ + +namespace Atomic { + +#if (__GNUC__ >= 5) || ((__GNUC__ == 4) && ((__GNUC_MINOR__ >= 1))) +// gcc version of the atomic operations +template <class T> T Or(volatile T * ptr, T mask) { __sync_or_and_fetch(ptr, mask); } +template <class T> T And(volatile T * ptr, T mask) { __sync_and_and_fetch(ptr, mask); } +template <class T> T Xor(volatile T * ptr, T mask) { __sync_xor_and_fetch(ptr, mask); } +template <class T> T Nand(volatile T * ptr, T mask) { __sync_nand_and_fetch(ptr, mask); } +template <class T> T Increment(volatile T * ptr, T delta = 1) { __sync_add_and_fetch(ptr, delta); } +template <class T> T Decrement(volatile T * ptr, T delta = 1) { __sync_sub_and_fetch(ptr, delta); } + +namespace Prefetch { +template <class T> T Or(volatile T * ptr, T mask) { __sync_fetch_and_or(ptr, mask); } +template <class T> T And(volatile T * ptr, T mask) { __sync_fetch_and_and(ptr, mask); } +template <class T> T Xor(volatile T * ptr, T mask) { __sync_fetch_and_xor(ptr, mask); } +template <class T> T Nand(volatile T * ptr, T mask) { __sync_fetch_and_nand(ptr, mask); } +template <class T> T Increment(volatile T * ptr, T delta = 1) { __sync_fetch_and_add(ptr, delta); } +template <class T> T Decrement(volatile T * ptr, T delta = 1) { __sync_fetch_and_sub(ptr, delta); } +}; + +template <class T> T Exchange32(volatile T * ptr, T exchange) { +#if defined(i386) || defined (__x86_64) + __asm__ __volatile__("lock xchgl %0, (%1)" : "+r"(exchange) : "r"(ptr)); + return exchange; +#else + T p; + do { p = *ptr; } while (!__sync_bool_compare_and_swap(ptr, p, exchange)); + return p; +#endif +} + +template <class T> T Exchange64(volatile T * ptr, T exchange) { +#if defined(i386) || defined (__x86_64) + __asm__ __volatile__("lock xchgq %0, (%1)" : "+r"(exchange) : "r"(ptr)); + return exchange; +#else + T p; + do { p = *ptr; } while (!__sync_bool_compare_and_swap(ptr, p, exchange)); + return p; +#endif +} + +#else +#ifdef _MSVC +// Visual Studio version of the atomic operations + +#error Not yet implemented... and probably never will -.- + +#else +#error No known platform for atomic operations. +#endif +#endif + +template <class T> T * ExchangePtr(T * volatile * ptr, T * exchange) { +#if defined (__x86_64) + return Exchange64(ptr, exchange); +#else + return Exchange32(ptr, exchange); +#endif +} + +}; + +#endif |