From 367a586bf75f1c5288480f6ac67f1523d1ebab86 Mon Sep 17 00:00:00 2001 From: q66 Date: Fri, 1 May 2015 20:59:47 +0100 Subject: [PATCH] start demacrofying msvc implementation --- octa/atomic.h | 86 ++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/octa/atomic.h b/octa/atomic.h index ab906d3..355aa4c 100644 --- a/octa/atomic.h +++ b/octa/atomic.h @@ -88,10 +88,12 @@ namespace octa { #define ATOMIC_POINTER_LOCK_FREE 2 static inline void __octa_atomic_thread_fence(MemoryOrder ord) { - if (ord > memory_order_consume) _ReadWriteBarrier(); - if (ord == memory_order_seq_cst) { - MemoryBarrier(); - if (ord > memory_order_consume) _ReadWriteBarrier(); + if (ord > memory_order_consume) { + _ReadWriteBarrier(); + if (ord == memory_order_seq_cst) { + MemoryBarrier(); + _ReadWriteBarrier(); + } } } @@ -103,52 +105,60 @@ namespace octa { return size <= sizeof(void *); } -#define __OCTA_MSVC_ATOMIC_STORE_N(n, dst, src, ord) \ - if (ord == memory_order_seq_cst) { \ - InterlockedExchange##n((volatile int##n##_t *)(dst), \ - (int##n##_t)(src)); \ - } else { \ - *(dst) = (src); \ - } + template + struct __OctaMsvcAtomicStore; -#define __OCTA_MSVC_ATOMIC_STORE_32(dst, src, ord) \ - if (ord == memory_order_seq_cst) { \ - InterlockedExchange((volatile int32_t *)(dst), (int32_t)(src)); \ - } else { \ - *(dst) = (src); \ - } + template + struct __OctaMsvcAtomicStore { + static inline void store(volatile T *dst, T src, MemoryOrder ord) + noexcept { + if (ord == memory_order_seq_cst) + InterlockedExchange8((volatile int8_t *)dst, (int8_t)src); + else *dst = src; + } + }; -#define __OCTA_MSVC_ATOMIC_STORE_64(dst, src, ord) \ - if (ord == memory_order_relaxed) { \ - InterlockedExchangeNoFence64((volatile int64_t *)(dst), \ - (int64_t)(src)); \ - } else { \ - InterlockedExchange64((volatile int64_t *)(dst), (int64_t)(src)); \ - } + template + struct __OctaMsvcAtomicStore { + static inline void store(volatile T *dst, T src, MemoryOrder ord) + noexcept { + if (ord == memory_order_seq_cst) + InterlockedExchange16((volatile int16_t *)dst, (int16_t)src); + else *dst = src; + } + }; -#define __OCTA_MSVC_ATOMIC_STORE(dst, src, ord) \ - if (sizeof(*dst) == 1) { \ - __OCTA_MSVC_ATOMIC_STORE_N(8, dst, src, ord); \ - } else if (sizeof(*dst) == 2) { \ - __OCTA_MSVC_ATOMIC_STORE_N(16, dst, src, ord); \ - } else if (sizeof(*dst) == 4) { \ - __OCTA_MSVC_ATOMIC_STORE_32(32, dst, src, ord); \ - } else if (sizeof(*dst) == 8) { \ - __OCTA_MSVC_ATOMIC_STORE_64(64, dst, src, ord); \ - } else { \ - abort(); \ - } + template + struct __OctaMsvcAtomicStore { + static inline void store(volatile T *dst, T src, MemoryOrder ord) + noexcept { + if (ord == memory_order_seq_cst) + InterlockedExchange((volatile int32_t *)dst, (int32_t)src); + else *dst = src; + } + }; + + template + struct __OctaMsvcAtomicStore { + static inline void store(volatile T *dst, T src, MemoryOrder ord) + noexcept { + if (ord == memory_order_relaxed) + InterlockedExchangeNoFence64((volatile int64_t *)dst, (int64_t)src); + else + InterlockedExchange64((volatile int64_t *)dst, (int64_t)src); + } + }; template static inline void __octa_atomic_store(volatile __OctaAtomicBase *a, T v, MemoryOrder ord) { - __OCTA_MSVC_ATOMIC_STORE(&a->value, v, __octa_to_gcc_order(ord)); + __OctaMsvcAtomicStore::store(&a->value, v, ord); } template static inline void __octa_atomic_store(__OctaAtomicBase *a, T v, MemoryOrder ord) { - __OCTA_MSVC_ATOMIC_STORE(&a->value, v, __octa_to_gcc_order(ord)); + __OctaMsvcAtomicStore::store(&a->value, v, ord); } #undef __OCTA_MSVC_ATOMIC_STORE_N