4#include "../Foundation/PrimitiveTypes.h"
9 long _InterlockedExchangeAdd(
long volatile* Addend,
long Value);
10 char _InterlockedExchange8(
char volatile* Target,
char Value);
11 void __dmb(
unsigned int _Type);
12 void __iso_volatile_store8(
volatile __int8*, __int8);
13 __int8 __iso_volatile_load8(
const volatile __int8*);
14 __int32 __iso_volatile_load32(
const volatile __int32*);
15 void _ReadWriteBarrier(
void);
18#define SC_COMPILER_MSVC_DISABLE_DEPRECATED_WARNING \
19 _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
20#elif defined(__CUDACC__) || defined(__INTEL_COMPILER)
21#define SC_COMPILER_MSVC_DISABLE_DEPRECATED_WARNING \
22 __pragma(warning(push)) __pragma(warning(disable : 4996))
24#define SC_COMPILER_MSVC_DISABLE_DEPRECATED_WARNING \
25 _Pragma("warning(push)") _Pragma("warning(disable : 4996)")
29#define SC_COMPILER_MSVC_RESTORE_DEPRECATED_WARNING _Pragma("clang diagnostic pop")
30#elif defined(__CUDACC__) || defined(__INTEL_COMPILER)
31#define SC_COMPILER_MSVC_RESTORE_DEPRECATED_WARNING __pragma(warning(pop))
33#define SC_COMPILER_MSVC_RESTORE_DEPRECATED_WARNING _Pragma("warning(pop)")
36#define SC_COMPILER_MSVC_COMPILER_BARRIER() \
37 SC_COMPILER_MSVC_DISABLE_DEPRECATED_WARNING _ReadWriteBarrier() SC_COMPILER_MSVC_RESTORE_DEPRECATED_WARNING
39#if defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC)
40#define SC_COMPILER_MSVC_MEMORY_BARRIER() __dmb(0xB)
41#define SC_COMPILER_MSVC_COMPILER_MEMORY_BARRIER() SC_COMPILER_MSVC_MEMORY_BARRIER()
42#elif defined(_M_IX86) || defined(_M_X64)
43#define SC_COMPILER_MSVC_COMPILER_MEMORY_BARRIER() SC_COMPILER_MSVC_COMPILER_BARRIER()
45#error Unsupported hardware
48#define SC_COMPILER_MSVC_ATOMIC_LOAD_VERIFY_MEMORY_ORDER(_Order_var) \
51 case memory_order_relaxed: break; \
52 case memory_order_consume: \
53 case memory_order_acquire: \
54 case memory_order_seq_cst: SC_COMPILER_MSVC_COMPILER_MEMORY_BARRIER(); break; \
55 case memory_order_release: \
56 case memory_order_acq_rel: \
65typedef enum memory_order
75typedef enum memory_order
77 memory_order_relaxed = __ATOMIC_RELAXED,
78 memory_order_consume = __ATOMIC_CONSUME,
79 memory_order_acquire = __ATOMIC_ACQUIRE,
80 memory_order_release = __ATOMIC_RELEASE,
81 memory_order_acq_rel = __ATOMIC_ACQ_REL,
82 memory_order_seq_cst = __ATOMIC_SEQ_CST
108 res = _InterlockedExchangeAdd(
reinterpret_cast<volatile long*
>(&value), val);
111 return __atomic_fetch_add(&value, val, __ATOMIC_SEQ_CST);
119 res = __iso_volatile_load32(
reinterpret_cast<volatile const int*
>(&value));
120 SC_COMPILER_MSVC_COMPILER_MEMORY_BARRIER();
122 __atomic_load(&value, &res, __ATOMIC_SEQ_CST);
127 int32_t load(memory_order mem)
const
131 res = __iso_volatile_load32(
reinterpret_cast<volatile const int*
>(&value));
132 SC_COMPILER_MSVC_ATOMIC_LOAD_VERIFY_MEMORY_ORDER(mem);
134 __atomic_load(&value, &res, mem);
146 Atomic(
bool value) : value(value) {}
148 bool exchange(
bool desired)
151 return static_cast<bool>(_InterlockedExchange8(
reinterpret_cast<volatile char*
>(&value), desired));
154 __atomic_exchange(&value, &desired, &res, __ATOMIC_SEQ_CST);
162 char res = __iso_volatile_load8(
reinterpret_cast<volatile const char*
>(&value));
163 SC_COMPILER_MSVC_COMPILER_MEMORY_BARRIER();
164 return reinterpret_cast<bool&
>(res);
167 __atomic_load(&value, &res, __ATOMIC_SEQ_CST);
172 bool load(memory_order mem)
const
175 char res = __iso_volatile_load8(
reinterpret_cast<volatile const char*
>(&value));
176 SC_COMPILER_MSVC_ATOMIC_LOAD_VERIFY_MEMORY_ORDER(mem);
177 return reinterpret_cast<bool&
>(res);
180 __atomic_load(&value, &res, mem);
191#undef SC_COMPILER_MSVC_ATOMIC_LOAD_VERIFY_MEMORY_ORDER
192#undef SC_COMPILER_MSVC_DISABLE_DEPRECATED_WARNING
193#undef SC_COMPILER_MSVC_RESTORE_DEPRECATED_WARNING
194#undef SC_COMPILER_MSVC_COMPILER_BARRIER
195#undef SC_COMPILER_MSVC_MEMORY_BARRIER
196#undef SC_COMPILER_MSVC_COMPILER_MEMORY_BARRIER
int int32_t
Platform independent (4) bytes signed int.
Definition: PrimitiveTypes.h:46
Atomic variables (only for int and bool for now).
Definition: Atomic.h:97