gh-129752: Don't update adaptive counters when TLBC=0 in free-threadi… · python/cpython@ee4fe00

GitHub

GitHub CopilotWrite better code with AI | MCP RegistryIntegrate external tools | ActionsAutomate any workflow | CodespacesInstant dev environments | IssuesPlan and track work | Code ReviewManage code changes | Code QualityEnforce quality at merge | Why GitHub | Marketplace | View all features | Enterprises | Small and medium teams | Startups | View all use cases | View all industries | View all solutions | AI | Software Development | DevOps | Security | View all topics | Customer stories | Events & webinars | Ebooks & reports | Business insights | Trust center | Partners | View all resources

File tree

Include/internal

Lib/test

Misc/NEWS.d/next/Core_and_Builtins

Python

Original file line numberDiff line numberDiff line change@@ -108,6 +108,12 @@ backoff_counter_triggers(_Py_BackoffCounter counter)

108108returncounter.value_and_backoff<UNREACHABLE_BACKOFF;

109109}

110110111+staticinlinebool

112+backoff_counter_is_unreachable(_Py_BackoffCountercounter)

113+{

114+return (counter.value_and_backoff&BACKOFF_MASK) ==UNREACHABLE_BACKOFF;

115+}

116+111117staticinline_Py_BackoffCounter

112118trigger_backoff_counter(void)

113119{

Original file line numberDiff line numberDiff line change@@ -108,7 +108,6 @@ def f(a, b, q=None):

108108 """)

109109assert_python_ok("-X", "tlbc=1", "-c", code)

[email protected]_if_sanitizer("gh-129752: data race on adaptive counter", thread=True)

112111deftest_no_copies_if_tlbc_disabled(self):

113112code=textwrap.dedent("""

114113 import queue

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.

Original file line numberDiff line numberDiff line change@@ -354,6 +354,25 @@ static void dtrace_function_return(_PyInterpreterFrame *);

354354#defineADAPTIVE_COUNTER_TRIGGERS(COUNTER) \

355355 backoff_counter_triggers(forge_backoff_counter((COUNTER)))

356356357+#ifdefPy_GIL_DISABLED

358+/* Counters are unreachable when thread-local bytecode is disabled,

359+ * so there is no need to update them. */

360+#defineADVANCE_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+#definePAUSE_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#defineADVANCE_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

366386367387#ifdefENABLE_SPECIALIZATION

368388/* Multiple threads may execute these concurrently if thread-local bytecode is