Skip to content

Commit

Permalink
Acquire the GIL when not owned.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed May 5, 2023
1 parent c5d3b2c commit 2404310
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions Python/ceval_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,42 +499,56 @@ PyEval_ThreadsInitialized(void)
return _PyEval_ThreadsInitialized();
}

static void
init_shared_gil(PyInterpreterState *interp, struct _gil_runtime_state *gil)
{
assert(gil_created(gil));
interp->ceval.gil = gil;
interp->ceval.own_gil = 0;
}

static void
init_own_gil(PyInterpreterState *interp, struct _gil_runtime_state *gil)
{
assert(!gil_created(gil));
create_gil(gil);
assert(gil_created(gil));
interp->ceval.gil = gil;
interp->ceval.own_gil = 1;
}

PyStatus
_PyEval_InitGIL(PyThreadState *tstate, int own_gil)
{
assert(tstate->interp->ceval.gil == NULL);
int locked;
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();
init_shared_gil(tstate->interp, main_interp->ceval.gil);
locked = _Py_atomic_load_relaxed(&main_interp->ceval.gil->locked);
}

/* XXX per-interpreter GIL */
struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
if (!_Py_IsMainInterpreter(tstate->interp)) {
else if (!_Py_IsMainInterpreter(tstate->interp)) {
/* Currently, the GIL is shared by all interpreters,
and only the main interpreter is responsible to create
and destroy it. */
assert(gil_created(gil));
tstate->interp->ceval.gil = gil;
struct _gil_runtime_state *main_gil = _PyInterpreterState_Main()->ceval.gil;
init_shared_gil(tstate->interp, main_gil);
// XXX For now we lie.
tstate->interp->ceval.own_gil = 1;
return _PyStatus_OK();
locked = _Py_atomic_load_relaxed(&main_gil->locked);
}
else {
PyThread_init_thread();
// XXX per-interpreter GIL: switch to interp->ceval._gil.
init_own_gil(tstate->interp, &tstate->interp->runtime->ceval.gil);
locked = 0;
}
if (!locked) {
take_gil(tstate);
}
assert(own_gil);

assert(!gil_created(gil));

PyThread_init_thread();
create_gil(gil);
assert(gil_created(gil));
tstate->interp->ceval.gil = gil;
tstate->interp->ceval.own_gil = 1;
take_gil(tstate);
return _PyStatus_OK();
}

Expand Down

0 comments on commit 2404310

Please sign in to comment.