Skip to content

Commit 6aa9d1d

Browse files
committed
gh-162764: Avoid 1 ms STW delay when a thread detaches
When detaching, check if STW is requested. If so, move from DETACHED to SUSPENDED and decrement countdown. Also, threads waiting to re-attach after a previous pause are asked to stop.
1 parent cb9d78f commit 6aa9d1d

3 files changed

Lines changed: 52 additions & 6 deletions

File tree

‎Include/internal/pycore_interp_structs.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,9 @@ struct _stoptheworld_state {
413413
PyMutex mutex; // Serializes stop-the-world attempts.
414414

415415
// NOTE: The below fields are protected by HEAD_LOCK(runtime), not by the
416-
// above mutex.
417-
bool requested; // Set when a pause is requested.
416+
// above mutex. `requested` is written atomically so that detaching
417+
// threads can check it without holding HEAD_LOCK.
418+
int requested; // Set when a pause is requested.
418419
bool world_stopped; // Set when the world is stopped.
419420
bool is_global; // Set when contained in PyRuntime struct.
420421

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Reduce the latency of stop-the-world pauses in the :term:`free-threaded build`.
2+
Threads that detach (for example, to block on a lock or around a blocking
3+
call) while a pause is pending now notify the requesting thread, and threads
4+
waiting to re-attach after a previous pause are asked to stop. Previously,
5+
such pauses could be delayed by about one millisecond.

‎Python/pystate.c‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,10 +2457,46 @@ detach_thread(PyThreadState *tstate, int detached_state)
24572457
_PyEval_ReleaseLock(tstate->interp, tstate, 0);
24582458
}
24592459

2460+
#ifdef Py_GIL_DISABLED
2461+
// Called after the current thread switched to the "detached" state.
2462+
static void
2463+
notify_stw_on_detach(PyThreadState *tstate)
2464+
{
2465+
_PyRuntimeState *runtime = &_PyRuntime;
2466+
PyInterpreterState *interp = tstate->interp;
2467+
2468+
if (!_Py_atomic_load_int(&runtime->stoptheworld.requested) &&
2469+
!_Py_atomic_load_int(&interp->stoptheworld.requested)) {
2470+
return;
2471+
}
2472+
2473+
HEAD_LOCK(runtime);
2474+
struct _stoptheworld_state *stw = NULL;
2475+
if (runtime->stoptheworld.requested) {
2476+
stw = &runtime->stoptheworld;
2477+
}
2478+
else if (interp->stoptheworld.requested) {
2479+
stw = &interp->stoptheworld;
2480+
}
2481+
if (stw != NULL && stw->requester != tstate) {
2482+
// If the CAS fails, the requester already parked this thread.
2483+
int expected = _Py_THREAD_DETACHED;
2484+
if (_Py_atomic_compare_exchange_int(&tstate->state, &expected,
2485+
_Py_THREAD_SUSPENDED)) {
2486+
decrement_stoptheworld_countdown(stw);
2487+
}
2488+
}
2489+
HEAD_UNLOCK(runtime);
2490+
}
2491+
#endif
2492+
24602493
void
24612494
_PyThreadState_Detach(PyThreadState *tstate)
24622495
{
24632496
detach_thread(tstate, _Py_THREAD_DETACHED);
2497+
#ifdef Py_GIL_DISABLED
2498+
notify_stw_on_detach(tstate);
2499+
#endif
24642500
}
24652501

24662502
void
@@ -2580,7 +2616,7 @@ park_detached_threads(struct _stoptheworld_state *stw)
25802616
int num_parked = 0;
25812617
_Py_FOR_EACH_STW_INTERP(stw, i) {
25822618
_Py_FOR_EACH_TSTATE_UNLOCKED(i, t) {
2583-
int state = _Py_atomic_load_int_relaxed(&t->state);
2619+
int state = _Py_atomic_load_int(&t->state);
25842620
// DETACHED_WAITING threads remain counted until they attach and
25852621
// stop, so repeated pauses cannot prevent them from attaching.
25862622
if (state == _Py_THREAD_DETACHED) {
@@ -2590,7 +2626,11 @@ park_detached_threads(struct _stoptheworld_state *stw)
25902626
num_parked++;
25912627
}
25922628
}
2593-
else if (state == _Py_THREAD_ATTACHED && t != stw->requester) {
2629+
else if ((state == _Py_THREAD_ATTACHED ||
2630+
state == _Py_THREAD_DETACHED_WAITING) &&
2631+
t != stw->requester) {
2632+
// Ask attached threads to stop. A DETACHED_WAITING thread
2633+
// will attach soon, so ask it to stop once it is attached.
25942634
_Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT);
25952635
}
25962636
}
@@ -2616,7 +2656,7 @@ stop_the_world(struct _stoptheworld_state *stw)
26162656
PyMutex_Lock(&stw->mutex);
26172657

26182658
HEAD_LOCK(runtime);
2619-
stw->requested = 1;
2659+
_Py_atomic_store_int(&stw->requested, 1);
26202660
stw->thread_countdown = 0;
26212661
stw->stop_event = (PyEvent){0}; // zero-initialize (unset)
26222662
stw->requester = _PyThreadState_GET(); // may be NULL
@@ -2665,7 +2705,7 @@ start_the_world(struct _stoptheworld_state *stw)
26652705
assert(PyMutex_IsLocked(&stw->mutex));
26662706

26672707
HEAD_LOCK(runtime);
2668-
stw->requested = 0;
2708+
_Py_atomic_store_int(&stw->requested, 0);
26692709
stw->world_stopped = 0;
26702710
// Switch threads back to the detached state.
26712711
_Py_FOR_EACH_STW_INTERP(stw, i) {

0 commit comments

Comments
 (0)