Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ extern void _PyThreadState_Suspend(PyThreadState *tstate);
// Returns 1 on success, 0 if the thread was not in the "detached" state.
extern int _PyThreadState_TrySuspendDetached(PyThreadState *tstate);

// Undo a successful _PyThreadState_TrySuspendDetached(): switch the thread
// back to "detached" and wake it if it is waiting to attach.
// Resume a thread suspended by _PyThreadState_TrySuspendDetached() or a
// stop-the-world pause: switch it back to "detached" or "detached-waiting"
// and wake it if it is waiting to attach.
extern void _PyThreadState_ResumeDetached(PyThreadState *tstate);
#endif

Expand Down
46 changes: 18 additions & 28 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2401,10 +2401,24 @@ void
_PyThreadState_ResumeDetached(PyThreadState *tstate)
{
assert(tstate != _PyThreadState_GET());
assert(_Py_atomic_load_int_relaxed(&tstate->state) == _Py_THREAD_SUSPENDED);
_Py_atomic_store_int(&tstate->state, _Py_THREAD_DETACHED);
int state = _Py_atomic_load_int_relaxed(&tstate->state);
int next_state;
do {
assert(state == _Py_THREAD_SUSPENDED ||
state == _Py_THREAD_SUSPENDED_WAITING);
if (state == _Py_THREAD_SUSPENDED_WAITING) {
next_state = _Py_THREAD_DETACHED_WAITING;
}
else {
next_state = _Py_THREAD_DETACHED;
}
// Retry if an attach waiter registered concurrently.
} while (!_Py_atomic_compare_exchange_int(
&tstate->state, &state, next_state));
// Wake the thread if it is parked in tstate_wait_attach().
_PyParkingLot_UnparkAll(&tstate->state);
if (state == _Py_THREAD_SUSPENDED_WAITING) {
_PyParkingLot_UnparkAll(&tstate->state);
}
}
#endif

Expand Down Expand Up @@ -2526,30 +2540,6 @@ stop_the_world(struct _stoptheworld_state *stw)
stw->world_stopped = 1;
}

static void
tstate_resume(PyThreadState *tstate)
{
assert(tstate != _PyThreadState_GET());
int state = _Py_atomic_load_int_relaxed(&tstate->state);
int next_state;
do {
assert(state == _Py_THREAD_SUSPENDED ||
state == _Py_THREAD_SUSPENDED_WAITING);
if (state == _Py_THREAD_SUSPENDED_WAITING) {
next_state = _Py_THREAD_DETACHED_WAITING;
}
else {
next_state = _Py_THREAD_DETACHED;
}
// Retry if an attach waiter registered concurrently.
} while (!_Py_atomic_compare_exchange_int(
&tstate->state, &state, next_state));
// Wake the thread if it is parked in tstate_wait_attach().
if (state == _Py_THREAD_SUSPENDED_WAITING) {
_PyParkingLot_UnparkAll(&tstate->state);
}
}

static void
start_the_world(struct _stoptheworld_state *stw)
{
Expand All @@ -2563,7 +2553,7 @@ start_the_world(struct _stoptheworld_state *stw)
_Py_FOR_EACH_STW_INTERP(stw, i) {
_Py_FOR_EACH_TSTATE_UNLOCKED(i, t) {
if (t != stw->requester) {
tstate_resume(t);
_PyThreadState_ResumeDetached(t);
}
}
}
Expand Down
Loading