Skip to content

Commit ad2b0ac

Browse files
authored
gh-155628: Add _Py_atomic_add_*_relaxed variants (#155630)
* Add relaxed _Py_atomic_add_* Add _Py_atomic_add_*_relaxed() for all arithmetic types supported by _Py_atomic_add_*(). The existing add operations are sequentially consistent, which is stronger (and on ARM, measurably more expensive) than necessary for uses like statistics counters and unique ID allocation, where the add must be atomic but does not need to order surrounding memory accesses. The GCC/Clang backend uses __atomic_fetch_add() with __ATOMIC_RELAXED, and the standard C11/C++11 backend uses atomic_fetch_add_explicit() with memory_order_relaxed. The MSVC backend uses the _InterlockedExchangeAdd*_nf ("no fence") intrinsics on ARM64; on x86 and x64 those intrinsics do not exist, so it falls back to the plain interlocked intrinsics, whose stronger ordering is a conforming implementation of relaxed (x86 has no cheaper atomic read-modify-write). As with the sequentially consistent version, 64-bit adds on 32-bit x86 fall back to a compare-exchange loop. * Use a relaxed add for the module index allocator * Use relaxed adds for the GC allocation counter * Use relaxed adds for the lru_cache hit/miss counters * Use a relaxed add for the asyncio task name counter * Add relaxed _Py_atomic_compare_exchange * Use relaxed compare_exchange for gc young count
1 parent 572ec16 commit ad2b0ac

12 files changed

Lines changed: 861 additions & 18 deletions

File tree

‎Include/cpython/pyatomic.h‎

Lines changed: 124 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
// Operations are sequentially consistent unless they have a suffix indicating
55
// otherwise. If in doubt, prefer the sequentially consistent operations.
66
//
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
1111
// 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.
1315
//
1416
// Note that atomic read-modify-write operations like _Py_atomic_add_* return
1517
// the previous value of the atomic variable, not the new value.
@@ -49,12 +51,27 @@
4951
// expected = obj
5052
// return False
5153
//
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+
//
5263
// def _Py_atomic_add(obj, value):
5364
// # sequential consistency
5465
// old_obj = obj
5566
// obj += value
5667
// return old_obj
5768
//
69+
// def _Py_atomic_add_relaxed(obj, value):
70+
// # relaxed consistency
71+
// old_obj = obj
72+
// obj += value
73+
// return old_obj
74+
//
5875
// def _Py_atomic_and(obj, value):
5976
// # sequential consistency
6077
// old_obj = obj
@@ -130,6 +147,50 @@ static inline Py_ssize_t
130147
_Py_atomic_add_ssize(Py_ssize_t *obj, Py_ssize_t value);
131148

132149

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+
133194
// --- _Py_atomic_compare_exchange -------------------------------------------
134195
// Performs an atomic compare-and-exchange.
135196
//
@@ -185,6 +246,64 @@ static inline int
185246
_Py_atomic_compare_exchange_ptr(void *obj, void *expected, void *value);
186247

187248

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+
188307
// --- _Py_atomic_exchange ---------------------------------------------------
189308
// Atomically replaces `*obj` with `value` and returns the previous value of `*obj`.
190309

‎Include/cpython/pyatomic_gcc.h‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,61 @@ _Py_atomic_add_ssize(Py_ssize_t *obj, Py_ssize_t value)
6363
{ return __atomic_fetch_add(obj, value, __ATOMIC_SEQ_CST); }
6464

6565

66+
// --- _Py_atomic_add_relaxed ------------------------------------------------
67+
68+
static inline int
69+
_Py_atomic_add_int_relaxed(int *obj, int value)
70+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
71+
72+
static inline int8_t
73+
_Py_atomic_add_int8_relaxed(int8_t *obj, int8_t value)
74+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
75+
76+
static inline int16_t
77+
_Py_atomic_add_int16_relaxed(int16_t *obj, int16_t value)
78+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
79+
80+
static inline int32_t
81+
_Py_atomic_add_int32_relaxed(int32_t *obj, int32_t value)
82+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
83+
84+
static inline int64_t
85+
_Py_atomic_add_int64_relaxed(int64_t *obj, int64_t value)
86+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
87+
88+
static inline intptr_t
89+
_Py_atomic_add_intptr_relaxed(intptr_t *obj, intptr_t value)
90+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
91+
92+
static inline unsigned int
93+
_Py_atomic_add_uint_relaxed(unsigned int *obj, unsigned int value)
94+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
95+
96+
static inline uint8_t
97+
_Py_atomic_add_uint8_relaxed(uint8_t *obj, uint8_t value)
98+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
99+
100+
static inline uint16_t
101+
_Py_atomic_add_uint16_relaxed(uint16_t *obj, uint16_t value)
102+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
103+
104+
static inline uint32_t
105+
_Py_atomic_add_uint32_relaxed(uint32_t *obj, uint32_t value)
106+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
107+
108+
static inline uint64_t
109+
_Py_atomic_add_uint64_relaxed(uint64_t *obj, uint64_t value)
110+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
111+
112+
static inline uintptr_t
113+
_Py_atomic_add_uintptr_relaxed(uintptr_t *obj, uintptr_t value)
114+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
115+
116+
static inline Py_ssize_t
117+
_Py_atomic_add_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t value)
118+
{ return __atomic_fetch_add(obj, value, __ATOMIC_RELAXED); }
119+
120+
66121
// --- _Py_atomic_compare_exchange -------------------------------------------
67122

