33 #ifndef itkAtomicIntDetail_h
34 #define itkAtomicIntDetail_h
43 #if defined(ITK_HAVE_SYNC_BUILTINS)
44 # define ITK_GCC_ATOMICS_32
45 # define ITK_GCC_ATOMICS_64
46 #elif defined(__APPLE__)
47 # include <libkern/OSAtomic.h>
48 # define ITK_APPLE_ATOMICS_32
49 # if ITK_SIZEOF_VOID_P == 8 || defined(__i386__)
50 # define ITK_APPLE_ATOMICS_64
52 #elif defined(_WIN32) && defined(_MSC_VER)
53 # define ITK_WINDOWS_ATOMICS_32
54 # if ITK_SIZEOF_VOID_P == 8
55 # define ITK_WINDOWS_ATOMICS_64
68 #if defined ITK_HAVE_SYNC_BUILTINS
70 template <
size_t VSize>
struct BaseType;
75 typedef typename BaseType<VSize>::Type AtomicType;
76 typedef typename BaseType<VSize>::Type ValueType;
78 static ValueType AddAndFetch(ValueType *ref, ValueType val)
80 return __sync_add_and_fetch(ref, val);
83 static ValueType SubAndFetch(ValueType *ref, ValueType val)
85 return __sync_sub_and_fetch(ref, val);
88 static ValueType PreIncrement(ValueType *ref)
90 return __sync_add_and_fetch(ref, 1);
93 static ValueType PreDecrement(ValueType *ref)
95 return __sync_sub_and_fetch(ref, 1);
98 static ValueType PostIncrement(ValueType *ref)
100 return __sync_fetch_and_add(ref, 1);
103 static ValueType PostDecrement(ValueType *ref)
105 return __sync_fetch_and_sub(ref, 1);
108 static ValueType Load(
const ValueType *ref)
110 __sync_synchronize();
111 return *
static_cast<const volatile ValueType *
>(ref);
114 static void Store(ValueType *ref, ValueType val)
116 *
static_cast<volatile ValueType*
>(ref) = val;
117 __sync_synchronize();
121 #endif // defined ITK_HAVE_SYNC_BUILTINS
123 #if defined(ITK_GCC_ATOMICS_64)
124 template<>
struct BaseType<8>
126 itkAlignedTypedef( 8,
int64_t, Type );
129 #elif defined(ITK_APPLE_ATOMICS_64)
130 template <>
class AtomicOps<8>
133 itkAlignedTypedef( 8,
int64_t, AtomicType );
138 return OSAtomicAdd64Barrier(val, ref);
143 return OSAtomicAdd64Barrier(-val, ref);
148 return OSAtomicIncrement64Barrier(ref);
153 return OSAtomicDecrement64Barrier(ref);
158 int64_t val = OSAtomicIncrement64Barrier(ref);
164 int64_t val = OSAtomicDecrement64Barrier(ref);
171 return *
static_cast<const volatile int64_t*
>(ref);
176 *
static_cast<volatile int64_t*
>(ref) = val;
186 #if defined(ITK_WINDOWS_ATOMICS_64)
187 itkAlignedTypedef( 8,
int64_t, AtomicType );
190 struct ITKCommon_EXPORT AtomicType
203 static int64_t PreIncrement(AtomicType *ref);
204 static int64_t PreDecrement(AtomicType *ref);
205 static int64_t PostIncrement(AtomicType *ref);
206 static int64_t PostDecrement(AtomicType *ref);
207 static int64_t Load(
const AtomicType *ref);
208 static void Store(AtomicType *ref,
int64_t val);
213 #if defined(ITK_GCC_ATOMICS_32)
214 template<>
struct BaseType<4>
216 itkAlignedTypedef( 4,
int32_t, Type );
219 #elif defined(ITK_APPLE_ATOMICS_32)
220 template <>
class AtomicOps<4>
223 itkAlignedTypedef( 4,
int32_t, AtomicType );
228 return OSAtomicAdd32Barrier(val, ref);
233 return OSAtomicAdd32Barrier(-val, ref);
238 return OSAtomicIncrement32Barrier(ref);
243 return OSAtomicDecrement32Barrier(ref);
248 int32_t val = OSAtomicIncrement32Barrier(ref);
254 int32_t val = OSAtomicDecrement32Barrier(ref);
261 return *
static_cast<const volatile int32_t*
>(ref);
266 *
static_cast<volatile int32_t*
>(ref) = val;
276 #if defined(ITK_WINDOWS_ATOMICS_32)
277 itkAlignedTypedef( 4,
int32_t, AtomicType );
279 struct ITKCommon_EXPORT AtomicType
292 static int32_t PreIncrement(AtomicType *ref);
293 static int32_t PreDecrement(AtomicType *ref);
294 static int32_t PostIncrement(AtomicType *ref);
295 static int32_t PostDecrement(AtomicType *ref);
296 static int32_t Load(
const AtomicType *ref);
297 static void Store(AtomicType *ref,
int32_t val);
302 template <
typename T>
Critical section locking class that can be allocated on the stack.
typedef::itksysFundamentalType_Int64 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
typedef::itksysFundamentalType_Int32 int32_t
Concept::Detail::UniqueType_bool< true > TrueT
SimpleFastMutexLock * mutex
Concept::Detail::UniqueType_bool< NumericTraits< T >::is_integer > IntegralT
itkConceptConstraintsMacro()