Uh oh!
There was an error while loading. Please reload this page.
gh-112535: Implement fallback implementation of _Py_ThreadId() - #113185
Conversation
corona10
commented
Dec 15, 2023
I reopened the PR, since there was some accident with #113094 diff --git a/Include/object.h b/Include/object.h
index 6afdc1688e..d31df80db0 100644
--- a/Include/object.h+++ b/Include/object.h@@ -245,54 +245,7 @@ static inline uintptr_t
_Py_ThreadId(void)
{
uintptr_t tid;
-#if defined(_MSC_VER) && defined(_M_X64)- tid = __readgsqword(48);-#elif defined(_MSC_VER) && defined(_M_IX86)- tid = __readfsdword(24);-#elif defined(_MSC_VER) && defined(_M_ARM64)- tid = __getReg(18);-#elif defined(__i386__)- __asm__("movl %%gs:0, %0" : "=r" (tid)); // 32-bit always uses GS-#elif defined(__MACH__) && defined(__x86_64__)- __asm__("movq %%gs:0, %0" : "=r" (tid)); // x86_64 macOSX uses GS-#elif defined(__x86_64__)- __asm__("movq %%fs:0, %0" : "=r" (tid)); // x86_64 Linux, BSD uses FS-#elif defined(__arm__)- __asm__ ("mrc p15, 0, %0, c13, c0, 3\nbic %0, %0, #3" : "=r" (tid));-#elif defined(__aarch64__) && defined(__APPLE__)- __asm__ ("mrs %0, tpidrro_el0" : "=r" (tid));-#elif defined(__aarch64__)- __asm__ ("mrs %0, tpidr_el0" : "=r" (tid));-#elif defined(__powerpc64__)- #if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)- tid = (uintptr_t)__builtin_thread_pointer();- #else- // r13 is reserved for use as system thread ID by the Power 64-bit ABI.- register uintptr_t tp __asm__ ("r13");- __asm__("" : "=r" (tp));- tid = tp;- #endif-#elif defined(__powerpc__)- #if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)- tid = (uintptr_t)__builtin_thread_pointer();- #else- // r2 is reserved for use as system thread ID by the Power 32-bit ABI.- register uintptr_t tp __asm__ ("r2");- __asm__ ("" : "=r" (tp));- tid = tp;- #endif-#elif defined(__s390__) && defined(__GNUC__)- // Both GCC and Clang have supported __builtin_thread_pointer- // for s390 from long time ago.- tid = (uintptr_t)__builtin_thread_pointer();-#elif defined(__riscv)- #if defined(__clang__) && _Py__has_builtin(__builtin_thread_pointer)- tid = (uintptr_t)__builtin_thread_pointer();- #else- // tp is Thread Pointer provided by the RISC-V ABI.- __asm__ ("mv %0, tp" : "=r" (tid));- #endif-#elif defined(thread_local) || defined(__GNUC__)+#if defined(thread_local) || defined(__GNUC__)
// gh-112535: Using characteristics of TLS address mapping.
// The address of the thread-local variable is not equal with the actual thread pointer,
// However, it has the property of being determined by loader at runtime, with no duplication of values |
colesbury
left a comment
There was a problem hiding this comment.
Some suggestions on code comments below. I think we should put any detailed explanations of _Py_GetThreadLocal_Addr near the implementation (and not where it's called in _Py_ThreadId().
I don't think there's a lot we can say generally about the implementation of &_Py_tss_tstate -- it depends on the platform, but adding a constant offset is generally not expensive. There are two common costs:
- Calling
_Py_GetThreadLocal_Addr(), which mostly can't be inlined. - Depending on the implementation of TLS, computing the address may or may not require another function call.
| // tp is Thread Pointer provided by the RISC-V ABI. | ||
| __asm__ ("mv %0, tp" : "=r" (tid)); | ||
| #endif | ||
| #elif defined(thread_local) || defined(__GNUC__) |
There was a problem hiding this comment.
I think this should just be an #else. We're not using thread_local or depending on GNU specific features here in object.h.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Sam Gross <colesbury@gmail.com>
…ythongh-113185) --------- Co-authored-by: Sam Gross <colesbury@gmail.com>
…ythongh-113185) --------- Co-authored-by: Sam Gross <colesbury@gmail.com>
…ythongh-113185) --------- Co-authored-by: Sam Gross <colesbury@gmail.com>
_Py_ThreadId()work on PowerPC, IBM Z, etc. #112535