33template <
size_t SizeOf>
39 typedef char Integral;
42 template <
class Atomic,
class Target>
43 static void read(
volatile Atomic* atomic, Target* target)
45 *(Integral*) target = *(
volatile Integral*) atomic;
50 template <
class Atomic,
class Addend>
51 static void add(
volatile Atomic* atomic, Addend* addend)
53 const Integral addendValue = *
static_cast<Integral*
>(addend);
55 *(Integral*) addend = (_InterlockedExchangeAdd8((
volatile Integral*) atomic, addendValue) + addendValue);
58 template <
class Atomic,
class Value>
59 static void store(
volatile Atomic* atomic,
const Value* value)
61 _InterlockedExchange8((
volatile Integral*) atomic, *(
const Integral*) value);
64 template <
class Atomic,
class Value>
65 static void exchange(
volatile Atomic* atomic, Value* value)
67 *(Integral*) value = _InterlockedExchange8((
volatile Integral*) atomic, *
static_cast<Integral*
>(value));
70 template <
class Atomic,
class Comparand,
class Value>
71 static bool compareExchange(
volatile Atomic* atomic, Comparand* comparand,
const Value* value)
73 const Integral comparandValue = *(Integral*) comparand;
75 const Integral previous =
76 _InterlockedCompareExchange8((
volatile Integral*) atomic, *(
const Integral*) value, comparandValue);
78 if (previous == comparandValue)
83 *(Integral*) value = previous;
92 typedef short Integral;
95 template <
class Atomic,
class Target>
96 static void read(
volatile Atomic* atomic, Target* target)
98 *(Integral*) target = *(
volatile Integral*) atomic;
103 template <
class Atomic,
class Addend>
104 static void add(
volatile Atomic* atomic, Addend* addend)
106 const Integral addendValue = *
static_cast<Integral*
>(addend);
108 *(Integral*) addend = (_InterlockedExchangeAdd16((
volatile Integral*) atomic, addendValue) + addendValue);
111 template <
class Atomic,
class Value>
112 static void store(
volatile Atomic* atomic,
const Value* value)
114 _InterlockedExchange16((
volatile Integral*) atomic, *(
const Integral*) value);
117 template <
class Atomic,
class Value>
118 static void exchange(
volatile Atomic* atomic, Value* value)
120 *(Integral*) value = _InterlockedExchange16((
volatile Integral*) atomic, *
static_cast<Integral*
>(value));
123 template <
class Atomic,
class Comparand,
class Value>
124 static bool compareExchange(
volatile Atomic* atomic, Comparand* comparand,
const Value* value)
126 const Integral comparandValue = *(Integral*) comparand;
128 const Integral previous =
129 _InterlockedCompareExchange16((
volatile Integral*) atomic, *(
const Integral*) value, comparandValue);
131 if (previous == comparandValue)
136 *(Integral*) value = previous;
145 typedef long Integral;
148 template <
class Atomic,
class Target>
149 static void read(
volatile Atomic* atomic, Target* target)
151 *(Integral*) target = *(
volatile Integral*) atomic;
156 template <
class Atomic,
class Addend>
157 static void add(
volatile Atomic* atomic, Addend* addend)
159 const Integral addendValue = *
static_cast<Integral*
>(addend);
161 *(Integral*) addend = (_InterlockedExchangeAdd((
volatile Integral*) atomic, addendValue) + addendValue);
164 template <
class Atomic,
class Value>
165 static void store(
volatile Atomic* atomic,
const Value* value)
167 _InterlockedExchange((
volatile Integral*) atomic, *(
const Integral*) value);
170 template <
class Atomic,
class Value>
171 static void exchange(
volatile Atomic* atomic, Value* value)
173 *(Integral*) value = _InterlockedExchange((
volatile Integral*) atomic, *
static_cast<Integral*
>(value));
176 template <
class Atomic,
class Comparand,
class Value>
177 static bool compareExchange(
volatile Atomic* atomic, Comparand* comparand,
const Value* value)
179 const Integral comparandValue = *(Integral*) comparand;
181 const Integral previous =
182 _InterlockedCompareExchange((
volatile Integral*) atomic, *(
const Integral*) value, comparandValue);
184 if (previous == comparandValue)
189 *(Integral*) value = previous;
198 typedef long long Integral;
201 template <
class Atomic,
class Target>
202 static void read(
volatile Atomic* atomic, Target* target)
204 *(Integral*) target = *(
volatile Integral*) atomic;
209 template <
class Atomic,
class Addend>
210 static void add(
volatile Atomic* atomic, Addend* addend)
212 const Integral addendValue = *
static_cast<Integral*
>(addend);
214 *(Integral*) addend = (_InterlockedExchangeAdd64((
volatile Integral*) atomic, addendValue) + addendValue);
217 template <
class Atomic,
class Value>
218 static void store(
volatile Atomic* atomic,
const Value* value)
220 _InterlockedExchange64((
volatile Integral*) atomic, *(
const Integral*) value);
223 template <
class Atomic,
class Value>
224 static void exchange(
volatile Atomic* atomic, Value* value)
226 *(Integral*) value = _InterlockedExchange64((
volatile Integral*) atomic, *
static_cast<Integral*
>(value));
229 template <
class Atomic,
class Comparand,
class Value>
230 static bool compareExchange(
volatile Atomic* atomic, Comparand* comparand,
const Value* value)
232 const Integral comparandValue = *(Integral*) comparand;
234 const Integral previous =
235 _InterlockedCompareExchange64((
volatile Integral*) atomic, *(
const Integral*) value, comparandValue);
237 if (previous == comparandValue)
242 *(Integral*) value = previous;
248template <
typename Type>
251 static Type read(
volatile Type& atomic)
255 AtomicOps<
sizeof(Type)>::read(&atomic, &value);
260 static void store(
volatile Type& atomic, Type value)
262 AtomicOps<
sizeof(Type)>::store(&atomic, &value);
265 static void exchange(
volatile Type& atomic, Type& value)
267 AtomicOps<
sizeof(Type)>::exchange(&atomic, &value);
270 static bool compareStore(
volatile Type& atomic, Type comparand, Type value)
272 return AtomicOps<
sizeof(Type)>::compareExchange(&atomic, &comparand, &value);
275 static bool compareExchange(
volatile Type& atomic, Type comparand, Type& value)
277 return AtomicOps<
sizeof(Type)>::compareExchange(&atomic, &comparand, &value);
281#elif defined(__GNUC__)
283template <
typename Type>
286 static Type read(
volatile Type& atomic)
288 const Type
value = atomic;
290 __sync_synchronize();
295 static void store(
volatile Type& atomic, Type value)
297 (void) (__sync_lock_test_and_set(&atomic, value));
300 static void exchange(
volatile Type& atomic, Type& value)
302 value = __sync_lock_test_and_set(&atomic, value);
305 static bool compareStore(
volatile Type& atomic, Type comparand, Type value)
307 return __sync_bool_compare_and_swap(&atomic, comparand, value);
310 static bool compareExchange(
volatile Type& atomic, Type comparand, Type& value)
312 const Type oldValue = __sync_val_compare_and_swap(&atomic, comparand, value);
314 if (oldValue == comparand)
325template <
typename Type, Type Value>
330 : variable_(variable)
336 Atomic<Type>::store(variable_, Value);
340 volatile Type& variable_;
#define ONIXS_CMEMDH_NAMESPACE_BEGIN
#define ONIXS_CMEMDH_NAMESPACE_END
AtomicScopedStore(volatile Type &variable)
bool value(Number &number, const MultiContainer &container, Tag tag)