| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "src/developer/debug/debug_agent/process_handle.h" | |
| #include <chrono> | |
| #include "src/developer/debug/debug_agent/thread_handle.h" | |
| namespace debug_agent { | |
| bool WaitForThreadSuspensionBeforeDeadline(const ThreadHandle& handle, TickTimePoint deadline) { | |
| if (std::chrono::steady_clock::now() >= deadline) { | |
| return false; | |
| } | |
| if (handle.WaitForSuspension(deadline)) { | |
| return true; | |
| } | |
| debug_ipc::ThreadRecord::State state = handle.GetState().state; | |
| return state == debug_ipc::ThreadRecord::State::kDying || | |
| state == debug_ipc::ThreadRecord::State::kDead; | |
| } | |
| ProcessHandle::WaitForThreadsSuspendedResult ProcessHandle::WaitForThreadsSuspended( | |
| TickTimePoint deadline) const { | |
| WaitForThreadsSuspendedResult result; | |
| bool all_settled = true; | |
| bool converged = false; | |
| for (size_t pass = 0; pass < kMaxThreadCoalescePasses; ++pass) { | |
| bool found_live_thread = false; | |
| for (const auto& thread : GetChildThreads()) { | |
| if (thread && result.waited_koids.insert(thread->GetKoid()).second) { | |
| debug_ipc::ThreadRecord::State state = thread->GetState().state; | |
| if (state != debug_ipc::ThreadRecord::State::kDying && | |
| state != debug_ipc::ThreadRecord::State::kDead) { | |
| found_live_thread = true; | |
| if (!WaitForThreadSuspensionBeforeDeadline(*thread, deadline)) { | |
| all_settled = false; | |
| } | |
| } | |
| } | |
| } | |
| if (!found_live_thread) { | |
| converged = true; | |
| break; | |
| } | |
| if (std::chrono::steady_clock::now() >= deadline) { | |
| break; | |
| } | |
| } | |
| result.settled = all_settled && converged; | |
| return result; | |
| } | |
| } // namespace debug_agent |