Skip to content

Commit

Permalink
pythongh-117514: Add sys._is_gil_enabled() function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
colesbury committed May 2, 2024
1 parent 72867c9 commit c46911b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
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)
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=e646df2a04652e80]*/
{
#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

0 comments on commit c46911b

Please sign in to comment.