Skip to content

Commit befac58

Browse files
committed
[3.14] gh-129752: Don't update adaptive counters when TLBC=0 in free-threading. (gh-155497)
(cherry picked from commit ee4fe00) Co-authored-by: Donghee Na <donghee.na@python.org>
1 parent 83197a5 commit befac58

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

Lib/test/test_thread_local_bytecode.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ def f(a, b, q=None):
108108
""")
109109
assert_python_ok("-X", "tlbc=1", "-c", code)
110110

111-
@support.skip_if_sanitizer("gh-129752: data race on adaptive counter", thread=True)
112111
def test_no_copies_if_tlbc_disabled(self):
113112
code = textwrap.dedent("""
114113
import queue
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Don't update adaptive counters in the free-threaded build when thread-local
2+
bytecode is disabled (``-X tlbc=0``). Patch by Donghee Na.

Python/ceval_macros.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,25 @@ GETITEM(PyObject *v, Py_ssize_t i) {
285285
#define ADAPTIVE_COUNTER_TRIGGERS(COUNTER) \
286286
backoff_counter_triggers(forge_backoff_counter((COUNTER)))
287287

288+
#ifdef Py_GIL_DISABLED
289+
/* Counters are unreachable when thread-local bytecode is disabled,
290+
* so there is no need to update them. */
291+
#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \
292+
do { \
293+
_Py_BackoffCounter cnt = (COUNTER); \
294+
if (!is_unreachable_backoff_counter(cnt)) { \
295+
(COUNTER) = advance_backoff_counter(cnt); \
296+
} \
297+
} while (0);
298+
299+
#define PAUSE_ADAPTIVE_COUNTER(COUNTER) \
300+
do { \
301+
_Py_BackoffCounter cnt = (COUNTER); \
302+
if (!is_unreachable_backoff_counter(cnt)) { \
303+
(COUNTER) = pause_backoff_counter(cnt); \
304+
} \
305+
} while (0);
306+
#else
288307
#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \
289308
do { \
290309
(COUNTER) = advance_backoff_counter((COUNTER)); \
@@ -294,6 +313,7 @@ GETITEM(PyObject *v, Py_ssize_t i) {
294313
do { \
295314
(COUNTER) = pause_backoff_counter((COUNTER)); \
296315
} while (0);
316+
#endif
297317

298318
#ifdef ENABLE_SPECIALIZATION_FT
299319
/* Multiple threads may execute these concurrently if thread-local bytecode is

0 commit comments

Comments
 (0)