Closed
Description
Feature or enhancement
Currently, _Py_ThreadId()
is only defined in Py_GIL_DISABLED
builds and does not compile on PowerPC or IBM Z. It would be convenient to make the functionality available in general: it will be useful to implement thread-safe recursive locks. (Currently our recursive lock implementations rely on the GIL for thread-safety.)
There are two parts to this:
- We should use
__builtin_thread_pointer()
when available - We should fall back to a function that returns the address of a thread-local variable if no other fast implementation is available (instead of erroring out).
The fallback (2) will be a bit slower, but it provides a portable implementation.
Note that checking if __builtin_thread_pointer()
is available is not trivial. __has_builtin(__builtin_thread_pointer)
is not useful (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96952). You need some combination of platform and GCC/clang version check.
Linked PRs
- gh-112535: Update _Py_ThreadId() to support PowerPC, IBM Z, etc #112553
- gh-112535: Update _Py_ThreadId() to support PowerPC #112624
- gh-112535: Add test on _Py_ThreadId() #112709
- gh-112535: Add comment for ppc32/64 registers #112746
- gh-112535: Update _Py_ThreadId() to support s390/s390x #112751
- gh-112535: Update _Py_ThreadId() to support RISC-V #113084
- [WIP] gh-112535: Implement fallback implementation of _Py_ThreadId() #113094
- gh-112535: Implement fallback implementation of _Py_ThreadId() #113185