Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Include/internal/pycore_backoff.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ backoff_counter_triggers(_Py_BackoffCounter counter)
return counter.value_and_backoff < UNREACHABLE_BACKOFF;
}

static inline bool
backoff_counter_is_unreachable(_Py_BackoffCounter counter)
{
return (counter.value_and_backoff & BACKOFF_MASK) == UNREACHABLE_BACKOFF;
}

static inline _Py_BackoffCounter
trigger_backoff_counter(void)
{
Expand Down
1 change: 0 additions & 1 deletion Lib/test/test_thread_local_bytecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ def f(a, b, q=None):
""")
assert_python_ok("-X", "tlbc=1", "-c", code)

@support.skip_if_sanitizer("gh-129752: data race on adaptive counter", thread=True)
def test_no_copies_if_tlbc_disabled(self):
code = textwrap.dedent("""
import queue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Don't update adaptive counters in the free-threaded build when thread-local
bytecode is disabled (``-X tlbc=0``). Patch by Donghee Na.
18 changes: 18 additions & 0 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,23 @@ static void dtrace_function_return(_PyInterpreterFrame *);
#define ADAPTIVE_COUNTER_TRIGGERS(COUNTER) \
backoff_counter_triggers(forge_backoff_counter((COUNTER)))

#ifdef Py_GIL_DISABLED
/* Counters are unreachable when thread-local bytecode is disabled,
* so there is no need to update them. */
#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \
do { \
if (!backoff_counter_is_unreachable((COUNTER))) { \
(COUNTER) = advance_backoff_counter((COUNTER)); \
} \
} while (0);

#define PAUSE_ADAPTIVE_COUNTER(COUNTER) \
do { \
if (!backoff_counter_is_unreachable((COUNTER))) { \
(COUNTER) = pause_backoff_counter((COUNTER)); \
} \
} while (0);
#else
#define ADVANCE_ADAPTIVE_COUNTER(COUNTER) \
do { \
(COUNTER) = advance_backoff_counter((COUNTER)); \
Expand All @@ -363,6 +380,7 @@ static void dtrace_function_return(_PyInterpreterFrame *);
do { \
(COUNTER) = pause_backoff_counter((COUNTER)); \
} while (0);
#endif

#ifdef ENABLE_SPECIALIZATION
/* Multiple threads may execute these concurrently if thread-local bytecode is
Expand Down
Loading