fn wait_for_machine_to_be_idle()

in focus/operations/src/sync.rs [414:463]


fn wait_for_machine_to_be_idle(
    idle_duration: Duration,
    max_wait: Duration,
    poll_interval: Duration,
) -> Result<bool> {
    use focus_platform::session_state;

    if max_wait < idle_duration {
        bail!("max_wait must be greater than idle_duration")
    } else if poll_interval > max_wait {
        bail!("poll_interval must be less than max_wait")
    }

    let started_at = SystemTime::now();
    loop {
        let elapsed = started_at
            .elapsed()
            .context("Determining elapsed time failed")?;
        if elapsed > max_wait {
            break;
        }
        let state = {
            // If we are running under test, read from a variable instead of doing any polling.
            if cfg!(test) {
                debug!("Running under test!");
                if test_only_get_preemptive_sync_machine_is_active() {
                    debug!("Pretending machine is active");
                    session_state::SessionStatus::Active
                } else {
                    debug!("Pretending machine is idle");
                    session_state::SessionStatus::Idle
                }
            } else {
                unsafe { session_state::has_session_been_idle_for(idle_duration) }
            }
        };

        match state {
            session_state::SessionStatus::Active => {
                std::thread::sleep(poll_interval);
            }
            _ => {
                // Note: If we can't determine whether the session is idle, just go ahead.
                return Ok(true);
            }
        }
    }

    Ok(false)
}