summaryrefslogtreecommitdiffstats
path: root/Python/ceval_gil.c
diff options
context:
space:
mode:
authorBrett Simmers <swtaarrs@users.noreply.github.com>2024-03-11 15:02:58 (GMT)
committerGitHub <noreply@github.com>2024-03-11 15:02:58 (GMT)
commit2731913dd5234ff5ab630a3b7f1c98ad79d4d9df (patch)
treec666bae1112581bf0fe4c23ced8d188082cbcefd /Python/ceval_gil.c
parent546eb7a3be241c5abd8a83cebbbab8c71107edcf (diff)
downloadcpython-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 'Python/ceval_gil.c')
-rw-r--r--Python/ceval_gil.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/Python/ceval_gil.c b/Python/ceval_gil.c
index edfc466..d2cd35d 100644
--- a/Python/ceval_gil.c
+++ b/Python/ceval_gil.c
@@ -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;