Skip to content

Commit

Permalink
Make PyThreadState_GET thread-local
Browse files Browse the repository at this point in the history
  • Loading branch information
colesbury committed Apr 16, 2023
1 parent 385eb1d commit de2be44
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
28 changes: 21 additions & 7 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ _Py_ThreadCanHandlePendingCalls(void)

/* Variable and macro for in-line access to current thread
and interpreter state */

static inline PyThreadState*
_PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
{
return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->gilstate.tstate_current);
}
#if defined(__GNUC__) && !defined(Py_ENABLE_SHARED)
__attribute__((tls_model("local-exec")))
#endif
extern Py_DECL_THREAD PyThreadState *_Py_current_tstate;

/* Get the current Python thread state.
Expand All @@ -82,7 +80,23 @@ _PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
static inline PyThreadState*
_PyThreadState_GET(void)
{
return _PyRuntimeState_GetThreadState(&_PyRuntime);
#if defined(Py_BUILD_CORE_MODULE)
return _PyThreadState_UncheckedGet();
#else
return _Py_current_tstate;
#endif
}

static inline void
_PyThreadState_SET(PyThreadState *tstate)
{
_Py_current_tstate = tstate;
}

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

static inline void
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 @@ -41,9 +41,6 @@ struct _gilstate_runtime_state {
/* bpo-26558: Flag to disable PyGILState_Check().
If set to non-zero, PyGILState_Check() always return 1. */
int check_enabled;
/* Assuming the current thread holds the GIL, this is the
PyThreadState for the current thread. */
_Py_atomic_address tstate_current;
/* The single PyInterpreterState used by this process'
GILState implementation
*/
Expand Down
18 changes: 8 additions & 10 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ to avoid the expense of doing their own locking).
extern "C" {
#endif

#define _PyRuntimeGILState_GetThreadState(gilstate) \
((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
#define _PyRuntimeGILState_SetThreadState(gilstate, value) \
_Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
(uintptr_t)(value))
#define _PyRuntimeGILState_GetThreadState(gilstate) _PyThreadState_GET()
#define _PyRuntimeGILState_SetThreadState(gilstate, value) _PyThreadState_SET(value)

/* Forward declarations */
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
Expand All @@ -58,6 +55,9 @@ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
static const _PyRuntimeState initial = _PyRuntimeState_INIT(_PyRuntime);
_Py_COMP_DIAG_POP

Py_DECL_THREAD PyThreadState *_Py_current_tstate;


static int
alloc_for_runtime(PyThread_type_lock *plock1, PyThread_type_lock *plock2,
PyThread_type_lock *plock3, PyThread_type_lock *plock4)
Expand Down Expand Up @@ -1164,8 +1164,7 @@ _PyThreadState_DeleteCurrent(PyThreadState *tstate)
void
PyThreadState_DeleteCurrent(void)
{
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
PyThreadState *tstate = _PyThreadState_GET();
_PyThreadState_DeleteCurrent(tstate);
}

Expand Down Expand Up @@ -1531,9 +1530,8 @@ static int
PyThreadState_IsCurrent(PyThreadState *tstate)
{
/* Must be the tstate for this thread */
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
assert(_PyGILState_GetThisThreadState(&_PyRuntime.gilstate) == tstate);
return tstate == _PyThreadState_GET();
}

/* Internal initialization/finalization functions called by
Expand Down

0 comments on commit de2be44

Please sign in to comment.