aboutsummaryrefslogtreecommitdiff
path: root/zencore/include
diff options
context:
space:
mode:
authorMartin Ridgers <[email protected]>2021-09-08 16:25:37 +0200
committerMartin Ridgers <[email protected]>2021-09-14 14:29:27 +0200
commitcb5974326e36f3fa6fe9844c3df25d7d5e328971 (patch)
treeccff82f051bf82ae1ca11191aefa134b0ec0d3e0 /zencore/include
parentMissing #include (diff)
downloadzen-cb5974326e36f3fa6fe9844c3df25d7d5e328971.tar.xz
zen-cb5974326e36f3fa6fe9844c3df25d7d5e328971.zip
std::atomic variants of MSVC's _Interlockm* calls
Diffstat (limited to 'zencore/include')
-rw-r--r--zencore/include/zencore/atomic.h29
1 files changed, 29 insertions, 0 deletions
diff --git a/zencore/include/zencore/atomic.h b/zencore/include/zencore/atomic.h
index 457128bd4..08ba53bdc 100644
--- a/zencore/include/zencore/atomic.h
+++ b/zencore/include/zencore/atomic.h
@@ -2,7 +2,12 @@
#pragma once
+#if ZEN_COMPILER_MSC
#include <intrin.h>
+#else
+#include <atomic>
+#endif
+
#include <cinttypes>
namespace zen {
@@ -10,34 +15,58 @@ namespace zen {
inline uint32_t
AtomicIncrement(volatile uint32_t& value)
{
+#if ZEN_COMPILER_MSC
return _InterlockedIncrement((long volatile*)&value);
+#else
+ return ((std::atomic<uint32_t>*)(&value))->fetch_add(1, std::memory_order_seq_cst) + 1;
+#endif
}
inline uint32_t
AtomicDecrement(volatile uint32_t& value)
{
+#if ZEN_COMPILER_MSC
return _InterlockedDecrement((long volatile*)&value);
+#else
+ return ((std::atomic<uint32_t>*)(&value))->fetch_sub(1, std::memory_order_seq_cst) - 1;
+#endif
}
inline uint64_t
AtomicIncrement(volatile uint64_t& value)
{
+#if ZEN_COMPILER_MSC
return _InterlockedIncrement64((__int64 volatile*)&value);
+#else
+ return ((std::atomic<uint64_t>*)(&value))->fetch_add(1, std::memory_order_seq_cst) + 1;
+#endif
}
inline uint64_t
AtomicDecrement(volatile uint64_t& value)
{
+#if ZEN_COMPILER_MSC
return _InterlockedDecrement64((__int64 volatile*)&value);
+#else
+ return ((std::atomic<uint64_t>*)(&value))->fetch_sub(1, std::memory_order_seq_cst) - 1;
+#endif
}
inline uint32_t
AtomicAdd(volatile uint32_t& value, uint32_t amount)
{
+#if ZEN_COMPILER_MSC
return _InterlockedExchangeAdd((long volatile*)&value, amount);
+#else
+ return ((std::atomic<uint32_t>*)(&value))->fetch_add(amount, std::memory_order_seq_cst);
+#endif
}
inline uint64_t
AtomicAdd(volatile uint64_t& value, uint64_t amount)
{
+#if ZEN_COMPILER_MSC
return _InterlockedExchangeAdd64((__int64 volatile*)&value, amount);
+#else
+ return ((std::atomic<uint64_t>*)(&value))->fetch_add(amount, std::memory_order_seq_cst);
+#endif
}
} // namespace zen