@@ -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+
24602493void
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
24662502void
@@ -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