Skip to content

gh-99113: Make Sure the GIL is Acquired at the Right Places #104208

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 7 commits into from
May 6, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Make sure the GIL is held by the current thread in _PyEval_InitGIL().
  • Loading branch information
ericsnowcurrently committed May 6, 2023
commit 3104796f16f3a6e25340fa9247e532d8863ff283
16 changes: 13 additions & 3 deletions Python/ceval_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,15 @@ PyEval_ThreadsInitialized(void)
return _PyEval_ThreadsInitialized();
}

static inline int
current_thread_holds_gil(struct _gil_runtime_state *gil, PyThreadState *tstate)
{
if (((PyThreadState*)_Py_atomic_load_relaxed(&gil->last_holder)) != tstate) {
return 0;
}
return _Py_atomic_load_relaxed(&gil->locked);
}

static void
init_shared_gil(PyInterpreterState *interp, struct _gil_runtime_state *gil)
{
Expand All @@ -525,8 +534,9 @@ _PyEval_InitGIL(PyThreadState *tstate, int own_gil)
if (!own_gil) {
PyInterpreterState *main_interp = _PyInterpreterState_Main();
assert(tstate->interp != main_interp);
init_shared_gil(tstate->interp, main_interp->ceval.gil);
locked = _Py_atomic_load_relaxed(&main_interp->ceval.gil->locked);
struct _gil_runtime_state *gil = main_interp->ceval.gil;
init_shared_gil(tstate->interp, gil);
locked = current_thread_holds_gil(gil, tstate);
}
/* XXX per-interpreter GIL */
else if (!_Py_IsMainInterpreter(tstate->interp)) {
Expand All @@ -537,7 +547,7 @@ _PyEval_InitGIL(PyThreadState *tstate, int own_gil)
init_shared_gil(tstate->interp, main_gil);
// XXX For now we lie.
tstate->interp->ceval.own_gil = 1;
locked = _Py_atomic_load_relaxed(&main_gil->locked);
locked = current_thread_holds_gil(main_gil, tstate);
}
else {
PyThread_init_thread();
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy