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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down