| 1 |
#ifndef AWS_COMMON_ATOMICS_GNU_OLD_INL |
| 2 |
#define AWS_COMMON_ATOMICS_GNU_OLD_INL |
| 3 |
/** |
| 4 |
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 |
* SPDX-License-Identifier: Apache-2.0. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* These are implicitly included, but help with editor highlighting */ |
| 9 |
#include <aws/common/atomics.h> |
| 10 |
#include <aws/common/common.h> |
| 11 |
|
| 12 |
#include <stdint.h> |
| 13 |
#include <stdlib.h> |
| 14 |
|
| 15 |
AWS_EXTERN_C_BEGIN |
| 16 |
|
| 17 |
#if defined(__GNUC__) |
| 18 |
# if (__GNUC__ < 4) |
| 19 |
# error GCC versions before 4.1.2 are not supported |
| 20 |
# elif (defined(__arm__) || defined(__ia64__)) && (__GNUC__ == 4 && __GNUC_MINOR__ < 4) |
| 21 |
/* See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36793 Itanium codegen */ |
| 22 |
/* https://bugs.launchpad.net/ubuntu/+source/gcc-4.4/+bug/491872 ARM codegen*/ |
| 23 |
/* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42263 ARM codegen */ |
| 24 |
# error GCC versions before 4.4.0 are not supported on ARM or Itanium |
| 25 |
# elif (defined(__x86_64__) || defined(__i386__)) && \ |
| 26 |
(__GNUC__ == 4 && (__GNUC_MINOR__ < 1 || (__GNUC_MINOR__ == 1 && __GNUC_PATCHLEVEL__ < 2))) |
| 27 |
/* 4.1.2 is the first gcc version with 100% working atomic intrinsics on Intel */ |
| 28 |
# error GCC versions before 4.1.2 are not supported on x86/x64 |
| 29 |
# endif |
| 30 |
#endif |
| 31 |
|
| 32 |
typedef size_t aws_atomic_impl_int_t; |
| 33 |
|
| 34 |
static inline void aws_atomic_private_compiler_barrier(void) { |
| 35 |
__asm__ __volatile__("" : : : "memory"); |
| 36 |
} |
| 37 |
|
| 38 |
static inline void aws_atomic_private_barrier_before(enum aws_memory_order order) { |
| 39 |
if (order == aws_memory_order_release || order == aws_memory_order_acq_rel || order == aws_memory_order_seq_cst) { |
| 40 |
__sync_synchronize(); |
| 41 |
} |
| 42 |
|
| 43 |
aws_atomic_private_compiler_barrier(); |
| 44 |
} |
| 45 |
|
| 46 |
static inline void aws_atomic_private_barrier_after(enum aws_memory_order order) { |
| 47 |
aws_atomic_private_compiler_barrier(); |
| 48 |
|
| 49 |
if (order == aws_memory_order_acquire || order == aws_memory_order_acq_rel || order == aws_memory_order_seq_cst) { |
| 50 |
__sync_synchronize(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Initializes an atomic variable with an integer value. This operation should be done before any |
| 56 |
* other operations on this atomic variable, and must be done before attempting any parallel operations. |
| 57 |
*/ |
| 58 |
AWS_STATIC_IMPL |
| 59 |
void aws_atomic_init_int(volatile struct aws_atomic_var *var, size_t n) { |
| 60 |
AWS_ATOMIC_VAR_INTVAL(var) = n; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Initializes an atomic variable with a pointer value. This operation should be done before any |
| 65 |
* other operations on this atomic variable, and must be done before attempting any parallel operations. |
| 66 |
*/ |
| 67 |
AWS_STATIC_IMPL |
| 68 |
void aws_atomic_init_ptr(volatile struct aws_atomic_var *var, void *p) { |
| 69 |
AWS_ATOMIC_VAR_PTRVAL(var) = p; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Reads an atomic var as an integer, using the specified ordering, and returns the result. |
| 74 |
*/ |
| 75 |
AWS_STATIC_IMPL |
| 76 |
size_t aws_atomic_load_int_explicit(volatile const struct aws_atomic_var *var, enum aws_memory_order memory_order) { |
| 77 |
aws_atomic_private_barrier_before(memory_order); |
| 78 |
|
| 79 |
size_t retval = AWS_ATOMIC_VAR_INTVAL(var); |
| 80 |
|
| 81 |
/* Release barriers are not permitted for loads, so we just do a compiler barrier here */ |
| 82 |
aws_atomic_private_compiler_barrier(); |
| 83 |
|
| 84 |
return retval; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Reads an atomic var as a pointer, using the specified ordering, and returns the result. |
| 89 |
*/ |
| 90 |
AWS_STATIC_IMPL |
| 91 |
void *aws_atomic_load_ptr_explicit(volatile const struct aws_atomic_var *var, enum aws_memory_order memory_order) { |
| 92 |
aws_atomic_private_barrier_before(memory_order); |
| 93 |
|
| 94 |
void *retval = AWS_ATOMIC_VAR_PTRVAL(var); |
| 95 |
|
| 96 |
/* Release barriers are not permitted for loads, so we just do a compiler barrier here */ |
| 97 |
aws_atomic_private_compiler_barrier(); |
| 98 |
|
| 99 |
return retval; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Stores an integer into an atomic var, using the specified ordering. |
| 104 |
*/ |
| 105 |
AWS_STATIC_IMPL |
| 106 |
void aws_atomic_store_int_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order memory_order) { |
| 107 |
/* Acquire barriers are not permitted for stores, so just do a compiler barrier before */ |
| 108 |
aws_atomic_private_compiler_barrier(); |
| 109 |
|
| 110 |
AWS_ATOMIC_VAR_INTVAL(var) = n; |
| 111 |
|
| 112 |
aws_atomic_private_barrier_after(memory_order); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Stores a pointer into an atomic var, using the specified ordering. |
| 117 |
*/ |
| 118 |
AWS_STATIC_IMPL |
| 119 |
void aws_atomic_store_ptr_explicit(volatile struct aws_atomic_var *var, void *p, enum aws_memory_order memory_order) { |
| 120 |
/* Acquire barriers are not permitted for stores, so just do a compiler barrier before */ |
| 121 |
aws_atomic_private_compiler_barrier(); |
| 122 |
|
| 123 |
AWS_ATOMIC_VAR_PTRVAL(var) = p; |
| 124 |
|
| 125 |
aws_atomic_private_barrier_after(memory_order); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Exchanges an integer with the value in an atomic_var, using the specified ordering. |
| 130 |
* Returns the value that was previously in the atomic_var. |
| 131 |
*/ |
| 132 |
AWS_STATIC_IMPL |
| 133 |
size_t aws_atomic_exchange_int_explicit( |
| 134 |
volatile struct aws_atomic_var *var, |
| 135 |
size_t n, |
| 136 |
enum aws_memory_order memory_order) { |
| 137 |
|
| 138 |
/* |
| 139 |
* GCC 4.6 and before have only __sync_lock_test_and_set as an exchange operation, |
| 140 |
* which may not support arbitrary values on all architectures. We simply emulate |
| 141 |
* with a CAS instead. |
| 142 |
*/ |
| 143 |
|
| 144 |
size_t oldval; |
| 145 |
do { |
| 146 |
oldval = AWS_ATOMIC_VAR_INTVAL(var); |
| 147 |
} while (!__sync_bool_compare_and_swap(&AWS_ATOMIC_VAR_INTVAL(var), oldval, n)); |
| 148 |
|
| 149 |
/* __sync_bool_compare_and_swap implies a full barrier */ |
| 150 |
|
| 151 |
return oldval; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Exchanges a pointer with the value in an atomic_var, using the specified ordering. |
| 156 |
* Returns the value that was previously in the atomic_var. |
| 157 |
*/ |
| 158 |
AWS_STATIC_IMPL |
| 159 |
void *aws_atomic_exchange_ptr_explicit( |
| 160 |
volatile struct aws_atomic_var *var, |
| 161 |
void *p, |
| 162 |
enum aws_memory_order memory_order) { |
| 163 |
|
| 164 |
/* |
| 165 |
* GCC 4.6 and before have only __sync_lock_test_and_set as an exchange operation, |
| 166 |
* which may not support arbitrary values on all architectures. We simply emulate |
| 167 |
* with a CAS instead. |
| 168 |
*/ |
| 169 |
void *oldval; |
| 170 |
do { |
| 171 |
oldval = AWS_ATOMIC_VAR_PTRVAL(var); |
| 172 |
} while (!__sync_bool_compare_and_swap(&AWS_ATOMIC_VAR_PTRVAL(var), oldval, p)); |
| 173 |
|
| 174 |
/* __sync_bool_compare_and_swap implies a full barrier */ |
| 175 |
|
| 176 |
return oldval; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Atomically compares *var to *expected; if they are equal, atomically sets *var = desired. Otherwise, *expected is set |
| 181 |
* to the value in *var. On success, the memory ordering used was order_success; otherwise, it was order_failure. |
| 182 |
* order_failure must be no stronger than order_success, and must not be release or acq_rel. |
| 183 |
*/ |
| 184 |
AWS_STATIC_IMPL |
| 185 |
bool aws_atomic_compare_exchange_int_explicit( |
| 186 |
volatile struct aws_atomic_var *var, |
| 187 |
size_t *expected, |
| 188 |
size_t desired, |
| 189 |
enum aws_memory_order order_success, |
| 190 |
enum aws_memory_order order_failure) { |
| 191 |
|
| 192 |
bool result = __sync_bool_compare_and_swap(&AWS_ATOMIC_VAR_INTVAL(var), *expected, desired); |
| 193 |
if (!result) { |
| 194 |
*expected = AWS_ATOMIC_VAR_INTVAL(var); |
| 195 |
} |
| 196 |
|
| 197 |
return result; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Atomically compares *var to *expected; if they are equal, atomically sets *var = desired. Otherwise, *expected is set |
| 202 |
* to the value in *var. On success, the memory ordering used was order_success; otherwise, it was order_failure. |
| 203 |
* order_failure must be no stronger than order_success, and must not be release or acq_rel. |
| 204 |
*/ |
| 205 |
AWS_STATIC_IMPL |
| 206 |
bool aws_atomic_compare_exchange_ptr_explicit( |
| 207 |
volatile struct aws_atomic_var *var, |
| 208 |
void **expected, |
| 209 |
void *desired, |
| 210 |
enum aws_memory_order order_success, |
| 211 |
enum aws_memory_order order_failure) { |
| 212 |
|
| 213 |
bool result = __sync_bool_compare_and_swap(&AWS_ATOMIC_VAR_PTRVAL(var), *expected, desired); |
| 214 |
if (!result) { |
| 215 |
*expected = AWS_ATOMIC_VAR_PTRVAL(var); |
| 216 |
} |
| 217 |
|
| 218 |
return result; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Atomically adds n to *var, and returns the previous value of *var. |
| 223 |
*/ |
| 224 |
AWS_STATIC_IMPL |
| 225 |
size_t aws_atomic_fetch_add_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) { |
| 226 |
return __sync_fetch_and_add(&AWS_ATOMIC_VAR_INTVAL(var), n); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Atomically subtracts n from *var, and returns the previous value of *var. |
| 231 |
*/ |
| 232 |
AWS_STATIC_IMPL |
| 233 |
size_t aws_atomic_fetch_sub_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) { |
| 234 |
return __sync_fetch_and_sub(&AWS_ATOMIC_VAR_INTVAL(var), n); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Atomically ORs n with *var, and returns the previous value of *var. |
| 239 |
*/ |
| 240 |
AWS_STATIC_IMPL |
| 241 |
size_t aws_atomic_fetch_or_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) { |
| 242 |
return __sync_fetch_and_or(&AWS_ATOMIC_VAR_INTVAL(var), n); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Atomically ANDs n with *var, and returns the previous value of *var. |
| 247 |
*/ |
| 248 |
AWS_STATIC_IMPL |
| 249 |
size_t aws_atomic_fetch_and_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) { |
| 250 |
return __sync_fetch_and_and(&AWS_ATOMIC_VAR_INTVAL(var), n); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Atomically XORs n with *var, and returns the previous value of *var. |
| 255 |
*/ |
| 256 |
AWS_STATIC_IMPL |
| 257 |
size_t aws_atomic_fetch_xor_explicit(volatile struct aws_atomic_var *var, size_t n, enum aws_memory_order order) { |
| 258 |
return __sync_fetch_and_xor(&AWS_ATOMIC_VAR_INTVAL(var), n); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Provides the same reordering guarantees as an atomic operation with the specified memory order, without |
| 263 |
* needing to actually perform an atomic operation. |
| 264 |
*/ |
| 265 |
AWS_STATIC_IMPL |
| 266 |
void aws_atomic_thread_fence(enum aws_memory_order order) { |
| 267 |
/* On old versions of GCC we only have this one big hammer... */ |
| 268 |
__sync_synchronize(); |
| 269 |
} |
| 270 |
|
| 271 |
#define AWS_ATOMICS_HAVE_THREAD_FENCE |
| 272 |
|
| 273 |
AWS_EXTERN_C_END |
| 274 |
#endif /* AWS_COMMON_ATOMICS_GNU_OLD_INL */ |