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

Commit 0d5a28b

Browse filesBrowse files
committed
Move threadstate pointer to thread-local (C) variable.
1 parent 6c6770d commit 0d5a28b
Copy full SHA for 0d5a28b

File tree

3 files changed

+11
-7
lines changed
Filter options

3 files changed

+11
-7
lines changed

‎Include/cpython/pystate.h

Copy file name to clipboardExpand all lines: Include/cpython/pystate.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ typedef struct _err_stackitem {
4646

4747
} _PyErr_StackItem;
4848

49+
extern __thread struct _ts *_Py_tls_tstate;
4950

5051
// The PyThreadState typedef is in Include/pystate.h.
5152
struct _ts {

‎Include/internal/pycore_pystate.h

Copy file name to clipboardExpand all lines: Include/internal/pycore_pystate.h
+3-4Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,8 @@ typedef struct _Py_AuditHookEntry {
189189

190190
struct _gilstate_runtime_state {
191191
int check_enabled;
192-
/* Assuming the current thread holds the GIL, this is the
193-
PyThreadState for the current thread. */
194-
_Py_atomic_address tstate_current;
192+
/* This is the PyThreadState for the thread holding the GIL. */
193+
_Py_atomic_address gil_holder;
195194
/* The single PyInterpreterState used by this process'
196195
GILState implementation
197196
*/
@@ -337,7 +336,7 @@ _Py_ThreadCanHandlePendingCalls(void)
337336
The caller must hold the GIL.
338337
339338
See also PyThreadState_Get() and PyThreadState_GET(). */
340-
#define _PyThreadState_GET() ((PyThreadState*)_Py_atomic_load_relaxed(&_PyRuntime.gilstate.tstate_current))
339+
#define _PyThreadState_GET() _Py_tls_tstate
341340

342341
/* Redefine PyThreadState_GET() as an alias to _PyThreadState_GET() */
343342
#undef PyThreadState_GET

‎Python/pystate.c

Copy file name to clipboardExpand all lines: Python/pystate.c
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ extern "C" {
3232
#endif
3333

3434
#define _PyRuntimeGILState_GetThreadState(gilstate) \
35-
((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->tstate_current))
35+
((PyThreadState*)_Py_atomic_load_relaxed(&(gilstate)->gil_holder))
3636
#define _PyRuntimeGILState_SetThreadState(gilstate, value) \
37-
_Py_atomic_store_relaxed(&(gilstate)->tstate_current, \
37+
_Py_atomic_store_relaxed(&(gilstate)->gil_holder, \
3838
(uintptr_t)(value))
3939

4040
static void
@@ -46,6 +46,7 @@ ensure_tstate_not_null(const char *func, PyThreadState *tstate)
4646
}
4747
}
4848

49+
__thread struct _ts *_Py_tls_tstate = NULL;
4950

5051
/* Forward declarations */
5152
static PyThreadState *_PyGILState_GetThisThreadState(struct _gilstate_runtime_state *gilstate);
@@ -860,6 +861,7 @@ _PyThreadState_DeleteCurrent(PyThreadState *tstate)
860861
struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
861862
tstate_delete_common(tstate, gilstate);
862863
_PyRuntimeGILState_SetThreadState(gilstate, NULL);
864+
_Py_tls_tstate = NULL;
863865
_PyEval_ReleaseLock(tstate);
864866
PyMem_RawFree(tstate);
865867
}
@@ -934,9 +936,11 @@ PyThreadState_Get(void)
934936
PyThreadState *
935937
_PyThreadState_Swap(struct _gilstate_runtime_state *gilstate, PyThreadState *newts)
936938
{
937-
PyThreadState *oldts = _PyRuntimeGILState_GetThreadState(gilstate);
939+
assert(_PyRuntimeGILState_GetThreadState(gilstate) == _Py_tls_tstate);
940+
PyThreadState *oldts = _Py_tls_tstate;
938941

939942
_PyRuntimeGILState_SetThreadState(gilstate, newts);
943+
_Py_tls_tstate = newts;
940944
/* It should not be possible for more than one thread state
941945
to be used for a thread. Check this the best we can in debug
942946
builds.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.