Skip to content

Commit d00dbf5

Browse files
corona10colesbury
andauthored
gh-112535: Implement fallback implementation of _Py_ThreadId() (gh-113185)
--------- Co-authored-by: Sam Gross <[email protected]>
1 parent 59f0766 commit d00dbf5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Include/object.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
239239
#define Py_Is(x, y) ((x) == (y))
240240

241241
#if defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
242+
PyAPI_FUNC(uintptr_t) _Py_GetThreadLocal_Addr(void);
243+
242244
static inline uintptr_t
243245
_Py_ThreadId(void)
244246
{
@@ -291,7 +293,9 @@ _Py_ThreadId(void)
291293
__asm__ ("mv %0, tp" : "=r" (tid));
292294
#endif
293295
#else
294-
# error "define _Py_ThreadId for this platform"
296+
// Fallback to a portable implementation if we do not have a faster
297+
// platform-specific implementation.
298+
tid = _Py_GetThreadLocal_Addr();
295299
#endif
296300
return tid;
297301
}

Python/pystate.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,20 @@ _PyThreadState_Bind(PyThreadState *tstate)
19511951
}
19521952
}
19531953

1954+
#if defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
1955+
uintptr_t
1956+
_Py_GetThreadLocal_Addr(void)
1957+
{
1958+
#ifdef HAVE_THREAD_LOCAL
1959+
// gh-112535: Use the address of the thread-local PyThreadState variable as
1960+
// a unique identifier for the current thread. Each thread has a unique
1961+
// _Py_tss_tstate variable with a unique address.
1962+
return (uintptr_t)&_Py_tss_tstate;
1963+
#else
1964+
# error "no supported thread-local variable storage classifier"
1965+
#endif
1966+
}
1967+
#endif
19541968

19551969
/***********************************/
19561970
/* routines for advanced debuggers */

0 commit comments

Comments
 (0)