|
4 | 4 | // Operations are sequentially consistent unless they have a suffix indicating |
5 | 5 | // otherwise. If in doubt, prefer the sequentially consistent operations. |
6 | 6 | // |
7 | | -// The "_relaxed" suffix for load and store operations indicates the "relaxed" |
8 | | -// memory order. They don't provide synchronization, but (roughly speaking) |
9 | | -// guarantee somewhat sane behavior for races instead of undefined behavior. |
10 | | -// In practice, they correspond to "normal" hardware load and store |
| 7 | +// The "_relaxed" suffix indicates the "relaxed" memory order. Relaxed |
| 8 | +// operations don't provide synchronization, but (roughly speaking) guarantee |
| 9 | +// somewhat sane behavior for races instead of undefined behavior. In practice, |
| 10 | +// relaxed loads and stores correspond to "normal" hardware load and store |
11 | 11 | // instructions, so they are almost as inexpensive as plain loads and stores |
12 | | -// in C. |
| 12 | +// in C. Relaxed read-modify-write operations, such as |
| 13 | +// _Py_atomic_add_*_relaxed, are still atomic, but do not order surrounding |
| 14 | +// memory accesses. |
13 | 15 | // |
14 | 16 | // Note that atomic read-modify-write operations like _Py_atomic_add_* return |
15 | 17 | // the previous value of the atomic variable, not the new value. |
|
49 | 51 | // expected = obj |
50 | 52 | // return False |
51 | 53 | // |
| 54 | +// def _Py_atomic_compare_exchange_relaxed(obj, expected, desired): |
| 55 | +// # relaxed consistency |
| 56 | +// if obj == expected: |
| 57 | +// obj = desired |
| 58 | +// return True |
| 59 | +// else: |
| 60 | +// expected = obj |
| 61 | +// return False |
| 62 | +// |
52 | 63 | // def _Py_atomic_add(obj, value): |
53 | 64 | // # sequential consistency |
54 | 65 | // old_obj = obj |
55 | 66 | // obj += value |
56 | 67 | // return old_obj |
57 | 68 | // |
| 69 | +// def _Py_atomic_add_relaxed(obj, value): |
| 70 | +// # relaxed consistency |
| 71 | +// old_obj = obj |
| 72 | +// obj += value |
| 73 | +// return old_obj |
| 74 | +// |
58 | 75 | // def _Py_atomic_and(obj, value): |
59 | 76 | // # sequential consistency |
60 | 77 | // old_obj = obj |
@@ -130,6 +147,50 @@ static inline Py_ssize_t |
130 | 147 | _Py_atomic_add_ssize(Py_ssize_t *obj, Py_ssize_t value); |
131 | 148 |
|
132 | 149 |
|
| 150 | +// --- _Py_atomic_add_relaxed ------------------------------------------------ |
| 151 | +// Atomically adds `value` to `obj` and returns the previous value |
| 152 | +// (relaxed consistency, i.e., no ordering) |
| 153 | + |
| 154 | +static inline int |
| 155 | +_Py_atomic_add_int_relaxed(int *obj, int value); |
| 156 | + |
| 157 | +static inline int8_t |
| 158 | +_Py_atomic_add_int8_relaxed(int8_t *obj, int8_t value); |
| 159 | + |
| 160 | +static inline int16_t |
| 161 | +_Py_atomic_add_int16_relaxed(int16_t *obj, int16_t value); |
| 162 | + |
| 163 | +static inline int32_t |
| 164 | +_Py_atomic_add_int32_relaxed(int32_t *obj, int32_t value); |
| 165 | + |
| 166 | +static inline int64_t |
| 167 | +_Py_atomic_add_int64_relaxed(int64_t *obj, int64_t value); |
| 168 | + |
| 169 | +static inline intptr_t |
| 170 | +_Py_atomic_add_intptr_relaxed(intptr_t *obj, intptr_t value); |
| 171 | + |
| 172 | +static inline unsigned int |
| 173 | +_Py_atomic_add_uint_relaxed(unsigned int *obj, unsigned int value); |
| 174 | + |
| 175 | +static inline uint8_t |
| 176 | +_Py_atomic_add_uint8_relaxed(uint8_t *obj, uint8_t value); |
| 177 | + |
| 178 | +static inline uint16_t |
| 179 | +_Py_atomic_add_uint16_relaxed(uint16_t *obj, uint16_t value); |
| 180 | + |
| 181 | +static inline uint32_t |
| 182 | +_Py_atomic_add_uint32_relaxed(uint32_t *obj, uint32_t value); |
| 183 | + |
| 184 | +static inline uint64_t |
| 185 | +_Py_atomic_add_uint64_relaxed(uint64_t *obj, uint64_t value); |
| 186 | + |
| 187 | +static inline uintptr_t |
| 188 | +_Py_atomic_add_uintptr_relaxed(uintptr_t *obj, uintptr_t value); |
| 189 | + |
| 190 | +static inline Py_ssize_t |
| 191 | +_Py_atomic_add_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t value); |
| 192 | + |
| 193 | + |
133 | 194 | // --- _Py_atomic_compare_exchange ------------------------------------------- |
134 | 195 | // Performs an atomic compare-and-exchange. |
135 | 196 | // |
@@ -185,6 +246,64 @@ static inline int |
185 | 246 | _Py_atomic_compare_exchange_ptr(void *obj, void *expected, void *value); |
186 | 247 |
|
187 | 248 |
|
| 249 | +// --- _Py_atomic_compare_exchange_relaxed ----------------------------------- |
| 250 | +// Performs an atomic compare-and-exchange (relaxed consistency, i.e., no |
| 251 | +// ordering on either success or failure). |
| 252 | +// |
| 253 | +// - If `*obj` and `*expected` are equal, store `desired` into `*obj` |
| 254 | +// and return 1 (success). |
| 255 | +// - Otherwise, store the `*obj` current value into `*expected` |
| 256 | +// and return 0 (failure). |
| 257 | +// |
| 258 | +// These correspond to the C11 atomic_compare_exchange_strong_explicit() |
| 259 | +// function with memory_order_relaxed for both the success and failure |
| 260 | +// memory orders. |
| 261 | + |
| 262 | +static inline int |
| 263 | +_Py_atomic_compare_exchange_int_relaxed(int *obj, int *expected, int desired); |
| 264 | + |
| 265 | +static inline int |
| 266 | +_Py_atomic_compare_exchange_int8_relaxed(int8_t *obj, int8_t *expected, int8_t desired); |
| 267 | + |
| 268 | +static inline int |
| 269 | +_Py_atomic_compare_exchange_int16_relaxed(int16_t *obj, int16_t *expected, int16_t desired); |
| 270 | + |
| 271 | +static inline int |
| 272 | +_Py_atomic_compare_exchange_int32_relaxed(int32_t *obj, int32_t *expected, int32_t desired); |
| 273 | + |
| 274 | +static inline int |
| 275 | +_Py_atomic_compare_exchange_int64_relaxed(int64_t *obj, int64_t *expected, int64_t desired); |
| 276 | + |
| 277 | +static inline int |
| 278 | +_Py_atomic_compare_exchange_intptr_relaxed(intptr_t *obj, intptr_t *expected, intptr_t desired); |
| 279 | + |
| 280 | +static inline int |
| 281 | +_Py_atomic_compare_exchange_uint_relaxed(unsigned int *obj, unsigned int *expected, unsigned int desired); |
| 282 | + |
| 283 | +static inline int |
| 284 | +_Py_atomic_compare_exchange_uint8_relaxed(uint8_t *obj, uint8_t *expected, uint8_t desired); |
| 285 | + |
| 286 | +static inline int |
| 287 | +_Py_atomic_compare_exchange_uint16_relaxed(uint16_t *obj, uint16_t *expected, uint16_t desired); |
| 288 | + |
| 289 | +static inline int |
| 290 | +_Py_atomic_compare_exchange_uint32_relaxed(uint32_t *obj, uint32_t *expected, uint32_t desired); |
| 291 | + |
| 292 | +static inline int |
| 293 | +_Py_atomic_compare_exchange_uint64_relaxed(uint64_t *obj, uint64_t *expected, uint64_t desired); |
| 294 | + |
| 295 | +static inline int |
| 296 | +_Py_atomic_compare_exchange_uintptr_relaxed(uintptr_t *obj, uintptr_t *expected, uintptr_t desired); |
| 297 | + |
| 298 | +static inline int |
| 299 | +_Py_atomic_compare_exchange_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t *expected, Py_ssize_t desired); |
| 300 | + |
| 301 | +// NOTE: `obj` and `expected` are logically `void**` types, but we use `void*` |
| 302 | +// so that we can pass types like `PyObject**` without a cast. |
| 303 | +static inline int |
| 304 | +_Py_atomic_compare_exchange_ptr_relaxed(void *obj, void *expected, void *value); |
| 305 | + |
| 306 | + |
188 | 307 | // --- _Py_atomic_exchange --------------------------------------------------- |
189 | 308 | // Atomically replaces `*obj` with `value` and returns the previous value of `*obj`. |
190 | 309 |
|
|
0 commit comments