Skip to content

Commit de2be44

Browse files
committed
Make PyThreadState_GET thread-local
1 parent 385eb1d commit de2be44

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

Include/internal/pycore_pystate.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,10 @@ _Py_ThreadCanHandlePendingCalls(void)
6363

6464
/* Variable and macro for in-line access to current thread
6565
and interpreter state */
66-
67-
static inline PyThreadState*
68-
_PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
69-
{
70-
return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->gilstate.tstate_current);
71-
}
66+
#if defined(__GNUC__) && !defined(Py_ENABLE_SHARED)
67+
__attribute__((tls_model("local-exec")))
68+
#endif
69+
extern Py_DECL_THREAD PyThreadState *_Py_current_tstate;
7270

7371
/* Get the current Python thread state.
7472
@@ -82,7 +80,23 @@ _PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
8280
static inline PyThreadState*
8381
_PyThreadState_GET(void)
8482
{
85-
return _PyRuntimeState_GetThreadState(&_PyRuntime);
83+
#if defined(Py_BUILD_CORE_MODULE)
84+
return _PyThreadState_UncheckedGet();
85+
#else
86+
return _Py_current_tstate;
87+
#endif
88+
}
89+
90+
static inline void
91+
_PyThreadState_SET(PyThreadState *tstate)
92+
{
93+
_Py_current_tstate = tstate;
94+
}
95+
96+
static inline PyThreadState*
97+
_PyRuntimeState_GetThreadState(_PyRuntimeState *runtime)
98+
{
99+
return _PyThreadState_GET();
86100
}
87101

88102
static inline void

Include/internal/pycore_runtime.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ struct _gilstate_runtime_state {
4141
/* bpo-26558: Flag to disable PyGILState_Check().
4242
If set to non-zero, PyGILState_Check() always return 1. */
4343
int check_enabled;
44-
/* Assuming the current thread holds the GIL, this is the
45-
PyThreadState for the current thread. */
46-
_Py_atomic_address tstate_current;
4744
/* The single PyInterpreterState used by this process'
4845
GILState implementation
4946
*/

Python/pystate.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@ to avoid the expense of doing their own locking).
3737
extern "C" {
3838
#endif
3939

40-
#define _PyRuntimeGILState_GetThreadState(gilstate) \
41-
((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
42-
#define _PyRuntimeGILState_SetThreadState(gilstate, value) \
43-
_Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
44-
(uintptr_t)(value))
40+
#define _PyRuntimeGILState_GetThreadState(gilstate) _PyThreadState_GET()
41+
#define _PyRuntimeGILState_SetThreadState(gilstate, value) _PyThreadState_SET(value)
4542

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

58+
Py_DECL_THREAD PyThreadState *_Py_current_tstate;
59+
60+
6161
static int
6262
alloc_for_runtime(PyThread_type_lock *plock1, PyThread_type_lock *plock2,
6363
PyThread_type_lock *plock3, PyThread_type_lock *plock4)
@@ -1164,8 +1164,7 @@ _PyThreadState_DeleteCurrent(PyThreadState *tstate)
11641164
void
11651165
PyThreadState_DeleteCurrent(void)
11661166
{
1167-
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1168-
PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
1167+
PyThreadState *tstate = _PyThreadState_GET();
11691168
_PyThreadState_DeleteCurrent(tstate);
11701169
}
11711170

@@ -1531,9 +1530,8 @@ static int
15311530
PyThreadState_IsCurrent(PyThreadState *tstate)
15321531
{
15331532
/* Must be the tstate for this thread */
1534-
struct _gilstate_runtime_state *gilstate = &_PyRuntime.gilstate;
1535-
assert(_PyGILState_GetThisThreadState(gilstate) == tstate);
1536-
return tstate == _PyRuntimeGILState_GetThreadState(gilstate);
1533+
assert(_PyGILState_GetThisThreadState(&_PyRuntime.gilstate) == tstate);
1534+
return tstate == _PyThreadState_GET();
15371535
}
15381536

15391537
/* Internal initialization/finalization functions called by

0 commit comments

Comments
 (0)