Skip to content

Commit d644555

Browse files
committed
gh-129752: Don't update adaptive counters when TLBC=0 in free-threading.
1 parent e4b22ad commit d644555

4 files changed

Lines changed: 26 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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,23 @@ 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+
if (!backoff_counter_is_unreachable((COUNTER))) { \
363+
(COUNTER) = advance_backoff_counter((COUNTER)); \
364+
} \
365+
} while (0);
366+
367+
#define PAUSE_ADAPTIVE_COUNTER(COUNTER) \
368+
do { \
369+
if (!backoff_counter_is_unreachable((COUNTER))) { \
370+
(COUNTER) = pause_backoff_counter((COUNTER)); \
371+
} \
372+
} while (0);
373+
#else
357374
#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \
358375
do { \
359376
(COUNTER) = advance_backoff_counter((COUNTER)); \
@@ -363,6 +380,7 @@ static void dtrace_function_return(_PyInterpreterFrame *);
363380
do { \
364381
(COUNTER) = pause_backoff_counter((COUNTER)); \
365382
} while (0);
383+
#endif
366384

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

0 commit comments

Comments
 (0)