gh-99113: Add PyInterpreterConfig.own_gil (gh-104204)

We also add PyInterpreterState.ceval.own_gil to record if the interpreter actually has its own GIL.

Note that for now we don't actually respect own_gil; all interpreters still share the one GIL.  However, PyInterpreterState.ceval.own_gil does reflect PyInterpreterConfig.own_gil.  That lie is a temporary one that we will fix when the GIL really becomes per-interpreter.
This commit is contained in:
Eric Snow 2023-05-05 15:59:20 -06:00 committed by GitHub
parent 66558d2a16
commit f3e7eb48f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 79 additions and 18 deletions

View file

@ -500,9 +500,18 @@ PyEval_ThreadsInitialized(void)
}
PyStatus
_PyEval_InitGIL(PyThreadState *tstate)
_PyEval_InitGIL(PyThreadState *tstate, int own_gil)
{
assert(tstate->interp->ceval.gil == NULL);
if (!own_gil) {
PyInterpreterState *main_interp = _PyInterpreterState_Main();
assert(tstate->interp != main_interp);
struct _gil_runtime_state *gil = main_interp->ceval.gil;
assert(gil_created(gil));
tstate->interp->ceval.gil = gil;
tstate->interp->ceval.own_gil = 0;
return _PyStatus_OK();
}
/* XXX per-interpreter GIL */
struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
@ -512,8 +521,11 @@ _PyEval_InitGIL(PyThreadState *tstate)
and destroy it. */
assert(gil_created(gil));
tstate->interp->ceval.gil = gil;
// XXX For now we lie.
tstate->interp->ceval.own_gil = 1;
return _PyStatus_OK();
}
assert(own_gil);
assert(!gil_created(gil));
@ -521,6 +533,7 @@ _PyEval_InitGIL(PyThreadState *tstate)
create_gil(gil);
assert(gil_created(gil));
tstate->interp->ceval.gil = gil;
tstate->interp->ceval.own_gil = 1;
take_gil(tstate);
return _PyStatus_OK();
}
@ -530,6 +543,14 @@ _PyEval_FiniGIL(PyInterpreterState *interp)
{
if (interp->ceval.gil == NULL) {
/* It was already finalized (or hasn't been initialized yet). */
assert(!interp->ceval.own_gil);
return;
}
else if (!interp->ceval.own_gil) {
PyInterpreterState *main_interp = _PyInterpreterState_Main();
assert(interp != main_interp);
assert(interp->ceval.gil == main_interp->ceval.gil);
interp->ceval.gil = NULL;
return;
}

View file

@ -585,7 +585,7 @@ init_interp_settings(PyInterpreterState *interp,
static PyStatus
init_interp_create_gil(PyThreadState *tstate)
init_interp_create_gil(PyThreadState *tstate, int own_gil)
{
PyStatus status;
@ -600,7 +600,7 @@ init_interp_create_gil(PyThreadState *tstate)
}
/* Create the GIL and take it */
status = _PyEval_InitGIL(tstate);
status = _PyEval_InitGIL(tstate, own_gil);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
@ -632,7 +632,9 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
return status;
}
const PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT;
PyInterpreterConfig config = _PyInterpreterConfig_LEGACY_INIT;
// The main interpreter always has its own GIL.
config.own_gil = 1;
status = init_interp_settings(interp, &config);
if (_PyStatus_EXCEPTION(status)) {
return status;
@ -645,7 +647,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
_PyThreadState_Bind(tstate);
(void) PyThreadState_Swap(tstate);
status = init_interp_create_gil(tstate);
status = init_interp_create_gil(tstate, config.own_gil);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
@ -2047,7 +2049,7 @@ new_interpreter(PyThreadState **tstate_p, const PyInterpreterConfig *config)
goto error;
}
status = init_interp_create_gil(tstate);
status = init_interp_create_gil(tstate, config->own_gil);
if (_PyStatus_EXCEPTION(status)) {
goto error;
}