---------
Co-authored-by: Sam Gross <colesbury@gmail.com>
#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)
{
__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;
}
}
}
+#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 */