@@ -2292,10 +2292,56 @@ detach_thread(PyThreadState *tstate, int detached_state)
22922292 _PyEval_ReleaseLock (tstate -> interp , tstate , 0 );
22932293}
22942294
2295+ #ifdef Py_GIL_DISABLED
2296+ static void
2297+ stop_after_detach (PyThreadState * tstate )
2298+ {
2299+ _PyRuntimeState * runtime = & _PyRuntime ;
2300+ PyInterpreterState * interp = tstate -> interp ;
2301+
2302+ HEAD_LOCK (runtime );
2303+ struct _stoptheworld_state * stw = NULL ;
2304+ if (runtime -> stoptheworld .requested ) {
2305+ stw = & runtime -> stoptheworld ;
2306+ }
2307+ else if (interp -> stoptheworld .requested ) {
2308+ stw = & interp -> stoptheworld ;
2309+ }
2310+ if (stw != NULL && stw -> requester != tstate ) {
2311+ int expected = _Py_THREAD_DETACHED ;
2312+ if (!_Py_atomic_compare_exchange_int (& tstate -> state , & expected ,
2313+ _Py_THREAD_SUSPENDED )) {
2314+ // BRC may have suspended this thread without counting it for STW.
2315+ // Leave the stop bit set so it sees the request when it reattaches.
2316+ HEAD_UNLOCK (runtime );
2317+ return ;
2318+ }
2319+ decrement_stoptheworld_countdown (stw );
2320+ }
2321+ // The request was handled or the bit was stale. Requests set the bit
2322+ // while holding HEAD_LOCK, so clearing it here cannot lose one.
2323+ _Py_unset_eval_breaker_bit (tstate , _PY_EVAL_PLEASE_STOP_BIT );
2324+ #if defined(Py_REMOTE_DEBUG ) && defined(Py_SUPPORTS_REMOTE_DEBUG )
2325+ if (tstate -> remote_debugger_support .debugger_pending_call ) {
2326+ // A remote debugger also sets the bit, to get the thread to call
2327+ // _Py_HandlePending().
2328+ _Py_set_eval_breaker_bit (tstate , _PY_EVAL_PLEASE_STOP_BIT );
2329+ }
2330+ #endif
2331+ HEAD_UNLOCK (runtime );
2332+ }
2333+ #endif
2334+
22952335void
22962336_PyThreadState_Detach (PyThreadState * tstate )
22972337{
22982338 detach_thread (tstate , _Py_THREAD_DETACHED );
2339+ #ifdef Py_GIL_DISABLED
2340+ uintptr_t breaker = _Py_atomic_load_uintptr (& tstate -> eval_breaker );
2341+ if ((breaker & _PY_EVAL_PLEASE_STOP_BIT ) != 0 ) {
2342+ stop_after_detach (tstate );
2343+ }
2344+ #endif
22992345}
23002346
23012347void
@@ -2393,6 +2439,30 @@ interp_for_stop_the_world(struct _stoptheworld_state *stw)
23932439 i != NULL; i = ((stw->is_global) ? i->next : NULL))
23942440
23952441
2442+ static bool
2443+ park_or_ask_to_stop (struct _stoptheworld_state * stw , PyThreadState * t )
2444+ {
2445+ if (t == stw -> requester ) {
2446+ return false;
2447+ }
2448+ int state = _Py_THREAD_DETACHED ;
2449+ if (_Py_atomic_compare_exchange_int (& t -> state , & state ,
2450+ _Py_THREAD_SUSPENDED )) {
2451+ return true;
2452+ }
2453+ if (state != _Py_THREAD_ATTACHED ) {
2454+ return false;
2455+ }
2456+
2457+ _Py_set_eval_breaker_bit (t , _PY_EVAL_PLEASE_STOP_BIT );
2458+
2459+ // The thread may have detached before the bit was set and missed it.
2460+ // Try to park it once more; a later _PyThreadState_Detach() sees the bit.
2461+ state = _Py_THREAD_DETACHED ;
2462+ return _Py_atomic_compare_exchange_int (& t -> state , & state ,
2463+ _Py_THREAD_SUSPENDED );
2464+ }
2465+
23962466// Try to transition threads atomically from the "detached" state to the
23972467// "gc stopped" state. Returns true if all threads are in the "gc stopped"
23982468static bool
@@ -2401,16 +2471,8 @@ park_detached_threads(struct _stoptheworld_state *stw)
24012471 int num_parked = 0 ;
24022472 _Py_FOR_EACH_STW_INTERP (stw , i ) {
24032473 _Py_FOR_EACH_TSTATE_UNLOCKED (i , t ) {
2404- int state = _Py_atomic_load_int_relaxed (& t -> state );
2405- if (state == _Py_THREAD_DETACHED ) {
2406- // Atomically transition to "suspended" if in "detached" state.
2407- if (_Py_atomic_compare_exchange_int (
2408- & t -> state , & state , _Py_THREAD_SUSPENDED )) {
2409- num_parked ++ ;
2410- }
2411- }
2412- else if (state == _Py_THREAD_ATTACHED && t != stw -> requester ) {
2413- _Py_set_eval_breaker_bit (t , _PY_EVAL_PLEASE_STOP_BIT );
2474+ if (park_or_ask_to_stop (stw , t )) {
2475+ num_parked ++ ;
24142476 }
24152477 }
24162478 }
0 commit comments