Skip to content
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

gh-117514: Add sys._is_gil_enabled() function #118514

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,14 @@ always available.
return value of :func:`intern` around to benefit from it.


.. function:: _is_gil_enabled()

Return :const:`True` if the :term:`GIL` is enabled and :const:`False` if
it is disabled.

.. versionadded:: 3.13


.. function:: is_finalizing()

Return :const:`True` if the main Python interpreter is
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,12 @@ def test_getallocatedblocks(self):
c = sys.getallocatedblocks()
self.assertIn(c, range(b - 50, b + 50))

def test_is_gil_enabled(self):
if support.Py_GIL_DISABLED:
self.assertIs(type(sys._is_gil_enabled()), bool)
Eclips4 marked this conversation as resolved.
Show resolved Hide resolved
else:
self.assertTrue(sys._is_gil_enabled())

def test_is_finalizing(self):
self.assertIs(sys.is_finalizing(), False)
# Don't use the atexit module because _Py_Finalizing is only set
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Add ``sys._is_gil_enabled()`` function that returns whether the GIL is
currently enabled. In the default build it always returns ``True`` because
the GIL is always enabled. In the free-threaded build, it may return
``True`` or ``False``.
30 changes: 29 additions & 1 deletion Python/clinic/sysmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -2569,6 +2588,7 @@ static PyMethodDef sys_methods[] = {
SYS__STATS_DUMP_METHODDEF
#endif
SYS__GET_CPU_COUNT_CONFIG_METHODDEF
SYS__IS_GIL_ENABLED_METHODDEF
{NULL, NULL} // sentinel
};

Expand Down
Loading