]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-103323: Get the "Current" Thread State from a Thread-Local Variable (gh-103324)
authorEric Snow <ericsnowcurrently@gmail.com>
Mon, 24 Apr 2023 17:17:02 +0000 (11:17 -0600)
committerGitHub <noreply@github.com>
Mon, 24 Apr 2023 17:17:02 +0000 (11:17 -0600)
commitf8abfa331421e2c28388c5162f37820495e3c2ee
tree515ce849a73a0c3f34783e8c1fe22f5593c974da
parent7ef614c1adad2b8857442bf0fea649891b591109
gh-103323: Get the "Current" Thread State from a Thread-Local Variable (gh-103324)

We replace _PyRuntime.tstate_current with a thread-local variable. As part of this change, we add a _Py_thread_local macro in pyport.h (only for the core runtime) to smooth out the compiler differences. The main motivation here is in support of a per-interpreter GIL, but this change also provides some performance improvement opportunities.

Note that we do not provide a fallback to the thread-local, either falling back to the old tstate_current or to thread-specific storage (PyThread_tss_*()). If that proves problematic then we can circle back. I consider it unlikely, but will run the buildbots to double-check.

Also note that this does not change any of the code related to the GILState API, where it uses a thread state stored in thread-specific storage. I suspect we can combine that with _Py_tss_tstate (from here). However, that can be addressed separately and is not urgent (nor critical).

(While this change was mostly done independently, I did take some inspiration from earlier (~2020) work by @markshannon (main...markshannon:threadstate_in_tls) and @vstinner (#23976).)
Include/internal/pycore_pystate.h
Include/internal/pycore_runtime.h
Include/pyport.h
Misc/NEWS.d/next/Core and Builtins/2023-04-07-12-18-41.gh-issue-103323.9802br.rst [new file with mode: 0644]
Python/pystate.c