diff options
Diffstat (limited to 'examples/redis-unstable/src/atomicvar.h')
| -rw-r--r-- | examples/redis-unstable/src/atomicvar.h | 186 |
1 files changed, 0 insertions, 186 deletions
diff --git a/examples/redis-unstable/src/atomicvar.h b/examples/redis-unstable/src/atomicvar.h deleted file mode 100644 index 3c332ee..0000000 --- a/examples/redis-unstable/src/atomicvar.h +++ /dev/null | |||
| @@ -1,186 +0,0 @@ | |||
| 1 | /* This file implements atomic counters using c11 _Atomic, __atomic or __sync | ||
| 2 | * macros if available, otherwise we will throw an error when compile. | ||
| 3 | * | ||
| 4 | * The exported interface is composed of the following macros: | ||
| 5 | * | ||
| 6 | * atomicIncr(var,count) -- Increment the atomic counter | ||
| 7 | * atomicGetIncr(var,oldvalue_var,count) -- Get and increment the atomic counter | ||
| 8 | * atomicIncrGet(var,newvalue_var,count) -- Increment and get the atomic counter new value | ||
| 9 | * atomicDecr(var,count) -- Decrement the atomic counter | ||
| 10 | * atomicGet(var,dstvar) -- Fetch the atomic counter value | ||
| 11 | * atomicSet(var,value) -- Set the atomic counter value | ||
| 12 | * atomicGetWithSync(var,value) -- 'atomicGet' with inter-thread synchronization | ||
| 13 | * atomicSetWithSync(var,value) -- 'atomicSet' with inter-thread synchronization | ||
| 14 | * atomicCompareExchange(type,var,expected_var,desired) -- Compare and exchange (CAS) operation | ||
| 15 | * | ||
| 16 | * Atomic operations on flags. | ||
| 17 | * Flag type can be int, long, long long or their unsigned counterparts. | ||
| 18 | * The value of the flag can be 1 or 0. | ||
| 19 | * | ||
| 20 | * atomicFlagGetSet(var,oldvalue_var) -- Get and set the atomic counter value | ||
| 21 | * | ||
| 22 | * NOTE1: __atomic* and _Atomic implementations can be actually elaborated to support any value by changing the | ||
| 23 | * hardcoded new value passed to __atomic_exchange* from 1 to @param count | ||
| 24 | * i.e oldvalue_var = atomic_exchange_explicit(&var, count). | ||
| 25 | * However, in order to be compatible with the __sync functions family, we can use only 0 and 1. | ||
| 26 | * The only exchange alternative suggested by __sync is __sync_lock_test_and_set, | ||
| 27 | * But as described by the gnu manual for __sync_lock_test_and_set(): | ||
| 28 | * https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html | ||
| 29 | * "A target may support reduced functionality here by which the only valid value to store is the immediate constant 1. The exact value | ||
| 30 | * actually stored in *ptr is implementation defined." | ||
| 31 | * Hence, we can't rely on it for a any value other than 1. | ||
| 32 | * We eventually chose to implement this method with __sync_val_compare_and_swap since it satisfies functionality needed for atomicFlagGetSet | ||
| 33 | * (if the flag was 0 -> set to 1, if it's already 1 -> do nothing, but the final result is that the flag is set), | ||
| 34 | * and also it has a full barrier (__sync_lock_test_and_set has acquire barrier). | ||
| 35 | * | ||
| 36 | * NOTE2: Unlike other atomic type, which aren't guaranteed to be lock free, c11 atomic_flag does. | ||
| 37 | * To check whether a type is lock free, atomic_is_lock_free() can be used. | ||
| 38 | * It can be considered to limit the flag type to atomic_flag to improve performance. | ||
| 39 | * | ||
| 40 | * Never use return value from the macros, instead use the AtomicGetIncr() | ||
| 41 | * if you need to get the current value and increment it atomically, like | ||
| 42 | * in the following example: | ||
| 43 | * | ||
| 44 | * long oldvalue; | ||
| 45 | * atomicGetIncr(myvar,oldvalue,1); | ||
| 46 | * doSomethingWith(oldvalue); | ||
| 47 | * | ||
| 48 | * ---------------------------------------------------------------------------- | ||
| 49 | * | ||
| 50 | * Copyright (c) 2015-Present, Redis Ltd. | ||
| 51 | * All rights reserved. | ||
| 52 | * | ||
| 53 | * Licensed under your choice of (a) the Redis Source Available License 2.0 | ||
| 54 | * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the | ||
| 55 | * GNU Affero General Public License v3 (AGPLv3). | ||
| 56 | */ | ||
| 57 | |||
| 58 | #include <pthread.h> | ||
| 59 | #include "config.h" | ||
| 60 | |||
| 61 | #ifndef __ATOMIC_VAR_H | ||
| 62 | #define __ATOMIC_VAR_H | ||
| 63 | |||
| 64 | /* Define redisAtomic for atomic variable. */ | ||
| 65 | #define redisAtomic | ||
| 66 | |||
| 67 | /* To test Redis with Helgrind (a Valgrind tool) it is useful to define | ||
| 68 | * the following macro, so that __sync macros are used: those can be detected | ||
| 69 | * by Helgrind (even if they are less efficient) so that no false positive | ||
| 70 | * is reported. */ | ||
| 71 | // #define __ATOMIC_VAR_FORCE_SYNC_MACROS | ||
| 72 | |||
| 73 | /* There will be many false positives if we test Redis with Helgrind, since | ||
| 74 | * Helgrind can't understand we have imposed ordering on the program, so | ||
| 75 | * we use macros in helgrind.h to tell Helgrind inter-thread happens-before | ||
| 76 | * relationship explicitly for avoiding false positives. | ||
| 77 | * | ||
| 78 | * For more details, please see: valgrind/helgrind.h and | ||
| 79 | * https://www.valgrind.org/docs/manual/hg-manual.html#hg-manual.effective-use | ||
| 80 | * | ||
| 81 | * These macros take effect only when 'make helgrind', and you must first | ||
| 82 | * install Valgrind in the default path configuration. */ | ||
| 83 | #ifdef __ATOMIC_VAR_FORCE_SYNC_MACROS | ||
| 84 | #include <valgrind/helgrind.h> | ||
| 85 | #else | ||
| 86 | #define ANNOTATE_HAPPENS_BEFORE(v) ((void) v) | ||
| 87 | #define ANNOTATE_HAPPENS_AFTER(v) ((void) v) | ||
| 88 | #endif | ||
| 89 | |||
| 90 | #if !defined(__ATOMIC_VAR_FORCE_SYNC_MACROS) && defined(__STDC_VERSION__) && \ | ||
| 91 | (__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__) | ||
| 92 | /* Use '_Atomic' keyword if the compiler supports. */ | ||
| 93 | #undef redisAtomic | ||
| 94 | #define redisAtomic _Atomic | ||
| 95 | /* Implementation using _Atomic in C11. */ | ||
| 96 | |||
| 97 | #include <stdatomic.h> | ||
| 98 | #define atomicIncr(var,count) atomic_fetch_add_explicit(&var,(count),memory_order_relaxed) | ||
| 99 | #define atomicGetIncr(var,oldvalue_var,count) do { \ | ||
| 100 | oldvalue_var = atomic_fetch_add_explicit(&var,(count),memory_order_relaxed); \ | ||
| 101 | } while(0) | ||
| 102 | #define atomicIncrGet(var, newvalue_var, count) \ | ||
| 103 | newvalue_var = atomicIncr(var,count) + count | ||
| 104 | #define atomicDecr(var,count) atomic_fetch_sub_explicit(&var,(count),memory_order_relaxed) | ||
| 105 | #define atomicGet(var,dstvar) do { \ | ||
| 106 | dstvar = atomic_load_explicit(&var,memory_order_relaxed); \ | ||
| 107 | } while(0) | ||
| 108 | #define atomicSet(var,value) atomic_store_explicit(&var,value,memory_order_relaxed) | ||
| 109 | #define atomicGetWithSync(var,dstvar) do { \ | ||
| 110 | dstvar = atomic_load_explicit(&var,memory_order_seq_cst); \ | ||
| 111 | } while(0) | ||
| 112 | #define atomicSetWithSync(var,value) \ | ||
| 113 | atomic_store_explicit(&var,value,memory_order_seq_cst) | ||
| 114 | #define atomicCompareExchange(type,var,expected_var,desired) \ | ||
| 115 | atomic_compare_exchange_weak_explicit(&var,&expected_var,desired,memory_order_relaxed,memory_order_relaxed) | ||
| 116 | #define atomicFlagGetSet(var,oldvalue_var) \ | ||
| 117 | oldvalue_var = atomic_exchange_explicit(&var,1,memory_order_relaxed) | ||
| 118 | #define REDIS_ATOMIC_API "c11-builtin" | ||
| 119 | |||
| 120 | #elif !defined(__ATOMIC_VAR_FORCE_SYNC_MACROS) && \ | ||
| 121 | (!defined(__clang__) || !defined(__APPLE__) || __apple_build_version__ > 4210057) && \ | ||
| 122 | defined(__ATOMIC_RELAXED) && defined(__ATOMIC_SEQ_CST) | ||
| 123 | /* Implementation using __atomic macros. */ | ||
| 124 | |||
| 125 | #define atomicIncr(var,count) __atomic_add_fetch(&var,(count),__ATOMIC_RELAXED) | ||
| 126 | #define atomicIncrGet(var, newvalue_var, count) \ | ||
| 127 | newvalue_var = __atomic_add_fetch(&var,(count),__ATOMIC_RELAXED) | ||
| 128 | #define atomicGetIncr(var,oldvalue_var,count) do { \ | ||
| 129 | oldvalue_var = __atomic_fetch_add(&var,(count),__ATOMIC_RELAXED); \ | ||
| 130 | } while(0) | ||
| 131 | #define atomicDecr(var,count) __atomic_sub_fetch(&var,(count),__ATOMIC_RELAXED) | ||
| 132 | #define atomicGet(var,dstvar) do { \ | ||
| 133 | dstvar = __atomic_load_n(&var,__ATOMIC_RELAXED); \ | ||
| 134 | } while(0) | ||
| 135 | #define atomicSet(var,value) __atomic_store_n(&var,value,__ATOMIC_RELAXED) | ||
| 136 | #define atomicGetWithSync(var,dstvar) do { \ | ||
| 137 | dstvar = __atomic_load_n(&var,__ATOMIC_SEQ_CST); \ | ||
| 138 | } while(0) | ||
| 139 | #define atomicSetWithSync(var,value) \ | ||
| 140 | __atomic_store_n(&var,value,__ATOMIC_SEQ_CST) | ||
| 141 | #define atomicCompareExchange(type,var,expected_var,desired) \ | ||
| 142 | __atomic_compare_exchange_n(&var,&expected_var,desired,1,__ATOMIC_RELAXED,__ATOMIC_RELAXED) | ||
| 143 | #define atomicFlagGetSet(var,oldvalue_var) \ | ||
| 144 | oldvalue_var = __atomic_exchange_n(&var,1,__ATOMIC_RELAXED) | ||
| 145 | #define REDIS_ATOMIC_API "atomic-builtin" | ||
| 146 | |||
| 147 | #elif defined(HAVE_ATOMIC) | ||
| 148 | /* Implementation using __sync macros. */ | ||
| 149 | |||
| 150 | #define atomicIncr(var,count) __sync_add_and_fetch(&var,(count)) | ||
| 151 | #define atomicIncrGet(var, newvalue_var, count) \ | ||
| 152 | newvalue_var = __sync_add_and_fetch(&var,(count)) | ||
| 153 | #define atomicGetIncr(var,oldvalue_var,count) do { \ | ||
| 154 | oldvalue_var = __sync_fetch_and_add(&var,(count)); \ | ||
| 155 | } while(0) | ||
| 156 | #define atomicDecr(var,count) __sync_sub_and_fetch(&var,(count)) | ||
| 157 | #define atomicGet(var,dstvar) do { \ | ||
| 158 | dstvar = __sync_sub_and_fetch(&var,0); \ | ||
| 159 | } while(0) | ||
| 160 | #define atomicSet(var,value) do { \ | ||
| 161 | while(!__sync_bool_compare_and_swap(&var,var,value)); \ | ||
| 162 | } while(0) | ||
| 163 | /* Actually the builtin issues a full memory barrier by default. */ | ||
| 164 | #define atomicGetWithSync(var,dstvar) do { \ | ||
| 165 | dstvar = __sync_sub_and_fetch(&var,0,__sync_synchronize); \ | ||
| 166 | ANNOTATE_HAPPENS_AFTER(&var); \ | ||
| 167 | } while(0) | ||
| 168 | #define atomicSetWithSync(var,value) do { \ | ||
| 169 | ANNOTATE_HAPPENS_BEFORE(&var); \ | ||
| 170 | while(!__sync_bool_compare_and_swap(&var,var,value,__sync_synchronize)); \ | ||
| 171 | } while(0) | ||
| 172 | #define atomicCompareExchange(type,var,expected_var,desired) ({ \ | ||
| 173 | type _old = __sync_val_compare_and_swap(&var,expected_var,desired); \ | ||
| 174 | int _success = (_old == expected_var); \ | ||
| 175 | if (!_success) expected_var = _old; \ | ||
| 176 | _success; \ | ||
| 177 | }) | ||
| 178 | #define atomicFlagGetSet(var,oldvalue_var) \ | ||
| 179 | oldvalue_var = __sync_val_compare_and_swap(&var,0,1) | ||
| 180 | #define REDIS_ATOMIC_API "sync-builtin" | ||
| 181 | |||
| 182 | #else | ||
| 183 | #error "Unable to determine atomic operations for your platform" | ||
| 184 | |||
| 185 | #endif | ||
| 186 | #endif /* __ATOMIC_VAR_H */ | ||
