Skip to content

Commit 29fa419

Browse files
committed
Simplify locking in make_pre_finalization_calls.
1 parent f9cb8f7 commit 29fa419

1 file changed

Lines changed: 6 additions & 17 deletions

File tree

Python/pylifecycle.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,10 +2225,11 @@ resolve_final_tstate(_PyRuntimeState *runtime)
22252225
#endif
22262226

22272227
static int
2228-
interp_has_threads_locked(PyInterpreterState *interp)
2228+
interp_has_threads(PyInterpreterState *interp)
22292229
{
22302230
/* This needs to check for non-daemon threads only, otherwise we get stuck
22312231
* in an infinite loop. */
2232+
assert(interp != NULL);
22322233
ASSERT_HEAD_IS_LOCKED(interp->runtime);
22332234
assert(interp->threads.head != NULL);
22342235
if (interp->threads.head->next == NULL) {
@@ -2245,17 +2246,6 @@ interp_has_threads_locked(PyInterpreterState *interp)
22452246
return 0;
22462247
}
22472248

2248-
static int
2249-
interp_has_threads(PyInterpreterState *interp)
2250-
{
2251-
assert(interp != NULL);
2252-
ASSERT_WORLD_STOPPED(interp);
2253-
HEAD_LOCK(interp->runtime);
2254-
int res = interp_has_threads_locked(interp);
2255-
HEAD_UNLOCK(interp->runtime);
2256-
return res;
2257-
}
2258-
22592249
static int
22602250
interp_has_pending_calls(PyInterpreterState *interp)
22612251
{
@@ -2278,9 +2268,7 @@ static int
22782268
runtime_has_subinterpreters(_PyRuntimeState *runtime)
22792269
{
22802270
assert(runtime != NULL);
2281-
HEAD_LOCK(runtime);
22822271
PyInterpreterState *interp = runtime->interpreters.head;
2283-
HEAD_UNLOCK(runtime);
22842272
return interp->next != NULL;
22852273
}
22862274

@@ -2359,11 +2347,10 @@ make_pre_finalization_calls(PyThreadState *tstate, int subinterpreters)
23592347
* the GIL. For pending calls, we acquire the dedicated mutex, because
23602348
* Py_AddPendingCall() can be called without an attached thread state.
23612349
*/
2362-
23632350
PyMutex_Lock(&interp->ceval.pending.mutex);
2364-
// XXX Why does _PyThreadState_DeleteList() rely on all interpreters
2365-
// being stopped?
23662351
_PyEval_StopTheWorldAll(interp->runtime);
2352+
2353+
HEAD_LOCK(interp->runtime);
23672354
int has_subinterpreters = subinterpreters
23682355
? runtime_has_subinterpreters(interp->runtime)
23692356
: 0;
@@ -2372,6 +2359,8 @@ make_pre_finalization_calls(PyThreadState *tstate, int subinterpreters)
23722359
|| interp_has_atexit_callbacks(interp)
23732360
|| interp_has_pending_calls(interp)
23742361
|| has_subinterpreters);
2362+
HEAD_UNLOCK(interp->runtime);
2363+
23752364
if (!should_continue) {
23762365
// We only want to prevent new guards once we're sure that we
23772366
// won't be running another pre-finalization cycle.

0 commit comments

Comments
 (0)