Skip to content

bpo-43311: Create GIL autoTSSkey ealier #24819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Include/internal/pycore_pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_runtime.h" // _PyRuntimeState

/* Forward declarations */
struct _PyArgv;
struct pyruntimestate;
Expand Down Expand Up @@ -88,7 +90,8 @@ extern void _PyWarnings_Fini(PyInterpreterState *interp);
extern void _PyAST_Fini(PyInterpreterState *interp);
extern void _PyAtExit_Fini(PyInterpreterState *interp);

extern PyStatus _PyGILState_Init(PyThreadState *tstate);
extern PyStatus _PyGILState_Init(_PyRuntimeState *runtime);
extern PyStatus _PyGILState_SetTstate(PyThreadState *tstate);
extern void _PyGILState_Fini(PyInterpreterState *interp);

PyAPI_FUNC(void) _PyGC_DumpShutdownStats(PyInterpreterState *interp);
Expand Down
11 changes: 9 additions & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ init_interp_create_gil(PyThreadState *tstate)
_PyEval_FiniGIL(tstate->interp);

/* Auto-thread-state API */
status = _PyGILState_Init(tstate);
status = _PyGILState_SetTstate(tstate);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
Expand All @@ -597,12 +597,19 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
const PyConfig *config,
PyThreadState **tstate_p)
{
/* Auto-thread-state API */
PyStatus status = _PyGILState_Init(runtime);
if (_PyStatus_EXCEPTION(status)) {
return status;
}

PyInterpreterState *interp = PyInterpreterState_New();
if (interp == NULL) {
return _PyStatus_ERR("can't make main interpreter");
}
assert(_Py_IsMainInterpreter(interp));

PyStatus status = _PyConfig_Copy(&interp->config, config);
status = _PyConfig_Copy(&interp->config, config);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
Expand Down
19 changes: 15 additions & 4 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,21 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
Py_Initialize/Py_FinalizeEx
*/
PyStatus
_PyGILState_Init(PyThreadState *tstate)
_PyGILState_Init(_PyRuntimeState *runtime)
{
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
return _PyStatus_NO_MEMORY();
}
// PyThreadState_New() calls _PyGILState_NoteThreadState() which does
// nothing before autoInterpreterState is set.
assert(gilstate->autoInterpreterState == NULL);
return _PyStatus_OK();
}


PyStatus
_PyGILState_SetTstate(PyThreadState *tstate)
{
if (!_Py_IsMainInterpreter(tstate->interp)) {
/* Currently, PyGILState is shared by all interpreters. The main
Expand All @@ -1341,9 +1355,6 @@ _PyGILState_Init(PyThreadState *tstate)

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

if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
return _PyStatus_NO_MEMORY();
}
gilstate->autoInterpreterState = tstate->interp;
assert(PyThread_tss_get(&gilstate->autoTSSkey) == NULL);
assert(tstate->gilstate_counter == 0);
Expand Down