Skip to content

Commit 0ee4aba

Browse files
use tls for interp
1 parent 95e5d59 commit 0ee4aba

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

Include/internal/pycore_pystate.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ _Py_ThreadCanHandleSignals(PyInterpreterState *interp)
9191

9292
#if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
9393
extern _Py_thread_local PyThreadState *_Py_tss_tstate;
94+
extern _Py_thread_local PyInterpreterState *_Py_tss_interp;
9495
#endif
9596

9697
#ifndef NDEBUG
@@ -204,11 +205,15 @@ _Py_EnsureFuncTstateNotNULL(const char *func, PyThreadState *tstate)
204205
See also PyInterpreterState_Get()
205206
and _PyGILState_GetInterpreterStateUnsafe(). */
206207
static inline PyInterpreterState* _PyInterpreterState_GET(void) {
207-
PyThreadState *tstate = _PyThreadState_GET();
208208
#ifdef Py_DEBUG
209+
PyThreadState *tstate = _PyThreadState_GET();
209210
_Py_EnsureTstateNotNULL(tstate);
210211
#endif
211-
return tstate->interp;
212+
#if defined(HAVE_THREAD_LOCAL) && !defined(Py_BUILD_CORE_MODULE)
213+
return _Py_tss_interp;
214+
#else
215+
return PyInterpreterState_Get();
216+
#endif
212217
}
213218

214219

Python/pystate.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ _Py_thread_local PyThreadState *_Py_tss_tstate = NULL;
7878
also known as a "gilstate." */
7979
_Py_thread_local PyThreadState *_Py_tss_gilstate = NULL;
8080

81+
/* The interpreter of the attached thread state */
82+
_Py_thread_local PyInterpreterState *_Py_tss_interp = NULL;
83+
8184
static inline PyThreadState *
8285
current_fast_get(void)
8386
{
@@ -89,12 +92,15 @@ current_fast_set(_PyRuntimeState *Py_UNUSED(runtime), PyThreadState *tstate)
8992
{
9093
assert(tstate != NULL);
9194
_Py_tss_tstate = tstate;
95+
assert(tstate->interp != NULL);
96+
_Py_tss_interp = tstate->interp;
9297
}
9398

9499
static inline void
95100
current_fast_clear(_PyRuntimeState *Py_UNUSED(runtime))
96101
{
97102
_Py_tss_tstate = NULL;
103+
_Py_tss_interp = NULL;
98104
}
99105

100106
#define tstate_verify_not_active(tstate) \

0 commit comments

Comments
 (0)