|
| 1 | +// File: crn_atomics.h |
| 2 | +#ifndef CRN_ATOMICS_H |
| 3 | +#define CRN_ATOMICS_H |
| 4 | + |
| 5 | +#ifdef WIN32 |
| 6 | +#pragma once |
| 7 | +#endif |
| 8 | + |
| 9 | +#ifdef WIN32 |
| 10 | +#include "crn_winhdr.h" |
| 11 | +#endif |
| 12 | + |
| 13 | +#if defined(__GNUC__) && CRNLIB_PLATFORM_PC |
| 14 | +extern __inline__ __attribute__((__always_inline__,__gnu_inline__)) void crnlib_yield_processor() |
| 15 | +{ |
| 16 | + __asm__ __volatile__("pause"); |
| 17 | +} |
| 18 | +#else |
| 19 | +CRNLIB_FORCE_INLINE void crnlib_yield_processor() |
| 20 | +{ |
| 21 | +#if CRNLIB_USE_MSVC_INTRINSICS |
| 22 | + #if CRNLIB_PLATFORM_PC_X64 |
| 23 | + _mm_pause(); |
| 24 | + #else |
| 25 | + YieldProcessor(); |
| 26 | + #endif |
| 27 | +#else |
| 28 | + // No implementation |
| 29 | +#endif |
| 30 | +} |
| 31 | +#endif |
| 32 | + |
| 33 | +#if CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS |
| 34 | + extern "C" __int64 _InterlockedCompareExchange64(__int64 volatile * Destination, __int64 Exchange, __int64 Comperand); |
| 35 | + #if defined(_MSC_VER) |
| 36 | + #pragma intrinsic(_InterlockedCompareExchange64) |
| 37 | + #endif |
| 38 | +#endif // CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS |
| 39 | + |
| 40 | +namespace crnlib |
| 41 | +{ |
| 42 | +#if CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS |
| 43 | + typedef LONG atomic32_t; |
| 44 | + typedef LONGLONG atomic64_t; |
| 45 | + |
| 46 | + // Returns the original value. |
| 47 | + inline atomic32_t atomic_compare_exchange32(atomic32_t volatile *pDest, atomic32_t exchange, atomic32_t comparand) |
| 48 | + { |
| 49 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 50 | + return InterlockedCompareExchange(pDest, exchange, comparand); |
| 51 | + } |
| 52 | + |
| 53 | + // Returns the original value. |
| 54 | + inline atomic64_t atomic_compare_exchange64(atomic64_t volatile *pDest, atomic64_t exchange, atomic64_t comparand) |
| 55 | + { |
| 56 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 7) == 0); |
| 57 | + return _InterlockedCompareExchange64(pDest, exchange, comparand); |
| 58 | + } |
| 59 | + |
| 60 | + // Returns the resulting incremented value. |
| 61 | + inline atomic32_t atomic_increment32(atomic32_t volatile *pDest) |
| 62 | + { |
| 63 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 64 | + return InterlockedIncrement(pDest); |
| 65 | + } |
| 66 | + |
| 67 | + // Returns the resulting decremented value. |
| 68 | + inline atomic32_t atomic_decrement32(atomic32_t volatile *pDest) |
| 69 | + { |
| 70 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 71 | + return InterlockedDecrement(pDest); |
| 72 | + } |
| 73 | + |
| 74 | + // Returns the original value. |
| 75 | + inline atomic32_t atomic_exchange32(atomic32_t volatile *pDest, atomic32_t val) |
| 76 | + { |
| 77 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 78 | + return InterlockedExchange(pDest, val); |
| 79 | + } |
| 80 | + |
| 81 | + // Returns the resulting value. |
| 82 | + inline atomic32_t atomic_add32(atomic32_t volatile *pDest, atomic32_t val) |
| 83 | + { |
| 84 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 85 | + return InterlockedExchangeAdd(pDest, val) + val; |
| 86 | + } |
| 87 | + |
| 88 | + // Returns the original value. |
| 89 | + inline atomic32_t atomic_exchange_add32(atomic32_t volatile *pDest, atomic32_t val) |
| 90 | + { |
| 91 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 92 | + return InterlockedExchangeAdd(pDest, val); |
| 93 | + } |
| 94 | +#elif CRNLIB_USE_GCC_ATOMIC_BUILTINS |
| 95 | + typedef long atomic32_t; |
| 96 | + typedef long long atomic64_t; |
| 97 | + |
| 98 | + // Returns the original value. |
| 99 | + inline atomic32_t atomic_compare_exchange32(atomic32_t volatile *pDest, atomic32_t exchange, atomic32_t comparand) |
| 100 | + { |
| 101 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 102 | + return __sync_val_compare_and_swap(pDest, comparand, exchange); |
| 103 | + } |
| 104 | + |
| 105 | + // Returns the original value. |
| 106 | + inline atomic64_t atomic_compare_exchange64(atomic64_t volatile *pDest, atomic64_t exchange, atomic64_t comparand) |
| 107 | + { |
| 108 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 7) == 0); |
| 109 | + return __sync_val_compare_and_swap(pDest, comparand, exchange); |
| 110 | + } |
| 111 | + |
| 112 | + // Returns the resulting incremented value. |
| 113 | + inline atomic32_t atomic_increment32(atomic32_t volatile *pDest) |
| 114 | + { |
| 115 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 116 | + return __sync_add_and_fetch(pDest, 1); |
| 117 | + } |
| 118 | + |
| 119 | + // Returns the resulting decremented value. |
| 120 | + inline atomic32_t atomic_decrement32(atomic32_t volatile *pDest) |
| 121 | + { |
| 122 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 123 | + return __sync_sub_and_fetch(pDest, 1); |
| 124 | + } |
| 125 | + |
| 126 | + // Returns the original value. |
| 127 | + inline atomic32_t atomic_exchange32(atomic32_t volatile *pDest, atomic32_t val) |
| 128 | + { |
| 129 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 130 | + return __sync_lock_test_and_set(pDest, val); |
| 131 | + } |
| 132 | + |
| 133 | + // Returns the resulting value. |
| 134 | + inline atomic32_t atomic_add32(atomic32_t volatile *pDest, atomic32_t val) |
| 135 | + { |
| 136 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 137 | + return __sync_add_and_fetch(pDest, val); |
| 138 | + } |
| 139 | + |
| 140 | + // Returns the original value. |
| 141 | + inline atomic32_t atomic_exchange_add32(atomic32_t volatile *pDest, atomic32_t val) |
| 142 | + { |
| 143 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 144 | + return __sync_fetch_and_add(pDest, val); |
| 145 | + } |
| 146 | +#else |
| 147 | + #define CRNLIB_NO_ATOMICS 1 |
| 148 | + |
| 149 | + // Atomic ops not supported - but try to do something reasonable. Assumes no threading at all. |
| 150 | + typedef long atomic32_t; |
| 151 | + typedef long long atomic64_t; |
| 152 | + |
| 153 | + inline atomic32_t atomic_compare_exchange32(atomic32_t volatile *pDest, atomic32_t exchange, atomic32_t comparand) |
| 154 | + { |
| 155 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 156 | + atomic32_t cur = *pDest; |
| 157 | + if (cur == comparand) |
| 158 | + *pDest = exchange; |
| 159 | + return cur; |
| 160 | + } |
| 161 | + |
| 162 | + inline atomic64_t atomic_compare_exchange64(atomic64_t volatile *pDest, atomic64_t exchange, atomic64_t comparand) |
| 163 | + { |
| 164 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 7) == 0); |
| 165 | + atomic64_t cur = *pDest; |
| 166 | + if (cur == comparand) |
| 167 | + *pDest = exchange; |
| 168 | + return cur; |
| 169 | + } |
| 170 | + |
| 171 | + inline atomic32_t atomic_increment32(atomic32_t volatile *pDest) |
| 172 | + { |
| 173 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 174 | + return (*pDest += 1); |
| 175 | + } |
| 176 | + |
| 177 | + inline atomic32_t atomic_decrement32(atomic32_t volatile *pDest) |
| 178 | + { |
| 179 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 180 | + return (*pDest -= 1); |
| 181 | + } |
| 182 | + |
| 183 | + inline atomic32_t atomic_exchange32(atomic32_t volatile *pDest, atomic32_t val) |
| 184 | + { |
| 185 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 186 | + atomic32_t cur = *pDest; |
| 187 | + *pDest = val; |
| 188 | + return cur; |
| 189 | + } |
| 190 | + |
| 191 | + inline atomic32_t atomic_add32(atomic32_t volatile *pDest, atomic32_t val) |
| 192 | + { |
| 193 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 194 | + return (*pDest += val); |
| 195 | + } |
| 196 | + |
| 197 | + inline atomic32_t atomic_exchange_add32(atomic32_t volatile *pDest, atomic32_t val) |
| 198 | + { |
| 199 | + CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0); |
| 200 | + atomic32_t cur = *pDest; |
| 201 | + *pDest += val; |
| 202 | + return cur; |
| 203 | + } |
| 204 | +#endif |
| 205 | + |
| 206 | +} // namespace crnlib |
| 207 | + |
| 208 | +#endif // CRN_ATOMICS_H |
0 commit comments