5 #ifndef ICE_UTIL_ATOMIC_H
6 #define ICE_UTIL_ATOMIC_H
8 #if ((defined(ICE_CPP11_COMPILER) && defined(_MSC_VER) && (_MSC_VER > 1600)) || \
9 (defined(ICE_CPP11_COMPILER) && !defined(_MSC_VER)))
10 # define ICE_CPP11_COMPILER_HAS_ATOMIC
13 #if defined(ICE_CPP11_COMPILER_HAS_ATOMIC)
15 #elif defined(ICE_USE_MUTEX_SHARED)
23 #elif ((defined(__GNUC__) && (((__GNUC__* 100) + __GNUC_MINOR__) >= 401)) || __clang__) && \
24 ((defined(__sun) && (defined(__sparc) || defined(__sparcv9))) || \
25 defined(__APPLE__) || \
26 (defined(__linux__) && \
27 (defined(__i486) || defined(__i586) || \
28 defined(__i686) || defined(__x86_64))))
30 # define ICE_HAS_GCC_BUILTINS
39 namespace IceUtilInternal
42 #ifdef ICE_CPP11_COMPILER_HAS_ATOMIC
43 typedef std::atomic<int> Atomic;
51 # if defined(__MINGW32__) || (defined(_MSC_VER) && (_MSC_VER <= 1500))
52 typedef volatile LONG ATOMIC_T;
54 typedef unsigned int ATOMIC_T;
78 inline int fetch_add(
int value)
81 return InterlockedExchangeAdd(&_ref, value);
82 #elif defined(ICE_HAS_GCC_BUILTINS)
83 return __sync_fetch_and_add(&_ref, value);
92 inline int fetch_sub(
int value)
95 # if defined(__MINGW32__) || (defined(_MSC_VER) && (_MSC_VER <= 1500))
96 return InterlockedExchangeAdd(&_ref, -value);
98 return InterlockedExchangeSubtract(&_ref, value);
100 #elif defined(ICE_HAS_GCC_BUILTINS)
101 return __sync_fetch_and_sub(&_ref, value);
110 inline int load()
const
113 return InterlockedExchangeAdd(
const_cast<ATOMIC_T*
>(&_ref), 0);
114 #elif defined(ICE_HAS_GCC_BUILTINS)
115 return __sync_fetch_and_add(
const_cast<ATOMIC_T*
>(&_ref), 0);
122 inline int exchange(
int value)
125 return InterlockedExchange(&_ref, value);
126 #elif defined(ICE_HAS_GCC_BUILTINS)
127 __sync_synchronize();
128 return __sync_lock_test_and_set(&_ref, value);
137 inline int operator++()
139 return fetch_add(1) + 1;
142 inline int operator--()
144 return fetch_sub(1) - 1;
147 inline int operator++(
int)
152 inline int operator--(
int)
157 inline operator int()
162 inline operator int()
const
170 #if !defined(_WIN32) && !defined(ICE_HAS_GCC_BUILTINS)