Skip to content

Commit ee4fe00

Browse files
authored
pythongh-129752: Don't update adaptive counters when TLBC=0 in free-threading. (pythongh-155497)
1 parent bc31217 commit ee4fe00

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

Include/internal/pycore_backoff.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ backoff_counter_triggers(_Py_BackoffCounter counter)
108108
return counter.value_and_backoff < UNREACHABLE_BACKOFF;
109109
}
110110

111+
static inline bool
112+
backoff_counter_is_unreachable(_Py_BackoffCounter counter)
113+
{
114+
return (counter.value_and_backoff & BACKOFF_MASK) == UNREACHABLE_BACKOFF;
115+
}
116+
111117
static inline _Py_BackoffCounter
112118
trigger_backoff_counter(void)
113119
{

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
@@ -354,6 +354,25 @@ static void dtrace_function_return(_PyInterpreterFrame *);
354354
#define ADAPTIVE_COUNTER_TRIGGERS(COUNTER) \
355355
backoff_counter_triggers(forge_backoff_counter((COUNTER)))
356356

357+
#ifdef Py_GIL_DISABLED
358+
/* Counters are unreachable when thread-local bytecode is disabled,
359+
* so there is no need to update them. */
360+
#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \
361+
do { \
362+
_Py_BackoffCounter cnt = (COUNTER); \
363+
if (!backoff_counter_is_unreachable(cnt)) { \
364+
(COUNTER) = advance_backoff_counter(cnt); \
365+
} \
366+
} while (0);
367+
368+
#define PAUSE_ADAPTIVE_COUNTER(COUNTER) \
369+
do { \
370+
_Py_BackoffCounter cnt = (COUNTER); \
371+
if (!backoff_counter_is_unreachable(cnt)) { \
372+
(COUNTER) = pause_backoff_counter(cnt); \
373+
} \
374+
} while (0);
375+
#else
357376
#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \
358377
do { \
359378
(COUNTER) = advance_backoff_counter((COUNTER)); \
@@ -363,6 +382,7 @@ static void dtrace_function_return(_PyInterpreterFrame *);
363382
do { \
364383
(COUNTER) = pause_backoff_counter((COUNTER)); \
365384
} while (0);
385+
#endif
366386

367387
#ifdef ENABLE_SPECIALIZATION
368388
/* Multiple threads may execute these concurrently if thread-local bytecode is

0 commit comments

Comments
 (0)