8000 gh-118727: Don't drop the GIL in `drop_gil()` unless the current thread holds it by swtaarrs · Pull Request #118745 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-118727: Don't drop the GIL in drop_gil() unless the current thread holds it #118745

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

Merged
merged 10 commits into from
May 23, 2024
Merged
Prev Previous commit
Next Next commit
Restore drop_gil_impl(), clear GIL drop request when disabling GIL
  • Loading branch information
swtaarrs committed May 13, 2024
commit d056a46b35544bafb9c9223031fb4be2a08dfc9f
29 changes: 20 additions & 9 deletions Python/ceval_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ static void recreate_gil(struct _gil_runtime_state *gil)
}
#endif

static inline void
drop_gil_impl(PyThreadState *tstate, struct _gil_runtime_state *gil)
{
MUTEX_LOCK(gil->mutex);
_Py_ANNOTATE_RWLOCK_RELEASED(&gil->locked, /*is_write=*/1);
_Py_atomic_store_int_relaxed(&gil->locked, 0);
if (tstate != NULL) {
tstate->_status.holds_gil = 0;
}
COND_SIGNAL(gil->cond);
MUTEX_UNLOCK(gil->mutex);
}

static void
drop_gil(PyInterpreterState *interp, PyThreadState *tstate, int final_release)
{
Expand Down Expand Up @@ -239,14 +252,7 @@ drop_gil(PyInterpreterState *interp, PyThreadState *tstate, int final_release)
_Py_atomic_store_ptr_relaxed(&gil->last_holder, tstate);
}

MUTEX_LOCK(gil->mutex);
_Py_ANNOTATE_RWLOCK_RELEASED(&gil->locked, /*is_write=*/1);
_Py_atomic_store_int_relaxed(&gil->locked, 0);
if (tstate != NULL) {
tstate->_status.holds_gil = 0;
}
COND_SIGNAL(gil->cond);
MUTEX_UNLOCK(gil->mutex);
drop_gil_impl(tstate, gil);

#ifdef FORCE_SWITCHING
/* We might be releasing the GIL for the last time in this thread. In that
Expand Down Expand Up @@ -1143,7 +1149,12 @@ _PyEval_DisableGIL(PyThreadState *tstate)
//
// Drop the GIL, which will wake up any threads waiting in take_gil()
// and let them resume execution without the GIL.
drop_gil(tstate->interp, tstate, 0);
drop_gil_impl(tstate, gil);

// If another thread asked us to drop the GIL, they should be
// free-threading by now. Remove any such request so we have a clean
// slate if/when the GIL is enabled again.
_Py_unset_eval_breaker_bit(tstate, _PY_GIL_DROP_REQUEST_BIT);
return 1;
}
return 0;
Expand Down
0