-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-103323: Get the "Current" Thread State from a Thread-Local Variable #103324
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
ericsnowcurrently
merged 12 commits into
python:main
from
ericsnowcurrently:tstate_current-as-thread_local
Apr 24, 2023
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c4db85a
Add a thread_local macro.
ericsnowcurrently 47a7094
tstate_current -> thread_local.
ericsnowcurrently cf22de1
Add _PyThraedState_GetCurrent().
ericsnowcurrently d4136d2
Add HAVE_THREAD_LOCAL.
ericsnowcurrently f8c6598
Support the faster approach, if available.
ericsnowcurrently 9496df0
Do not fail if thread_local not supported.
ericsnowcurrently 2c335a3
thread_local -> _Py_thread_local
ericsnowcurrently 4af0ce7
Only define _Py_thread_local for the core runtime.
ericsnowcurrently 3db4007
Fix pystate.c.
ericsnowcurrently d573053
Call _PyThreadState_GET() from _PyRuntimeState_GetThreadState().
ericsnowcurrently feb8ef5
Fix the error message.
ericsnowcurrently 2332a2e
Add a NEWS entry.
ericsnowcurrently File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Call _PyThreadState_GET() from _PyRuntimeState_GetThreadState().
- Loading branch information
commit d57305336a00b68563b59ac6dfdd424f18e2a998
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,8 +69,15 @@ extern _Py_thread_local PyThreadState *_Py_tss_tstate; | |
#endif | ||
PyAPI_DATA(PyThreadState *) _PyThreadState_GetCurrent(void); | ||
|
||
/* Get the current Python thread state. | ||
|
||
This function is unsafe: it does not check for error and it can return NULL. | ||
|
||
The caller must hold the GIL. | ||
|
||
See also PyThreadState_Get() and _PyThreadState_UncheckedGet(). */ | ||
static inline PyThreadState* | ||
_PyRuntimeState_GetThreadState(_PyRuntimeState *runtime) | ||
_PyThreadState_GET(void) | ||
{ | ||
#if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE) | ||
return _Py_tss_tstate; | ||
|
@@ -79,20 +86,13 @@ _PyRuntimeState_GetThreadState(_PyRuntimeState *runtime) | |
#endif | ||
} | ||
|
||
|
||
/* Get the current Python thread state. | ||
|
||
This function is unsafe: it does not check for error and it can return NULL. | ||
|
||
The caller must hold the GIL. | ||
|
||
See also PyThreadState_Get() and _PyThreadState_UncheckedGet(). */ | ||
static inline PyThreadState* | ||
_PyThreadState_GET(void) | ||
_PyRuntimeState_GetThreadState(_PyRuntimeState *Py_UNUSED(runtime)) | ||
{ | ||
return _PyRuntimeState_GetThreadState(&_PyRuntime); | ||
return _PyThreadState_GET(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function no longer makes sense: I wrote PR #104171 to remove it. |
||
} | ||
|
||
|
||
static inline void | ||
_Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate) | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the use case for this new _PyThreadState_GetCurrent() function? There is already PyThreadState_Get(). How is it different?
The API to get the current thread state is already complicated and has a complicated history: https://pythondev.readthedocs.io/pystate.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's there because I couldn't find a way to mix
PyAPI_DATA
with_Py_thread_local
. Looks like that's the same issue you ran into in 2020.