diff options
author | Sam Gross <colesbury@gmail.com> | 2024-05-03 15:09:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-03 15:09:57 (GMT) |
commit | 2dae505e87e3815f087d4b07a71bb2c5cce22304 (patch) | |
tree | 232577513b53b378623dc28c7dae53c846b2bf77 /Python/sysmodule.c | |
parent | 24e643d4ef024a3561c927dc07c59c435bb27bcc (diff) | |
download | cpython-2dae505e87e3815f087d4b07a71bb2c5cce22304.zip cpython-2dae505e87e3815f087d4b07a71bb2c5cce22304.tar.gz cpython-2dae505e87e3815f087d4b07a71bb2c5cce22304.tar.bz2 |
gh-117514: Add `sys._is_gil_enabled()` function (#118514)
The function returns `True` or `False` depending on whether the GIL is
currently enabled. In the default build, it always returns `True`
because the GIL is always enabled.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r-- | Python/sysmodule.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index d3fbfcd..f469f16 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2393,6 +2393,25 @@ sys__get_cpu_count_config_impl(PyObject *module) return config->cpu_count; } +/*[clinic input] +sys._is_gil_enabled -> bool + +Return True if the GIL is currently enabled and False otherwise. +[clinic start generated code]*/ + +static int +sys__is_gil_enabled_impl(PyObject *module) +/*[clinic end generated code: output=57732cf53f5b9120 input=7e9c47f15a00e809]*/ +{ +#ifdef Py_GIL_DISABLED + PyInterpreterState *interp = _PyInterpreterState_GET(); + return interp->ceval.gil->enabled; +#else + return 1; +#endif +} + + static PerfMapState perf_map_state; PyAPI_FUNC(int) PyUnstable_PerfMapState_Init(void) { @@ -2565,6 +2584,7 @@ static PyMethodDef sys_methods[] = { SYS__STATS_DUMP_METHODDEF #endif SYS__GET_CPU_COUNT_CONFIG_METHODDEF + SYS__IS_GIL_ENABLED_METHODDEF {NULL, NULL} // sentinel }; |