Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
Merged
Prev Previous commit
Next Next commit
Add _PyThraedState_GetCurrent().
  • Loading branch information
ericsnowcurrently committed Apr 7, 2023
commit cf22de1b1229033aac3eb780eaad889fafe9c6c6
6 changes: 3 additions & 3 deletions 6 Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ _Py_ThreadCanHandlePendingCalls(void)
/* Variable and macro for in-line access to current thread
and interpreter state */

PyAPI_DATA(thread_local PyThreadState *) _Py_tss_tstate;
PyAPI_DATA(PyThreadState *) _PyThreadState_GetCurrent(void);
Copy link
Member

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

Copy link
Member Author

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.


static inline PyThreadState*
_PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
{
return _Py_tss_tstate;
return _PyThreadState_GetCurrent();
}

/* Get the current Python thread state.
Expand All @@ -82,7 +82,7 @@ _PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
static inline PyThreadState*
_PyThreadState_GET(void)
{
return _PyRuntimeState_GetThreadState(&_PyRuntime);
return _PyThreadState_GetCurrent();
}

static inline void
Expand Down
8 changes: 7 additions & 1 deletion 8 Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ extern "C" {
*/


thread_local PyThreadState *_Py_tss_tstate;
thread_local PyThreadState *_Py_tss_tstate = NULL;

PyThreadState *
_PyThreadState_GetCurrent(void)
{
return _Py_tss_tstate;
}

static inline PyThreadState *
current_fast_get(_PyRuntimeState *Py_UNUSED(runtime))
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.