8000 gh-103323: Get the "Current" Thread State from a Thread-Local Variable by ericsnowcurrently · Pull Request #103324 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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
tstate_current -> thread_local.
  • Loading branch information
ericsnowcurrently committed Apr 6, 2023
commit 47a70947c82cd0380fc23cff50950f5d18465a3d
8 changes: 4 additions & 4 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ _Py_ThreadCanHandlePendingCalls(void)
/* Variable and macro for in-line access to current thread
and interpreter state */

PyAPI_DATA(thread_local PyThreadState *) _Py_tss_tstate;

static inline PyThreadState*
_PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
{
return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->tstate_current);
return _Py_tss_tstate;
}

/* Get the current Python thread state.

Efficient macro reading directly the 'tstate_current' atomic
variable. The macro is unsafe: it does not check for error and it can
return NULL.
This function is unsafe: it does not check for error and it can return NULL.

The caller must hold the GIL.

Expand Down
3 changes: 0 additions & 3 deletions Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ typedef struct pyruntimestate {

unsigned long main_thread;

/* Assuming the current thread holds the GIL, this is the
PyThreadState for the current thread. */
_Py_atomic_address tstate_current;
/* Used for the thread state bound to the current thread. */
Py_tss_t autoTSSkey;

Expand Down
15 changes: 9 additions & 6 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,26 @@ extern "C" {
For each of these functions, the GIL must be held by the current thread.
*/


thread_local PyThreadState *_Py_tss_tstate;

static inline PyThreadState *
current_fast_get(_PyRuntimeState *runtime)
current_fast_get(_PyRuntimeState *Py_UNUSED(runtime))
{
return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->tstate_current);
return _Py_tss_tstate;
}

static inline void
current_fast_set(_PyRuntimeState *runtime, PyThreadState *tstate)
current_fast_set(_PyRuntimeState *Py_UNUSED(runtime), PyThreadState *tstate)
{
assert(tstate != NULL);
_Py_atomic_store_relaxed(&runtime->tstate_current, (uintptr_t)tstate);
_Py_tss_tstate = tstate;
}

static inline void
current_fast_clear(_PyRuntimeState *runtime)
current_fast_clear(_PyRuntimeState *Py_UNUSED(runtime))
{
_Py_atomic_store_relaxed(&runtime->tstate_current, (uintptr_t)NULL);
_Py_tss_tstate = NULL;
}

#define tstate_verify_not_active(tstate) \
Expand Down
0