Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Acquire the GIL when not owned.
  • Loading branch information
ericsnowcurrently committed May 5, 2023
commit 2404310dea7c14a59fba9a7ffae4f203005bbf4f
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;
Comment thread
erlend-aasland marked this conversation as resolved.
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