@@ -2428,10 +2428,56 @@ detach_thread(PyThreadState *tstate, int detached_state)
24282428 _PyEval_ReleaseLock(tstate->interp, tstate, 0);
24292429}
24302430
2431+ #ifdef Py_GIL_DISABLED
2432+ static void
2433+ stop_after_detach(PyThreadState *tstate)
2434+ {
2435+ _PyRuntimeState *runtime = &_PyRuntime;
2436+ PyInterpreterState *interp = tstate->interp;
2437+
2438+ HEAD_LOCK(runtime);
2439+ struct _stoptheworld_state *stw = NULL;
2440+ if (runtime->stoptheworld.requested) {
2441+ stw = &runtime->stoptheworld;
2442+ }
2443+ else if (interp->stoptheworld.requested) {
2444+ stw = &interp->stoptheworld;
2445+ }
2446+ if (stw != NULL && stw->requester != tstate) {
2447+ int expected = _Py_THREAD_DETACHED;
2448+ if (!_Py_atomic_compare_exchange_int(&tstate->state, &expected,
2449+ _Py_THREAD_SUSPENDED)) {
2450+ // BRC may have suspended this thread without counting it for STW.
2451+ // Leave the stop bit set so it sees the request when it reattaches.
2452+ HEAD_UNLOCK(runtime);
2453+ return;
2454+ }
2455+ decrement_stoptheworld_countdown(stw);
2456+ }
2457+ // The request was handled or the bit was stale. Requests set the bit
2458+ // while holding HEAD_LOCK, so clearing it here cannot lose one.
2459+ _Py_unset_eval_breaker_bit(tstate, _PY_EVAL_PLEASE_STOP_BIT);
2460+ #if defined(Py_REMOTE_DEBUG) && defined(Py_SUPPORTS_REMOTE_DEBUG)
2461+ if (tstate->remote_debugger_support.debugger_pending_call) {
2462+ // A remote debugger also sets the bit, to get the thread to call
2463+ // _Py_HandlePending().
2464+ _Py_set_eval_breaker_bit(tstate, _PY_EVAL_PLEASE_STOP_BIT);
2465+ }
2466+ #endif
2467+ HEAD_UNLOCK(runtime);
2468+ }
2469+ #endif
2470+
24312471void
24322472_PyThreadState_Detach(PyThreadState *tstate)
24332473{
24342474 detach_thread(tstate, _Py_THREAD_DETACHED);
2475+ #ifdef Py_GIL_DISABLED
2476+ uintptr_t breaker = _Py_atomic_load_uintptr(&tstate->eval_breaker);
2477+ if ((breaker & _PY_EVAL_PLEASE_STOP_BIT) != 0) {
2478+ stop_after_detach(tstate);
2479+ }
2480+ #endif
24352481}
24362482
24372483void
@@ -2543,6 +2589,32 @@ interp_for_stop_the_world(struct _stoptheworld_state *stw)
25432589 i != NULL; i = ((stw->is_global) ? i->next : NULL))
25442590
25452591
2592+ static bool
2593+ park_or_ask_to_stop(struct _stoptheworld_state *stw, PyThreadState *t)
2594+ {
2595+ if (t == stw->requester) {
2596+ return false;
2597+ }
2598+ int state = _Py_THREAD_DETACHED;
2599+ if (_Py_atomic_compare_exchange_int(&t->state, &state,
2600+ _Py_THREAD_SUSPENDED)) {
2601+ return true;
2602+ }
2603+ if (state != _Py_THREAD_ATTACHED && state != _Py_THREAD_DETACHED_WAITING) {
2604+ return false;
2605+ }
2606+
2607+ // Let a DETACHED_WAITING thread attach before stopping it, so repeated
2608+ // pauses cannot starve its attach attempt.
2609+ _Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT);
2610+
2611+ // The thread may have detached before the bit was set and missed it.
2612+ // Try to park it once more; a later _PyThreadState_Detach() sees the bit.
2613+ state = _Py_THREAD_DETACHED;
2614+ return _Py_atomic_compare_exchange_int(&t->state, &state,
2615+ _Py_THREAD_SUSPENDED);
2616+ }
2617+
25462618// Try to transition threads atomically from the "detached" state to the
25472619// "gc stopped" state. Returns true if all threads are in the "gc stopped"
25482620static bool
@@ -2551,18 +2623,8 @@ park_detached_threads(struct _stoptheworld_state *stw)
25512623 int num_parked = 0;
25522624 _Py_FOR_EACH_STW_INTERP(stw, i) {
25532625 _Py_FOR_EACH_TSTATE_UNLOCKED(i, t) {
2554- int state = _Py_atomic_load_int_relaxed(&t->state);
2555- // DETACHED_WAITING threads remain counted until they attach and
2556- // stop, so repeated pauses cannot prevent them from attaching.
2557- if (state == _Py_THREAD_DETACHED) {
2558- // Atomically transition to "suspended" if in "detached" state.
2559- if (_Py_atomic_compare_exchange_int(
2560- &t->state, &state, _Py_THREAD_SUSPENDED)) {
2561- num_parked++;
2562- }
2563- }
2564- else if (state == _Py_THREAD_ATTACHED && t != stw->requester) {
2565- _Py_set_eval_breaker_bit(t, _PY_EVAL_PLEASE_STOP_BIT);
2626+ if (park_or_ask_to_stop(stw, t)) {
2627+ num_parked++;
25662628 }
25672629 }
25682630 }
0 commit comments