22 #include <boost/utility.hpp>
24 #include "mongo/platform/windows_basic.h"
27 #pragma intrinsic(_InterlockedCompareExchange64)
34 template <
typename T,
typename _IsTSupported =
void>
35 class AtomicIntrinsics {
45 class AtomicIntrinsics<T, typename boost::enable_if_c<sizeof(T) == sizeof(LONG)>::type> {
47 static T compareAndSwap(
volatile T* dest, T expected, T newValue) {
48 return InterlockedCompareExchange(
49 reinterpret_cast<volatile LONG*>(dest), LONG(newValue), LONG(expected));
52 static T swap(
volatile T* dest, T newValue) {
53 return InterlockedExchange(reinterpret_cast<volatile LONG*>(dest), LONG(newValue));
56 static T load(
volatile const T* value) {
63 static T loadRelaxed(
volatile const T* value) {
67 static void store(
volatile T* dest, T newValue) {
73 static T fetchAndAdd(
volatile T* dest, T increment) {
74 return InterlockedExchangeAdd(reinterpret_cast<volatile LONG*>(dest), LONG(increment));
85 template <
typename T,
bool HaveInterlocked64Ops>
91 static T compareAndSwap(
volatile T* dest, T expected, T newValue) {
92 return InterlockedCompareExchange64(
93 reinterpret_cast<volatile LONGLONG*>(dest), LONGLONG(newValue), LONGLONG(expected));
96 static T swap(
volatile T* dest, T newValue) {
97 return InterlockedExchange64(reinterpret_cast<volatile LONGLONG*>(dest),
101 static T fetchAndAdd(
volatile T* dest, T increment) {
102 return InterlockedExchangeAdd64(reinterpret_cast<volatile LONGLONG*>(dest),
103 LONGLONG(increment));
109 template <
typename T>
111 static T compareAndSwap(
volatile T* dest, T expected, T newValue) {
114 return _InterlockedCompareExchange64(
115 reinterpret_cast<volatile LONGLONG*>(dest), LONGLONG(newValue), LONGLONG(expected));
118 static T swap(
volatile T* dest, T newValue) {
126 T currentValue = *dest;
128 const T result = compareAndSwap(dest, currentValue, newValue);
129 if (result == currentValue)
131 currentValue = result;
135 static T fetchAndAdd(
volatile T* dest, T increment) {
137 T currentValue = *dest;
139 const T incremented = currentValue + increment;
140 const T result = compareAndSwap(dest, currentValue, incremented);
141 if (result == currentValue)
143 currentValue = result;
152 template <
typename U,
typename _IsTTooBig =
void>
156 template <
typename U>
157 struct LoadStoreImpl<U, typename boost::enable_if_c<sizeof(U) <= sizeof(void*)>::type> {
158 static U load(
volatile const U* value) {
166 static void store(
volatile U* dest, U newValue) {
174 template <
typename U>
175 struct LoadStoreImpl<U, typename boost::disable_if_c<sizeof(U) <= sizeof(void*)>::type> {
178 static U load(
volatile const U* value);
179 static void store(
volatile U* dest, U newValue);
187 template <
typename T>
188 class AtomicIntrinsics<T, typename boost::enable_if_c<sizeof(T) == sizeof(LONGLONG)>::type> {
190 #if defined(NTDDI_VERSION) && defined(NTDDI_WS03SP2) && (NTDDI_VERSION >= NTDDI_WS03SP2)
191 static const bool kHaveInterlocked64 =
true;
193 static const bool kHaveInterlocked64 =
false;
199 static T compareAndSwap(
volatile T* dest, T expected, T newValue) {
200 return InterlockedImpl::compareAndSwap(dest, expected, newValue);
203 static T swap(
volatile T* dest, T newValue) {
204 return InterlockedImpl::swap(dest, newValue);
207 static T load(
volatile const T* value) {
208 return LoadStoreImpl::load(value);
211 static void store(
volatile T* dest, T newValue) {
212 LoadStoreImpl::store(dest, newValue);
215 static T fetchAndAdd(
volatile T* dest, T increment) {
216 return InterlockedImpl::fetchAndAdd(dest, increment);
226 template <
typename U>
227 U LoadStoreImpl<U, typename boost::disable_if_c<sizeof(U) <= sizeof(void*)>::type>::load(
228 volatile const U* value) {
232 template <
typename U>
233 void LoadStoreImpl<U, typename boost::disable_if_c<sizeof(U) <= sizeof(void*)>::type>::store(
234 volatile U* dest, U newValue) {
235 AtomicIntrinsics<U>::swap(dest, newValue);
Definition: atomic_intrinsics_win32.h:153
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Definition: atomic_intrinsics_win32.h:86
Instantiation of AtomicIntrinsics<> for all word types T.
Definition: atomic_intrinsics_gcc_atomic.h:29