Skip to content

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

Merged
merged 4 commits into from
Dec 18, 2023

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Dec 15, 2023

@corona10
Copy link
Member Author

I reopened the PR, since there was some accident with #113094
As same as before, I checked that the fallback logic works correctly on my local machine.

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

Copy link
Contributor

@colesbury colesbury left a 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:

  1. Calling _Py_GetThreadLocal_Addr(), which mostly can't be inlined.
  2. 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__)
Copy link
Contributor

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.

corona10 and others added 2 commits December 19, 2023 01:27
Co-authored-by: Sam Gross <colesbury@gmail.com>
@corona10 corona10 enabled auto-merge (squash) December 18, 2023 16:32
@corona10 corona10 merged commit d00dbf5 into python:main Dec 18, 2023
@corona10 corona10 deleted the gh-112535-fallback branch December 18, 2023 16:55
ryan-duve pushed a commit to ryan-duve/cpython that referenced this pull request Dec 26, 2023
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants