David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 1 | /*===-- atomic.c - Implement support functions for atomic operations.------=== |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
| 5 | * This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | * Source Licenses. See LICENSE.TXT for details. |
| 7 | * |
| 8 | *===----------------------------------------------------------------------=== |
| 9 | * |
| 10 | * atomic.c defines a set of functions for performing atomic accesses on |
| 11 | * arbitrary-sized memory locations. This design uses locks that should |
| 12 | * be fast in the uncontended case, for two reasons: |
| 13 | * |
| 14 | * 1) This code must work with C programs that do not link to anything |
| 15 | * (including pthreads) and so it should not depend on any pthread |
| 16 | * functions. |
| 17 | * 2) Atomic operations, rather than explicit mutexes, are most commonly used |
| 18 | * on code where contended operations are rate. |
| 19 | * |
| 20 | * To avoid needing a per-object lock, this code allocates an array of |
| 21 | * locks and hashes the object pointers to find the one that it should use. |
| 22 | * For operations that must be atomic on two locations, the lower lock is |
| 23 | * always acquired first, to avoid deadlock. |
| 24 | * |
| 25 | *===----------------------------------------------------------------------=== |
| 26 | */ |
| 27 | |
Andrew Hsieh | c880fea | 2013-09-27 13:33:10 +0800 | [diff] [blame] | 28 | #if defined(__clang__) |
| 29 | |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 30 | #include <stdint.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | // Clang objects if you redefine a builtin. This little hack allows us to |
| 34 | // define a function with the same name as an intrinsic. |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 35 | #pragma redefine_extname __atomic_load_c __atomic_load |
| 36 | #pragma redefine_extname __atomic_store_c __atomic_store |
| 37 | #pragma redefine_extname __atomic_exchange_c __atomic_exchange |
| 38 | #pragma redefine_extname __atomic_compare_exchange_c __atomic_compare_exchange |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 39 | |
| 40 | /// Number of locks. This allocates one page on 32-bit platforms, two on |
| 41 | /// 64-bit. This can be specified externally if a different trade between |
| 42 | /// memory usage and contention probability is required for a given platform. |
| 43 | #ifndef SPINLOCK_COUNT |
| 44 | #define SPINLOCK_COUNT (1<<10) |
| 45 | #endif |
| 46 | static const long SPINLOCK_MASK = SPINLOCK_COUNT - 1; |
| 47 | |
| 48 | //////////////////////////////////////////////////////////////////////////////// |
| 49 | // Platform-specific lock implementation. Falls back to spinlocks if none is |
| 50 | // defined. Each platform should define the Lock type, and corresponding |
| 51 | // lock() and unlock() functions. |
| 52 | //////////////////////////////////////////////////////////////////////////////// |
| 53 | #ifdef __FreeBSD__ |
| 54 | #include <errno.h> |
| 55 | #include <sys/types.h> |
| 56 | #include <machine/atomic.h> |
| 57 | #include <sys/umtx.h> |
| 58 | typedef struct _usem Lock; |
| 59 | inline static void unlock(Lock *l) { |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 60 | __c11_atomic_store((_Atomic(uint32_t)*)&l->_count, 1, __ATOMIC_RELEASE); |
| 61 | __c11_atomic_thread_fence(__ATOMIC_SEQ_CST); |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 62 | if (l->_has_waiters) |
| 63 | _umtx_op(l, UMTX_OP_SEM_WAKE, 1, 0, 0); |
| 64 | } |
| 65 | inline static void lock(Lock *l) { |
| 66 | uint32_t old = 1; |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 67 | while (!__c11_atomic_compare_exchange_weak((_Atomic(uint32_t)*)&l->_count, &old, |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 68 | 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { |
| 69 | _umtx_op(l, UMTX_OP_SEM_WAIT, 0, 0, 0); |
| 70 | old = 1; |
| 71 | } |
| 72 | } |
| 73 | /// locks for atomic operations |
| 74 | static Lock locks[SPINLOCK_COUNT] = { [0 ... SPINLOCK_COUNT-1] = {0,1,0} }; |
| 75 | #else |
| 76 | typedef _Atomic(uintptr_t) Lock; |
| 77 | /// Unlock a lock. This is a release operation. |
| 78 | inline static void unlock(Lock *l) { |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 79 | __c11_atomic_store(l, 0, __ATOMIC_RELEASE); |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 80 | } |
| 81 | /// Locks a lock. In the current implementation, this is potentially |
| 82 | /// unbounded in the contended case. |
| 83 | inline static void lock(Lock *l) { |
| 84 | uintptr_t old = 0; |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 85 | while (!__c11_atomic_compare_exchange_weak(l, &old, 1, __ATOMIC_ACQUIRE, |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 86 | __ATOMIC_RELAXED)) |
| 87 | old = 0; |
| 88 | } |
| 89 | /// locks for atomic operations |
| 90 | static Lock locks[SPINLOCK_COUNT]; |
| 91 | #endif |
| 92 | |
| 93 | |
| 94 | /// Returns a lock to use for a given pointer. |
| 95 | static inline Lock *lock_for_pointer(void *ptr) { |
| 96 | intptr_t hash = (intptr_t)ptr; |
| 97 | // Disregard the lowest 4 bits. We want all values that may be part of the |
| 98 | // same memory operation to hash to the same value and therefore use the same |
| 99 | // lock. |
| 100 | hash >>= 4; |
| 101 | // Use the next bits as the basis for the hash |
| 102 | intptr_t low = hash & SPINLOCK_MASK; |
| 103 | // Now use the high(er) set of bits to perturb the hash, so that we don't |
| 104 | // get collisions from atomic fields in a single object |
| 105 | hash >>= 16; |
| 106 | hash ^= low; |
| 107 | // Return a pointer to the word to use |
| 108 | return locks + (hash & SPINLOCK_MASK); |
| 109 | } |
| 110 | |
| 111 | /// Macros for determining whether a size is lock free. Clang can not yet |
| 112 | /// codegen __atomic_is_lock_free(16), so for now we assume 16-byte values are |
| 113 | /// not lock free. |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 114 | #define IS_LOCK_FREE_1 __c11_atomic_is_lock_free(1) |
| 115 | #define IS_LOCK_FREE_2 __c11_atomic_is_lock_free(2) |
| 116 | #define IS_LOCK_FREE_4 __c11_atomic_is_lock_free(4) |
| 117 | #define IS_LOCK_FREE_8 __c11_atomic_is_lock_free(8) |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 118 | #define IS_LOCK_FREE_16 0 |
| 119 | |
| 120 | /// Macro that calls the compiler-generated lock-free versions of functions |
| 121 | /// when they exist. |
| 122 | #define LOCK_FREE_CASES() \ |
| 123 | do {\ |
| 124 | switch (size) {\ |
| 125 | case 2:\ |
| 126 | if (IS_LOCK_FREE_2) {\ |
| 127 | LOCK_FREE_ACTION(uint16_t);\ |
| 128 | }\ |
| 129 | case 4:\ |
| 130 | if (IS_LOCK_FREE_4) {\ |
| 131 | LOCK_FREE_ACTION(uint32_t);\ |
| 132 | }\ |
| 133 | case 8:\ |
| 134 | if (IS_LOCK_FREE_8) {\ |
| 135 | LOCK_FREE_ACTION(uint64_t);\ |
| 136 | }\ |
| 137 | case 16:\ |
| 138 | if (IS_LOCK_FREE_16) {\ |
Benjamin Kramer | fa3eee4 | 2012-03-30 21:37:08 +0000 | [diff] [blame] | 139 | /* FIXME: __uint128_t isn't available on 32 bit platforms. |
| 140 | LOCK_FREE_ACTION(__uint128_t);*/\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 141 | }\ |
| 142 | }\ |
| 143 | } while (0) |
| 144 | |
| 145 | |
| 146 | /// An atomic load operation. This is atomic with respect to the source |
| 147 | /// pointer only. |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 148 | void __atomic_load_c(int size, void *src, void *dest, int model) { |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 149 | #define LOCK_FREE_ACTION(type) \ |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 150 | *((type*)dest) = __c11_atomic_load((_Atomic(type)*)src, model);\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 151 | return; |
| 152 | LOCK_FREE_CASES(); |
| 153 | #undef LOCK_FREE_ACTION |
| 154 | Lock *l = lock_for_pointer(src); |
| 155 | lock(l); |
| 156 | memcpy(dest, src, size); |
| 157 | unlock(l); |
| 158 | } |
| 159 | |
| 160 | /// An atomic store operation. This is atomic with respect to the destination |
| 161 | /// pointer only. |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 162 | void __atomic_store_c(int size, void *dest, void *src, int model) { |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 163 | #define LOCK_FREE_ACTION(type) \ |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 164 | __c11_atomic_store((_Atomic(type)*)dest, *(type*)dest, model);\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 165 | return; |
| 166 | LOCK_FREE_CASES(); |
| 167 | #undef LOCK_FREE_ACTION |
| 168 | Lock *l = lock_for_pointer(dest); |
| 169 | lock(l); |
| 170 | memcpy(dest, src, size); |
| 171 | unlock(l); |
| 172 | } |
| 173 | |
| 174 | /// Atomic compare and exchange operation. If the value at *ptr is identical |
| 175 | /// to the value at *expected, then this copies value at *desired to *ptr. If |
| 176 | /// they are not, then this stores the current value from *ptr in *expected. |
| 177 | /// |
| 178 | /// This function returns 1 if the exchange takes place or 0 if it fails. |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 179 | int __atomic_compare_exchange_c(int size, void *ptr, void *expected, |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 180 | void *desired, int success, int failure) { |
| 181 | #define LOCK_FREE_ACTION(type) \ |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 182 | return __c11_atomic_compare_exchange_strong((_Atomic(type)*)ptr, (type*)expected,\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 183 | *(type*)desired, success, failure) |
| 184 | LOCK_FREE_CASES(); |
| 185 | #undef LOCK_FREE_ACTION |
| 186 | Lock *l = lock_for_pointer(ptr); |
| 187 | lock(l); |
| 188 | if (memcmp(ptr, expected, size) == 0) { |
| 189 | memcpy(ptr, desired, size); |
| 190 | unlock(l); |
| 191 | return 1; |
| 192 | } |
| 193 | memcpy(expected, ptr, size); |
| 194 | unlock(l); |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /// Performs an atomic exchange operation between two pointers. This is atomic |
| 199 | /// with respect to the target address. |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 200 | void __atomic_exchange_c(int size, void *ptr, void *val, void *old, int model) { |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 201 | #define LOCK_FREE_ACTION(type) \ |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 202 | *(type*)old = __c11_atomic_exchange((_Atomic(type)*)ptr, *(type*)val,\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 203 | model);\ |
| 204 | return; |
| 205 | LOCK_FREE_CASES(); |
| 206 | #undef LOCK_FREE_ACTION |
| 207 | Lock *l = lock_for_pointer(ptr); |
| 208 | lock(l); |
| 209 | memcpy(old, ptr, size); |
| 210 | memcpy(ptr, val, size); |
| 211 | unlock(l); |
| 212 | } |
| 213 | |
| 214 | //////////////////////////////////////////////////////////////////////////////// |
| 215 | // Where the size is known at compile time, the compiler may emit calls to |
| 216 | // specialised versions of the above functions. |
| 217 | //////////////////////////////////////////////////////////////////////////////// |
| 218 | #define OPTIMISED_CASES\ |
| 219 | OPTIMISED_CASE(1, IS_LOCK_FREE_1, uint8_t)\ |
| 220 | OPTIMISED_CASE(2, IS_LOCK_FREE_2, uint16_t)\ |
| 221 | OPTIMISED_CASE(4, IS_LOCK_FREE_4, uint32_t)\ |
| 222 | OPTIMISED_CASE(8, IS_LOCK_FREE_8, uint64_t)\ |
Benjamin Kramer | fa3eee4 | 2012-03-30 21:37:08 +0000 | [diff] [blame] | 223 | /* FIXME: __uint128_t isn't available on 32 bit platforms. |
| 224 | OPTIMISED_CASE(16, IS_LOCK_FREE_16, __uint128_t)*/\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 225 | |
| 226 | #define OPTIMISED_CASE(n, lockfree, type)\ |
| 227 | type __atomic_load_##n(type *src, int model) {\ |
| 228 | if (lockfree)\ |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 229 | return __c11_atomic_load((_Atomic(type)*)src, model);\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 230 | Lock *l = lock_for_pointer(src);\ |
| 231 | lock(l);\ |
| 232 | type val = *src;\ |
| 233 | unlock(l);\ |
| 234 | return val;\ |
| 235 | } |
| 236 | OPTIMISED_CASES |
| 237 | #undef OPTIMISED_CASE |
| 238 | |
| 239 | #define OPTIMISED_CASE(n, lockfree, type)\ |
| 240 | void __atomic_store_##n(type *dest, type val, int model) {\ |
| 241 | if (lockfree) {\ |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 242 | __c11_atomic_store((_Atomic(type)*)dest, val, model);\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 243 | return;\ |
| 244 | }\ |
| 245 | Lock *l = lock_for_pointer(dest);\ |
| 246 | lock(l);\ |
| 247 | *dest = val;\ |
| 248 | unlock(l);\ |
| 249 | return;\ |
| 250 | } |
| 251 | OPTIMISED_CASES |
| 252 | #undef OPTIMISED_CASE |
| 253 | |
| 254 | #define OPTIMISED_CASE(n, lockfree, type)\ |
| 255 | type __atomic_exchange_##n(type *dest, type val, int model) {\ |
| 256 | if (lockfree)\ |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 257 | return __c11_atomic_exchange((_Atomic(type)*)dest, val, model);\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 258 | Lock *l = lock_for_pointer(dest);\ |
| 259 | lock(l);\ |
| 260 | type tmp = *dest;\ |
| 261 | *dest = val;\ |
| 262 | unlock(l);\ |
| 263 | return tmp;\ |
| 264 | } |
| 265 | OPTIMISED_CASES |
| 266 | #undef OPTIMISED_CASE |
| 267 | |
| 268 | #define OPTIMISED_CASE(n, lockfree, type)\ |
| 269 | int __atomic_compare_exchange_##n(type *ptr, type *expected, type desired,\ |
| 270 | int success, int failure) {\ |
| 271 | if (lockfree)\ |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 272 | return __c11_atomic_compare_exchange_strong((_Atomic(type)*)ptr, expected, desired,\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 273 | success, failure);\ |
| 274 | Lock *l = lock_for_pointer(ptr);\ |
| 275 | lock(l);\ |
| 276 | if (*ptr == *expected) {\ |
| 277 | *ptr = desired;\ |
| 278 | unlock(l);\ |
| 279 | return 1;\ |
| 280 | }\ |
| 281 | *expected = *ptr;\ |
| 282 | unlock(l);\ |
| 283 | return 0;\ |
| 284 | } |
| 285 | OPTIMISED_CASES |
| 286 | #undef OPTIMISED_CASE |
| 287 | |
| 288 | //////////////////////////////////////////////////////////////////////////////// |
| 289 | // Atomic read-modify-write operations for integers of various sizes. |
| 290 | //////////////////////////////////////////////////////////////////////////////// |
| 291 | #define ATOMIC_RMW(n, lockfree, type, opname, op) \ |
| 292 | type __atomic_fetch_##opname##_##n(type *ptr, type val, int model) {\ |
| 293 | if (lockfree) \ |
David Chisnall | bc7ab9c | 2012-05-15 12:36:14 +0000 | [diff] [blame] | 294 | return __c11_atomic_fetch_##opname((_Atomic(type)*)ptr, val, model);\ |
David Chisnall | 2aff738 | 2012-03-30 08:35:32 +0000 | [diff] [blame] | 295 | Lock *l = lock_for_pointer(ptr);\ |
| 296 | lock(l);\ |
| 297 | type tmp = *ptr;\ |
| 298 | *ptr = tmp op val;\ |
| 299 | unlock(l);\ |
| 300 | return tmp;\ |
| 301 | } |
| 302 | |
| 303 | #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, add, +) |
| 304 | OPTIMISED_CASES |
| 305 | #undef OPTIMISED_CASE |
| 306 | #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, sub, -) |
| 307 | OPTIMISED_CASES |
| 308 | #undef OPTIMISED_CASE |
| 309 | #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, and, &) |
| 310 | OPTIMISED_CASES |
| 311 | #undef OPTIMISED_CASE |
| 312 | #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, or, |) |
| 313 | OPTIMISED_CASES |
| 314 | #undef OPTIMISED_CASE |
| 315 | #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, xor, ^) |
| 316 | OPTIMISED_CASES |
| 317 | #undef OPTIMISED_CASE |
Andrew Hsieh | c880fea | 2013-09-27 13:33:10 +0800 | [diff] [blame] | 318 | |
| 319 | #endif // __clang__ |