68123
static inline int
@@ -136,6 +191,79 @@ _Py_atomic_compare_exchange_ptr(void *obj, void *expected, void *desired)
136191
__ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); }
137192

138193

194+
// --- _Py_atomic_compare_exchange_relaxed -----------------------------------
195+
196+
static inline int
197+
_Py_atomic_compare_exchange_int_relaxed(int *obj, int *expected, int desired)
198+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
199+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
200+
201+
static inline int
202+
_Py_atomic_compare_exchange_int8_relaxed(int8_t *obj, int8_t *expected, int8_t desired)
203+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
204+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
205+
206+
static inline int
207+
_Py_atomic_compare_exchange_int16_relaxed(int16_t *obj, int16_t *expected, int16_t desired)
208+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
209+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
210+
211+
static inline int
212+
_Py_atomic_compare_exchange_int32_relaxed(int32_t *obj, int32_t *expected, int32_t desired)
213+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
214+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
215+
216+
static inline int
217+
_Py_atomic_compare_exchange_int64_relaxed(int64_t *obj, int64_t *expected, int64_t desired)
218+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
219+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
220+
221+
static inline int
222+
_Py_atomic_compare_exchange_intptr_relaxed(intptr_t *obj, intptr_t *expected, intptr_t desired)
223+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
224+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
225+
226+
static inline int
227+
_Py_atomic_compare_exchange_uint_relaxed(unsigned int *obj, unsigned int *expected, unsigned int desired)
228+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
229+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
230+
231+
static inline int
232+
_Py_atomic_compare_exchange_uint8_relaxed(uint8_t *obj, uint8_t *expected, uint8_t desired)
233+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
234+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
235+
236+
static inline int
237+
_Py_atomic_compare_exchange_uint16_relaxed(uint16_t *obj, uint16_t *expected, uint16_t desired)
238+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
239+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
240+
241+
static inline int
242+
_Py_atomic_compare_exchange_uint32_relaxed(uint32_t *obj, uint32_t *expected, uint32_t desired)
243+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
244+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
245+
246+
static inline int
247+
_Py_atomic_compare_exchange_uint64_relaxed(uint64_t *obj, uint64_t *expected, uint64_t desired)
248+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
249+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
250+
251+
static inline int
252+
_Py_atomic_compare_exchange_uintptr_relaxed(uintptr_t *obj, uintptr_t *expected, uintptr_t desired)
253+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
254+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
255+
256+
static inline int
257+
_Py_atomic_compare_exchange_ssize_relaxed(Py_ssize_t *obj, Py_ssize_t *expected, Py_ssize_t desired)
258+
{ return __atomic_compare_exchange_n(obj, expected, desired, 0,
259+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
260+
261+
static inline int
262+
_Py_atomic_compare_exchange_ptr_relaxed(void *obj, void *expected, void *desired)
263+
{ return __atomic_compare_exchange_n((void **)obj, (void **)expected, desired, 0,
264+
__ATOMIC_RELAXED, __ATOMIC_RELAXED); }
265+
266+
139267
// --- _Py_atomic_exchange ---------------------------------------------------
140268

141269
static inline int

0 commit comments

Comments
 (0)