Skip to content

Commit 0c2ce02

Browse files
committed
Fix some other missing cases.
1 parent 4371c15 commit 0c2ce02

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

Include/internal/pycore_interp_structs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ struct _is {
968968
struct {
969969
_PyRWMutex lock;
970970
Py_ssize_t countdown;
971-
} finalization_locks;
971+
} finalization_guards;
972972

973973
/* the initial PyInterpreterState.threads.head */
974974
_PyThreadStateImpl _initial_thread;

Python/pylifecycle.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,22 +2130,23 @@ make_pre_finalization_calls(PyThreadState *tstate, int subinterpreters)
21302130
// XXX Why does _PyThreadState_DeleteList() rely on all interpreters
21312131
// being stopped?
21322132
_PyEval_StopTheWorldAll(interp->runtime);
2133-
_PyRWMutex_Lock(&interp->finalization_locks.lock);
2133+
_PyRWMutex_Lock(&interp->finalization_guards.lock);
21342134
int has_subinterpreters = subinterpreters
21352135
? runtime_has_subinterpreters(interp->runtime)
21362136
: 0;
2137-
// TODO: The interpreter reference countdown probably isn't very efficient.
2137+
// TODO: The interpreter guard countdown isn't very efficient. We should
2138+
// wait on an event or something like that.
21382139
int should_continue = (interp_has_threads(interp)
21392140
|| interp_has_atexit_callbacks(interp)
21402141
|| interp_has_pending_calls(interp)
21412142
|| has_subinterpreters
2142-
|| interp->finalization_locks.countdown > 0);
2143+
|| interp->finalization_guards.countdown > 0);
21432144
if (!should_continue) {
21442145
break;
21452146
}
21462147
// Temporarily let other threads execute
21472148
_PyThreadState_Detach(tstate);
2148-
_PyRWMutex_Unlock(&interp->finalization_locks.lock);
2149+
_PyRWMutex_Unlock(&interp->finalization_guards.lock);
21492150
_PyEval_StartTheWorldAll(interp->runtime);
21502151
PyMutex_Unlock(&interp->ceval.pending.mutex);
21512152
_PyThreadState_Attach(tstate);
@@ -2209,7 +2210,7 @@ _Py_Finalize(_PyRuntimeState *runtime)
22092210
for (PyThreadState *p = list; p != NULL; p = p->next) {
22102211
_PyThreadState_SetShuttingDown(p);
22112212
}
2212-
_PyRWMutex_Unlock(&tstate->interp->finalization_locks.lock);
2213+
_PyRWMutex_Unlock(&tstate->interp->finalization_guards.lock);
22132214
_PyEval_StartTheWorldAll(runtime);
22142215
PyMutex_Unlock(&tstate->interp->ceval.pending.mutex);
22152216

@@ -2579,7 +2580,7 @@ Py_EndInterpreter(PyThreadState *tstate)
25792580
_PyThreadState_SetShuttingDown(p);
25802581
}
25812582

2582-
_PyRWMutex_Unlock(&interp->finalization_locks.lock);
2583+
_PyRWMutex_Unlock(&interp->finalization_guards.lock);
25832584
_PyEval_StartTheWorldAll(interp->runtime);
25842585
PyMutex_Unlock(&interp->ceval.pending.mutex);
25852586
_PyThreadState_DeleteList(list, /*is_after_fork=*/0);

Python/pystate.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,19 +3147,19 @@ Py_ssize_t
31473147
_PyInterpreterState_LockCountdown(PyInterpreterState *interp)
31483148
{
31493149
assert(interp != NULL);
3150-
return _Py_atomic_load_ssize_relaxed(&interp->finalization_locks.countdown);
3150+
return _Py_atomic_load_ssize_relaxed(&interp->finalization_guards.countdown);
31513151
}
31523152

31533153
static PyInterpreterGuard
31543154
try_acquire_interp_guard(PyInterpreterState *interp)
31553155
{
3156-
_PyRWMutex_RLock(&interp->finalization_locks.lock);
3156+
_PyRWMutex_RLock(&interp->finalization_guards.lock);
31573157
if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
3158-
_PyRWMutex_RUnlock(&interp->finalization_locks.lock);
3158+
_PyRWMutex_RUnlock(&interp->finalization_guards.lock);
31593159
return (PyInterpreterGuard)interp;
31603160
}
3161-
_Py_atomic_add_ssize(&interp->finalization_locks.countdown, 1);
3162-
_PyRWMutex_RUnlock(&interp->finalization_locks.lock);
3161+
_Py_atomic_add_ssize(&interp->finalization_guards.countdown, 1);
3162+
_PyRWMutex_RUnlock(&interp->finalization_guards.lock);
31633163
return (PyInterpreterGuard)interp;
31643164
}
31653165

@@ -3204,9 +3204,9 @@ PyInterpreterGuard_Release(PyInterpreterGuard guard)
32043204
{
32053205
PyInterpreterState *interp = guard_as_interp(guard);
32063206
assert(interp != NULL);
3207-
_PyRWMutex_RLock(&interp->finalization_locks.lock);
3208-
Py_ssize_t old = _Py_atomic_add_ssize(&interp->finalization_locks.countdown, -1);
3209-
_PyRWMutex_RUnlock(&interp->finalization_locks.lock);
3207+
_PyRWMutex_RLock(&interp->finalization_guards.lock);
3208+
Py_ssize_t old = _Py_atomic_add_ssize(&interp->finalization_guards.countdown, -1);
3209+
_PyRWMutex_RUnlock(&interp->finalization_guards.lock);
32103210
if (old <= 0) {
32113211
Py_FatalError("interpreter has negative guard count, likely due"
32123212
" to an extra PyInterpreterGuard_Release() call");

0 commit comments

Comments
 (0)