gh-116167: Allow disabling the GIL with PYTHON_GIL=0 or -X gil=0 (#116338)

In free-threaded builds, running with `PYTHON_GIL=0` will now disable the
GIL. Follow-up issues track work to re-enable the GIL when loading an
incompatible extension, and to disable the GIL by default.

In order to support re-enabling the GIL at runtime, all GIL-related data
structures are initialized as usual, and disabling the GIL simply sets a flag
that causes `take_gil()` and `drop_gil()` to return early.
This commit is contained in:
Brett Simmers 2024-03-11 11:02:58 -04:00 committed by GitHub
parent 546eb7a3be
commit 2731913dd5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 163 additions and 1 deletions

View file

@ -3048,6 +3048,7 @@ static PyStructSequence_Field flags_fields[] = {
{"warn_default_encoding", "-X warn_default_encoding"},
{"safe_path", "-P"},
{"int_max_str_digits", "-X int_max_str_digits"},
{"gil", "-X gil"},
{0}
};
@ -3097,6 +3098,16 @@ set_flags_from_config(PyInterpreterState *interp, PyObject *flags)
SetFlag(config->warn_default_encoding);
SetFlagObj(PyBool_FromLong(config->safe_path));
SetFlag(config->int_max_str_digits);
#ifdef Py_GIL_DISABLED
if (config->enable_gil == _PyConfig_GIL_DEFAULT) {
SetFlagObj(Py_NewRef(Py_None));
}
else {
SetFlag(config->enable_gil);
}
#else
SetFlagObj(PyLong_FromLong(1));
#endif
#undef SetFlagObj
#undef SetFlag
return 0;