-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-112535: Implement fallback implementation of _Py_ThreadId() #113185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Include/object.h
Outdated
@@ -290,6 +292,13 @@ _Py_ThreadId(void) | |||
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should just be an #else
. We're not using thread_local
or depending on GNU specific features here in object.h.
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