33 #ifndef itkAtomicIntDetail_h
34 #define itkAtomicIntDetail_h
42 #if defined(__APPLE__)
43 # include <libkern/OSAtomic.h>
44 # define ITK_APPLE_ATOMICS_32
45 # if ITK_SIZEOF_VOID_P == 8 || defined(__i386__)
46 # define ITK_APPLE_ATOMICS_64
48 #elif defined(_WIN32) && defined(_MSC_VER)
49 # define ITK_WINDOWS_ATOMICS_32
50 # if ITK_SIZEOF_VOID_P == 8
51 # define ITK_WINDOWS_ATOMICS_64
53 #elif defined(ITK_HAVE_SYNC_BUILTINS)
54 # define ITK_GCC_ATOMICS_32
55 # define ITK_GCC_ATOMICS_64
67 #if defined ITK_HAVE_SYNC_BUILTINS
69 template <
size_t VSize>
struct BaseType;
74 typedef typename BaseType<VSize>::Type AtomicType;
75 typedef typename BaseType<VSize>::Type ValueType;
77 static ValueType AddAndFetch(ValueType *ref, ValueType val)
79 return __sync_add_and_fetch(ref, val);
82 static ValueType SubAndFetch(ValueType *ref, ValueType val)
84 return __sync_sub_and_fetch(ref, val);
87 static ValueType PreIncrement(ValueType *ref)
89 return __sync_add_and_fetch(ref, 1);
92 static ValueType PreDecrement(ValueType *ref)
94 return __sync_sub_and_fetch(ref, 1);
97 static ValueType PostIncrement(ValueType *ref)
99 return __sync_fetch_and_add(ref, 1);
102 static ValueType PostDecrement(ValueType *ref)
104 return __sync_fetch_and_sub(ref, 1);
107 static ValueType Load(
const ValueType *ref)
109 __sync_synchronize();
110 return *
static_cast<const volatile ValueType *
>(ref);
113 static void Store(ValueType *ref, ValueType val)
115 *
static_cast<volatile ValueType*
>(ref) = val;
116 __sync_synchronize();
120 #endif // defined ITK_HAVE_SYNC_BUILTINS
122 #if defined(ITK_GCC_ATOMICS_64)
123 template<>
struct BaseType<8>
125 itkAlignedTypedef( 8,
int64_t, Type );
128 #elif defined(ITK_APPLE_ATOMICS_64)
129 template <>
class AtomicOps<8>
132 itkAlignedTypedef( 8,
int64_t, AtomicType );
137 return OSAtomicAdd64Barrier(val, ref);
142 return OSAtomicAdd64Barrier(-val, ref);
147 return OSAtomicIncrement64Barrier(ref);
152 return OSAtomicDecrement64Barrier(ref);
157 int64_t val = OSAtomicIncrement64Barrier(ref);
163 int64_t val = OSAtomicDecrement64Barrier(ref);
170 return *
static_cast<const volatile int64_t*
>(ref);
175 *
static_cast<volatile int64_t*
>(ref) = val;
185 #if defined(ITK_WINDOWS_ATOMICS_64)
186 itkAlignedTypedef( 8,
int64_t, AtomicType );
189 struct ITKCommon_EXPORT AtomicType
202 static int64_t PreIncrement(AtomicType *ref);
203 static int64_t PreDecrement(AtomicType *ref);
204 static int64_t PostIncrement(AtomicType *ref);
205 static int64_t PostDecrement(AtomicType *ref);
206 static int64_t Load(
const AtomicType *ref);
207 static void Store(AtomicType *ref,
int64_t val);
212 #if defined(ITK_GCC_ATOMICS_32)
213 template<>
struct BaseType<4>
215 itkAlignedTypedef( 4,
int32_t, Type );
218 #elif defined(ITK_APPLE_ATOMICS_32)
219 template <>
class AtomicOps<4>
222 itkAlignedTypedef( 4,
int32_t, AtomicType );
227 return OSAtomicAdd32Barrier(val, ref);
232 return OSAtomicAdd32Barrier(-val, ref);
237 return OSAtomicIncrement32Barrier(ref);
242 return OSAtomicDecrement32Barrier(ref);
247 int32_t val = OSAtomicIncrement32Barrier(ref);
253 int32_t val = OSAtomicDecrement32Barrier(ref);
260 return *
static_cast<const volatile int32_t*
>(ref);
265 *
static_cast<volatile int32_t*
>(ref) = val;
275 #if defined(ITK_WINDOWS_ATOMICS_32)
276 itkAlignedTypedef( 4,
int32_t, AtomicType );
278 struct ITKCommon_EXPORT AtomicType
291 static int32_t PreIncrement(AtomicType *ref);
292 static int32_t PreDecrement(AtomicType *ref);
293 static int32_t PostIncrement(AtomicType *ref);
294 static int32_t PostDecrement(AtomicType *ref);
295 static int32_t Load(
const AtomicType *ref);
296 static void Store(AtomicType *ref,
int32_t val);
301 template <
typename T>
Critical section locking class that can be allocated on the stack.
KWIML_INT_int64_t int64_t
IsAtomicSupportedIntegralType Self
SimpleFastMutexLock * mutex
void IgnoreUnusedVariable(T)
Concept::Detail::UniqueType_bool< sizeof(T)==4||sizeof(T)==8 > SizeT
Concept::Detail::UniqueType_bool< NumericTraits< T >::is_specialized > SpecializedT
Concept::Detail::UniqueType_bool< true > TrueT
KWIML_INT_int32_t int32_t
SimpleFastMutexLock * mutex
Concept::Detail::UniqueType_bool< NumericTraits< T >::is_integer > IntegralT
itkConceptConstraintsMacro()