8000 gh-116322: Enable the GIL while loading C extension modules by swtaarrs · Pull Request #118560 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-116322: Enable the GIL while loading C extension modules #118560

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 9 commits into from
May 7, 2024
Prev Previous commit
Next Next commit
Review comments from Sam, plus some cleanup
  • Loading branch information
swtaarrs committed May 3, 2024
commit 0fd1967be2117d80d265f1b7fe998c3b22a9812c
6 changes: 5 additions & 1 deletion Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ extern void _PyEval_ReleaseLock(PyInterpreterState *, PyThreadState *);
//
// The enabled state of the GIL will not change while one or more threads are
// attached.
extern int _PyEval_IsGILEnabled(PyThreadState *tstate);
static inline int
_PyEval_IsGILEnabled(PyThreadState *tstate)
{
return tstate->interp->ceval.gil->enabled != 0;
}

// Enable or disable the GIL used by the interpreter that owns tstate, which
// must be the current thread. This may affect other interpreters, if the GIL
Expand Down
5 changes: 4 additions & 1 deletion Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,11 @@ extern int _PyImport_CheckSubinterpIncompatibleExtensionAllowed(
PyAPI_FUNC(int) _PyImport_ClearExtension(PyObject *name, PyObject *filename);

#ifdef Py_GIL_DISABLED
// Store an association between module and gil (which should be one of the
// values for the Py_mod_gil module slot) in the current interpreter.
//
// Only for use on modules with md_def->m_size == -1.
extern int _PyImport_SetModuleGIL(PyObject *module, void *gil);
extern void *_PyImport_GetModuleDefGIL(PyModuleDef *def);

// Assuming that the GIL is enabled from a call to
// _PyEval_EnableGILTransient(), resolve the transient request depending on the
Expand Down
18 changes: 6 additions & 12 deletions Python/ceval_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -1033,17 +1033,11 @@ _PyEval_InitState(PyInterpreterState *interp)
}

#ifdef Py_GIL_DISABLED
int
_PyEval_IsGILEnabled(PyThreadState *tstate)
{
return tstate->interp->ceval.gil->enabled != 0;
}

int
_PyEval_EnableGILTransient(PyThreadState *tstate)
{
if (_PyInterpreterState_GetConfig(tstate->interp)->enable_gil !=
_PyConfig_GIL_DEFAULT) {
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
if (config->enable_gil != _PyConfig_GIL_DEFAULT) {
return 0;
}
struct _gil_runtime_state *gil = tstate->interp->ceval.gil;
Expand Down Expand Up @@ -1094,8 +1088,8 @@ _PyEval_EnableGILTransient(PyThreadState *tstate)
int
_PyEval_EnableGILPermanent(PyThreadState *tstate)
{
if (_PyInterpreterState_GetConfig(tstate->interp)->enable_gil !=
_PyConfig_GIL_DEFAULT) {
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
if (config->enable_gil != _PyConfig_GIL_DEFAULT) {
return 0;
}

Expand All @@ -1114,8 +1108,8 @@ _PyEval_EnableGILPermanent(PyThreadState *tstate)
int
_PyEval_DisableGIL(PyThreadState *tstate)
{
if (_PyInterpreterState_GetConfig(tstate->interp)->enable_gil !=
_PyConfig_GIL_DEFAULT) {
const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
if (config->enable_gil != _PyConfig_GIL_DEFAULT) {
return 0;
}

Expand Down
4 changes: 1 addition & 3 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,7 @@ _PyImport_SetModuleGIL(PyObject *module, void *gil)
PyInterpreterState *interp = _PyInterpreterState_GET();
assert(PyModule_Check(module));
PyModuleDef *def = ((PyModuleObject *)module)->md_def;
if (def->m_size != -1) {
return 0;
}
assert(def->m_size == -1);

Py_ssize_t index = def->m_base.m_index;
assert(index > 0);
Expand Down
6 changes: 5 additions & 1 deletion Python/importdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,11 @@ _PyImport_RunModInitFunc(PyModInitFunction p0,
// This module may be recreated (without running its init function)
// in reload_singlephase_extension(), so remember its GIL slot
// here.
_PyImport_SetModuleGIL(m, ((PyModuleObject *)m)->md_gil);
if (_PyImport_SetModuleGIL(m, ((PyModuleObject *)m)->md_gil) < 0) {
_Py_ext_module_loader_result_set_error(
&res, _Py_ext_module_loader_result_EXCEPTION);
goto error;
}
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2071,7 +2071,7 @@ _PyThreadState_Attach(PyThreadState *tstate)
}

#ifdef Py_GIL_DISABLED
if (!!tstate->interp->ceval.gil->enabled != acquired_gil) {
if (_PyEval_IsGILEnabled(tstate) != acquired_gil) {
// The GIL was enabled between our call to _PyEval_AcquireLock()
// and when we attached (the GIL can't go from enabled to disabled
// here because only a thread holding the GIL can disable
Expand Down
Loading
0