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

@ -219,6 +219,11 @@ drop_gil(PyInterpreterState *interp, PyThreadState *tstate)
// XXX assert(tstate == NULL || !tstate->_status.cleared);
struct _gil_runtime_state *gil = ceval->gil;
#ifdef Py_GIL_DISABLED
if (!gil->enabled) {
return;
}
#endif
if (!_Py_atomic_load_ptr_relaxed(&gil->locked)) {
Py_FatalError("drop_gil: GIL is not locked");
}
@ -294,6 +299,11 @@ take_gil(PyThreadState *tstate)
assert(_PyThreadState_CheckConsistency(tstate));
PyInterpreterState *interp = tstate->interp;
struct _gil_runtime_state *gil = interp->ceval.gil;
#ifdef Py_GIL_DISABLED
if (!gil->enabled) {
return;
}
#endif
/* Check that _PyEval_InitThreads() was called to create the lock */
assert(gil_created(gil));
@ -440,6 +450,11 @@ static void
init_own_gil(PyInterpreterState *interp, struct _gil_runtime_state *gil)
{
assert(!gil_created(gil));
#ifdef Py_GIL_DISABLED
// gh-116329: Once it is safe to do so, change this condition to
// (enable_gil == _PyConfig_GIL_ENABLE), so the GIL is disabled by default.
gil->enabled = _PyInterpreterState_GetConfig(interp)->enable_gil != _PyConfig_GIL_DISABLE;
#endif
create_gil(gil);
assert(gil_created(gil));
interp->ceval.gil = gil;

View file

@ -95,6 +95,9 @@ static const PyConfigSpec PYCONFIG_SPEC[] = {
SPEC(safe_path, BOOL),
SPEC(int_max_str_digits, INT),
SPEC(cpu_count, INT),
#ifdef Py_GIL_DISABLED
SPEC(enable_gil, INT),
#endif
SPEC(pathconfig_warnings, BOOL),
SPEC(program_name, WSTR),
SPEC(pythonpath_env, WSTR_OPT),
@ -278,6 +281,9 @@ static const char usage_envvars[] =
"PYTHON_CPU_COUNT: Overrides the return value of os.process_cpu_count(),\n"
" os.cpu_count(), and multiprocessing.cpu_count() if set to\n"
" a positive integer.\n"
#ifdef Py_GIL_DISABLED
"PYTHON_GIL : When set to 0, disables the GIL.\n"
#endif
"PYTHONDEVMODE : enable the development mode.\n"
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n"
"PYTHONWARNDEFAULTENCODING: enable opt-in EncodingWarning for 'encoding=None'.\n"
@ -862,6 +868,9 @@ _PyConfig_InitCompatConfig(PyConfig *config)
config->_is_python_build = 0;
config->code_debug_ranges = 1;
config->cpu_count = -1;
#ifdef Py_GIL_DISABLED
config->enable_gil = _PyConfig_GIL_DEFAULT;
#endif
}
@ -1574,6 +1583,24 @@ config_wstr_to_int(const wchar_t *wstr, int *result)
return 0;
}
static PyStatus
config_read_gil(PyConfig *config, size_t len, wchar_t first_char)
{
#ifdef Py_GIL_DISABLED
if (len == 1 && first_char == L'0') {
config->enable_gil = _PyConfig_GIL_DISABLE;
}
else if (len == 1 && first_char == L'1') {
config->enable_gil = _PyConfig_GIL_ENABLE;
}
else {
return _PyStatus_ERR("PYTHON_GIL / -X gil must be \"0\" or \"1\"");
}
return _PyStatus_OK();
#else
return _PyStatus_ERR("PYTHON_GIL / -X gil are not supported by this build");
#endif
}
static PyStatus
config_read_env_vars(PyConfig *config)
@ -1652,6 +1679,15 @@ config_read_env_vars(PyConfig *config)
config->safe_path = 1;
}
const char *gil = config_get_env(config, "PYTHON_GIL");
if (gil != NULL) {
size_t len = strlen(gil);
status = config_read_gil(config, len, gil[0]);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
return _PyStatus_OK();
}
@ -2207,6 +2243,15 @@ config_read(PyConfig *config, int compute_path_config)
config->show_ref_count = 1;
}
const wchar_t *x_gil = config_get_xoption_value(config, L"gil");
if (x_gil != NULL) {
size_t len = wcslen(x_gil);
status = config_read_gil(config, len, x_gil[0]);
if (_PyStatus_EXCEPTION(status)) {
return status;
}
}
#ifdef Py_STATS
if (config_get_xoption(config, L"pystats")) {
config->_pystats = 1;

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;