Skip to content

Commit 87f649a

Browse files
authored
bpo-43311: Create GIL autoTSSkey ealier (GH-24819)
At Python startup, call _PyGILState_Init() before PyInterpreterState_New() which calls _PyThreadState_GET(). When Python is built using --with-experimental-isolated-subinterpreters, _PyThreadState_GET() uses autoTSSkey.
1 parent 9a9c11a commit 87f649a

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

Include/internal/pycore_pylifecycle.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_runtime.h" // _PyRuntimeState
12+
1113
/* Forward declarations */
1214
struct _PyArgv;
1315
struct pyruntimestate;
@@ -88,7 +90,8 @@ extern void _PyWarnings_Fini(PyInterpreterState *interp);
8890
extern void _PyAST_Fini(PyInterpreterState *interp);
8991
extern void _PyAtExit_Fini(PyInterpreterState *interp);
9092

91-
extern PyStatus _PyGILState_Init(PyThreadState *tstate);
93+
extern PyStatus _PyGILState_Init(_PyRuntimeState *runtime);
94+
extern PyStatus _PyGILState_SetTstate(PyThreadState *tstate);
9295
extern void _PyGILState_Fini(PyInterpreterState *interp);
9396

9497
PyAPI_FUNC(void) _PyGC_DumpShutdownStats(PyInterpreterState *interp);

Python/pylifecycle.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ init_interp_create_gil(PyThreadState *tstate)
577577
_PyEval_FiniGIL(tstate->interp);
578578

579579
/* Auto-thread-state API */
580-
status = _PyGILState_Init(tstate);
580+
status = _PyGILState_SetTstate(tstate);
581581
if (_PyStatus_EXCEPTION(status)) {
582582
return status;
583583
}
@@ -597,12 +597,19 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
597597
const PyConfig *config,
598598
PyThreadState **tstate_p)
599599
{
600+
/* Auto-thread-state API */
601+
PyStatus status = _PyGILState_Init(runtime);
602+
if (_PyStatus_EXCEPTION(status)) {
603+
return status;
604+
}
605+
600606
PyInterpreterState *interp = PyInterpreterState_New();
601607
if (interp == NULL) {
602608
return _PyStatus_ERR("can't make main interpreter");
603609
}
610+
assert(_Py_IsMainInterpreter(interp));
604611

605-
PyStatus status = _PyConfig_Copy(&interp->config, config);
612+
status = _PyConfig_Copy(&interp->config, config);
606613
if (_PyStatus_EXCEPTION(status)) {
607614
return status;
608615
}

Python/pystate.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,21 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
13271327
Py_Initialize/Py_FinalizeEx
13281328
*/
13291329
PyStatus
1330-
_PyGILState_Init(PyThreadState *tstate)
1330+
_PyGILState_Init(_PyRuntimeState *runtime)
1331+
{
1332+
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
1333+
if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
1334+
return _PyStatus_NO_MEMORY();
1335+
}
1336+
// PyThreadState_New() calls _PyGILState_NoteThreadState() which does
1337+
// nothing before autoInterpreterState is set.
1338+
assert(gilstate->autoInterpreterState == NULL);
1339+
return _PyStatus_OK();
1340+
}
1341+
1342+
1343+
PyStatus
1344+
_PyGILState_SetTstate(PyThreadState *tstate)
13311345
{
13321346
if (!_Py_IsMainInterpreter(tstate->interp)) {
13331347
/* Currently, PyGILState is shared by all interpreters. The main
@@ -1341,9 +1355,6 @@ _PyGILState_Init(PyThreadState *tstate)
13411355

13421356
struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
13431357

1344-
if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
1345-
return _PyStatus_NO_MEMORY();
1346-
}
13471358
gilstate->autoInterpreterState = tstate->interp;
13481359
assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
13491360
assert(tstate->gilstate_counter == 0);

0 commit comments

Comments
 (0)