]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112535: Implement fallback implementation of _Py_ThreadId() (gh-113185)
authorDonghee Na <donghee.na@python.org>
Mon, 18 Dec 2023 16:54:49 +0000 (16:54 +0000)
committerGitHub <noreply@github.com>
Mon, 18 Dec 2023 16:54:49 +0000 (16:54 +0000)
---------

Co-authored-by: Sam Gross <colesbury@gmail.com>
Include/object.h
Python/pystate.c

index d22e5c2b8be2a9d19ee129d82d000eceb1eb90c3..48f1ddf7510887ddc390fe3b1223177b2d234259 100644 (file)
@@ -239,6 +239,8 @@ PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
 #define Py_Is(x, y) ((x) == (y))
 
 #if defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
+PyAPI_FUNC(uintptr_t) _Py_GetThreadLocal_Addr(void);
+
 static inline uintptr_t
 _Py_ThreadId(void)
 {
@@ -291,7 +293,9 @@ _Py_ThreadId(void)
     __asm__ ("mv %0, tp" : "=r" (tid));
     #endif
 #else
-  # error "define _Py_ThreadId for this platform"
+    // Fallback to a portable implementation if we do not have a faster
+    // platform-specific implementation.
+    tid = _Py_GetThreadLocal_Addr();
 #endif
   return tid;
 }
index e18eb0186d0010317d3d0371e40659f44834ac07..632a119ea6d4f8525e1a05192fae7c01a9f8fe67 100644 (file)
@@ -1951,6 +1951,20 @@ _PyThreadState_Bind(PyThreadState *tstate)
     }
 }
 
+#if defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
+uintptr_t
+_Py_GetThreadLocal_Addr(void)
+{
+#ifdef HAVE_THREAD_LOCAL
+    // gh-112535: Use the address of the thread-local PyThreadState variable as
+    // a unique identifier for the current thread. Each thread has a unique
+    // _Py_tss_tstate variable with a unique address.
+    return (uintptr_t)&_Py_tss_tstate;
+#else
+#  error "no supported thread-local variable storage classifier"
+#endif
+}
+#endif
 
 /***********************************/
 /* routines for advanced debuggers */