8000 gh-59956: Clarify GILState-related Code by ericsnowcurrently · Pull Request #101161 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-59956: Clarify GILState-related Code #101161

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 11 commits into from
Jan 19, 2023
Merged
Prev Previous commit
Next Next commit
PyThreadState_IsCurrent() -> holds_gil().
  • Loading branch information
ericsnowcurrently committed Jan 19, 2023
commit 5e7283c786a38c9bfd2154a7a2d6c16b464ab5f5
56 changes: 30 additions & 26 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,26 @@ unbind_tstate(PyThreadState *tstate)
tstate->native_thread_id = -1;
}

/* This is not exported, as it is not reliable! It can only
ever be compared to the state for the *current* thread.
* If not equal, then it doesn't matter that the actual
value may change immediately after comparison, as it can't
possibly change to the current thread's state.
* If equal, then the current thread holds the lock, so the value can't
change until we yield the lock.
*/
static int
holds_gil(PyThreadState *tstate)
{
// XXX Fall back to tstate->interp->runtime->ceval.gil.last_holder
// (and tstate->interp->runtime->ceval.gil.locked).
assert(tstate != NULL);
_PyRuntimeState *runtime = tstate->interp->runtime;
/* Must be the tstate for this thread */
assert(tstate == current_tss_get(runtime));
return tstate == current_fast_get(runtime);
}


/* the global runtime */

Expand Down Expand Up @@ -1410,6 +1430,7 @@ _PyThreadState_Swap(_PyRuntimeState *runtime, PyThreadState *newts)
to be used for a thread. Check this the best we can in debug
builds.
*/
// XXX The above isn't true when multiple interpreters are involved.
#if defined(Py_DEBUG)
if (newts && current_tss_initialized(runtime)) {
/* This can be called from PyEval_RestoreThread(). Similar
Expand Down Expand Up @@ -1692,24 +1713,6 @@ _PyThread_CurrentExceptions(void)

/* Python "auto thread state" API. */

/* Keep this as a static, as it is not reliable! It can only
ever be compared to the state for the *current* thread.
* If not equal, then it doesn't matter that the actual
value may change immediately after comparison, as it can't
possibly change to the current thread's state.
* If equal, then the current thread holds the lock, so the value can't
change until we yield the lock.
*/
static int
PyThreadState_IsCurrent(PyThreadState *tstate)
{
assert(tstate != NULL);
_PyRuntimeState *runtime = tstate->interp->runtime;
/* Must be the tstate for this thread */
assert(tstate == current_tss_get(runtime));
return tstate == current_fast_get(runtime);
}

/* Internal initialization/finalization functions called by
Py_Initialize/Py_FinalizeEx
*/
Expand Down Expand Up @@ -1817,7 +1820,7 @@ PyGILState_Ensure(void)
assert(runtime->gilstate.autoInterpreterState != NULL);

PyThreadState *tcur = current_tss_get(runtime);
int current;
int has_gil;
if (tcur == NULL) {
/* Create a new Python thread state for this thread */
tcur = PyThreadState_New(runtime->gilstate.autoInterpreterState);
Expand All @@ -1829,13 +1832,13 @@ PyGILState_Ensure(void)
matching call to PyGILState_Release(). */
assert(tcur->gilstate_counter == 1);
tcur->gilstate_counter = 0;
current = 0; /* new thread state is never current */
has_gil = 0; /* new thread state is never current */
}
else {
current = PyThreadState_IsCurrent(tcur);
has_gil = holds_gil(tcur);
}

if (current == 0) {
if (!has_gil) {
PyEval_RestoreThread(tcur);
}

Expand All @@ -1846,7 +1849,7 @@ PyGILState_Ensure(void)
*/
++tcur->gilstate_counter;

return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
return has_gil ? PyGILState_LOCKED : PyGILState_UNLOCKED;
}

void
Expand All @@ -1864,12 +1867,12 @@ PyGILState_Release(PyGILState_STATE oldstate)
but while this is very new (April 2003), the extra check
by release-only users can't hurt.
*/
if (!PyThreadState_IsCurrent(tstate)) {
if (!holds_gil(tstate)) {
_Py_FatalErrorFormat(__func__,
"thread state %p must be current when releasing",
tstate);
}
assert(PyThreadState_IsCurrent(tstate));
assert(holds_gil(tstate));
--tstate->gilstate_counter;
assert(tstate->gilstate_counter >= 0); /* illegal counter value */

Expand All @@ -1889,8 +1892,9 @@ PyGILState_Release(PyGILState_STATE oldstate)
_PyThreadState_DeleteCurrent(tstate);
}
/* Release the lock if necessary */
else if (oldstate == PyGILState_UNLOCKED)
else if (oldstate == PyGILState_UNLOCKED) {
PyEval_SaveThread();
}
}


Expand Down
0