35template <
size_t SizeOf>
41 typedef char Integral;
44 template <
class Atomic,
class Target>
45 static void read(
volatile Atomic* atomic, Target* target)
47 *(Integral*) target = *(
volatile Integral*) atomic;
52 template <
class Atomic,
class Addend>
53 static void add(
volatile Atomic* atomic, Addend* addend)
55 const Integral addendValue = *
static_cast<Integral*
>(addend);
57 *(Integral*) addend = (_InterlockedExchangeAdd8((
volatile Integral*) atomic, addendValue) + addendValue);
60 template <
class Atomic,
class Value>
61 static void store(
volatile Atomic* atomic,
const Value* value)
63 _InterlockedExchange8((
volatile Integral*) atomic, *(
const Integral*) value);
66 template <
class Atomic,
class Value>
67 static void exchange(
volatile Atomic* atomic, Value* value)
69 *(Integral*) value = _InterlockedExchange8((
volatile Integral*) atomic, *
static_cast<Integral*
>(value));
72 template <
class Atomic,
class Comparand,
class Value>
73 static bool compareExchange(
volatile Atomic* atomic, Comparand* comparand,
const Value* value)
75 const Integral comparandValue = *(Integral*) comparand;
77 const Integral previous =
78 _InterlockedCompareExchange8((
volatile Integral*) atomic, *(
const Integral*) value, comparandValue);
80 if (previous == comparandValue)
85 *(Integral*) value = previous;
94 typedef short Integral;
97 template <
class Atomic,
class Target>
98 static void read(
volatile Atomic* atomic, Target* target)
100 *(Integral*) target = *(
volatile Integral*) atomic;
105 template <
class Atomic,
class Addend>
106 static void add(
volatile Atomic* atomic, Addend* addend)
108 const Integral addendValue = *
static_cast<Integral*
>(addend);
110 *(Integral*) addend = (_InterlockedExchangeAdd16((
volatile Integral*) atomic, addendValue) + addendValue);
113 template <
class Atomic,
class Value>
114 static void store(
volatile Atomic* atomic,
const Value* value)
116 _InterlockedExchange16((
volatile Integral*) atomic, *(
const Integral*) value);
119 template <
class Atomic,
class Value>
120 static void exchange(
volatile Atomic* atomic, Value* value)
122 *(Integral*) value = _InterlockedExchange16((
volatile Integral*) atomic, *
static_cast<Integral*
>(value));
125 template <
class Atomic,
class Comparand,
class Value>
126 static bool compareExchange(
volatile Atomic* atomic, Comparand* comparand,
const Value* value)
128 const Integral comparandValue = *(Integral*) comparand;
130 const Integral previous =
131 _InterlockedCompareExchange16((
volatile Integral*) atomic, *(
const Integral*) value, comparandValue);
133 if (previous == comparandValue)
138 *(Integral*) value = previous;
147 typedef long Integral;
150 template <
class Atomic,
class Target>
151 static void read(
volatile Atomic* atomic, Target* target)
153 *(Integral*) target = *(
volatile Integral*) atomic;
158 template <
class Atomic,
class Addend>
159 static void add(
volatile Atomic* atomic, Addend* addend)
161 const Integral addendValue = *
static_cast<Integral*
>(addend);
163 *(Integral*) addend = (_InterlockedExchangeAdd((
volatile Integral*) atomic, addendValue) + addendValue);
166 template <
class Atomic,
class Value>
167 static void store(
volatile Atomic* atomic,
const Value* value)
169 _InterlockedExchange((
volatile Integral*) atomic, *(
const Integral*) value);
172 template <
class Atomic,
class Value>
173 static void exchange(
volatile Atomic* atomic, Value* value)
175 *(Integral*) value = _InterlockedExchange((
volatile Integral*) atomic, *
static_cast<Integral*
>(value));
178 template <
class Atomic,
class Comparand,
class Value>
179 static bool compareExchange(
volatile Atomic* atomic, Comparand* comparand,
const Value* value)
181 const Integral comparandValue = *(Integral*) comparand;
183 const Integral previous =
184 _InterlockedCompareExchange((
volatile Integral*) atomic, *(
const Integral*) value, comparandValue);
186 if (previous == comparandValue)
191 *(Integral*) value = previous;
200 typedef long long Integral;
203 template <
class Atomic,
class Target>
204 static void read(
volatile Atomic* atomic, Target* target)
206 *(Integral*) target = *(
volatile Integral*) atomic;
211 template <
class Atomic,
class Addend>
212 static void add(
volatile Atomic* atomic, Addend* addend)
214 const Integral addendValue = *
static_cast<Integral*
>(addend);
216 *(Integral*) addend = (_InterlockedExchangeAdd64((
volatile Integral*) atomic, addendValue) + addendValue);
219 template <
class Atomic,
class Value>
220 static void store(
volatile Atomic* atomic,
const Value* value)
222 _InterlockedExchange64((
volatile Integral*) atomic, *(
const Integral*) value);
225 template <
class Atomic,
class Value>
226 static void exchange(
volatile Atomic* atomic, Value* value)
228 *(Integral*) value = _InterlockedExchange64((
volatile Integral*) atomic, *
static_cast<Integral*
>(value));
231 template <
class Atomic,
class Comparand,
class Value>
232 static bool compareExchange(
volatile Atomic* atomic, Comparand* comparand,
const Value* value)
234 const Integral comparandValue = *(Integral*) comparand;
236 const Integral previous =
237 _InterlockedCompareExchange64((
volatile Integral*) atomic, *(
const Integral*) value, comparandValue);
239 if (previous == comparandValue)
244 *(Integral*) value = previous;
250template <
typename Type>
254 std::is_integral<Type>::value || std::is_enum<Type>::value || std::is_pointer<Type>::value,
255 "Atomic<T> requires an integral, enumeration, or pointer type"
258 static Type read(
volatile Type& atomic)
noexcept
262 AtomicOps<
sizeof(Type)>::read(&atomic, &value);
267 static void store(
volatile Type& atomic, Type value)
noexcept
269 AtomicOps<
sizeof(Type)>::store(&atomic, &value);
272 static void exchange(
volatile Type& atomic, Type& value)
noexcept
274 AtomicOps<
sizeof(Type)>::exchange(&atomic, &value);
277 static bool compareStore(
volatile Type& atomic, Type comparand, Type value)
noexcept
279 return AtomicOps<
sizeof(Type)>::compareExchange(&atomic, &comparand, &value);
282 static bool compareExchange(
volatile Type& atomic, Type comparand, Type& value)
noexcept
284 return AtomicOps<
sizeof(Type)>::compareExchange(&atomic, &comparand, &value);
288#elif defined(__GNUC__)
290template <
typename Type>
294 std::is_integral<Type>::value || std::is_enum<Type>::value || std::is_pointer<Type>::value,
295 "Atomic<T> requires an integral, enumeration, or pointer type"
298 static Type read(
volatile Type& atomic)
noexcept
302 __atomic_load(&atomic, &value, __ATOMIC_SEQ_CST);
303 __atomic_thread_fence(__ATOMIC_SEQ_CST);
308 static void store(
volatile Type& atomic, Type value)
noexcept
310 __atomic_store(&atomic, &value, __ATOMIC_SEQ_CST);
313 static void exchange(
volatile Type& atomic, Type& value)
noexcept
317 __atomic_exchange(&atomic, &value, &previous, __ATOMIC_SEQ_CST);
322 static bool compareStore(
volatile Type& atomic, Type comparand, Type value)
noexcept
324 return compareExchange(atomic, comparand, value);
327 static bool compareExchange(
volatile Type& atomic, Type comparand, Type& value)
noexcept
329 if (__atomic_compare_exchange(&atomic, &comparand, &value,
false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
340template <
typename Type, Type Value>
345 : variable_(variable)
351 Atomic<Type>::store(variable_, Value);
355 volatile Type& variable_;
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
#define ONIXS_CMEMDH_NAMESPACE_END
#define ONIXS_CMEMDH_DEPRECATED
AtomicScopedStore(volatile Type &variable)
bool value(Number &number, const MultiContainer &container, Tag tag)