diff options
author | Brett Simmers <swtaarrs@users.noreply.github.com> | 2024-03-11 15:02:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-11 15:02:58 (GMT) |
commit | 2731913dd5234ff5ab630a3b7f1c98ad79d4d9df (patch) | |
tree | c666bae1112581bf0fe4c23ced8d188082cbcefd /Include/internal | |
parent | 546eb7a3be241c5abd8a83cebbbab8c71107edcf (diff) | |
download | cpython-2731913dd5234ff5ab630a3b7f1c98ad79d4d9df.zip cpython-2731913dd5234ff5ab630a3b7f1c98ad79d4d9df.tar.gz cpython-2731913dd5234ff5ab630a3b7f1c98ad79d4d9df.tar.bz2 |
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.
Diffstat (limited to 'Include/internal')
-rw-r--r-- | Include/internal/pycore_gil.h | 5 | ||||
-rw-r--r-- | Include/internal/pycore_initconfig.h | 12 |
2 files changed, 17 insertions, 0 deletions
diff --git a/Include/internal/pycore_gil.h b/Include/internal/pycore_gil.h index 19b0d23..d36b4c0 100644 --- a/Include/internal/pycore_gil.h +++ b/Include/internal/pycore_gil.h @@ -20,6 +20,11 @@ extern "C" { #define FORCE_SWITCHING struct _gil_runtime_state { +#ifdef Py_GIL_DISABLED + /* Whether or not this GIL is being used. Can change from 0 to 1 at runtime + if, for example, a module that requires the GIL is loaded. */ + int enabled; +#endif /* microseconds (the Python API uses seconds, though) */ unsigned long interval; /* Last PyThreadState holding / having held the GIL. This helps us diff --git a/Include/internal/pycore_initconfig.h b/Include/internal/pycore_initconfig.h index c869882..1c68161 100644 --- a/Include/internal/pycore_initconfig.h +++ b/Include/internal/pycore_initconfig.h @@ -153,6 +153,18 @@ typedef enum { _PyConfig_INIT_ISOLATED = 3 } _PyConfigInitEnum; +typedef enum { + /* For now, this means the GIL is enabled. + + gh-116329: This will eventually change to "the GIL is disabled but can + be reenabled by loading an incompatible extension module." */ + _PyConfig_GIL_DEFAULT = -1, + + /* The GIL has been forced off or on, and will not be affected by module loading. */ + _PyConfig_GIL_DISABLE = 0, + _PyConfig_GIL_ENABLE = 1, +} _PyConfigGILEnum; + // Export for '_testembed' program PyAPI_FUNC(void) _PyConfig_InitCompatConfig(PyConfig *config); |