Skip to main content

scx_forge_agent/
interrupt.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: GPL-2.0-only
3
4use std::fmt;
5use std::sync::atomic::{AtomicBool, Ordering};
6use std::sync::Arc;
7use std::time::Duration;
8
9use anyhow::Error;
10
11#[derive(Debug)]
12pub struct Interrupted;
13
14impl fmt::Display for Interrupted {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        f.write_str("interrupted by Ctrl-C")
17    }
18}
19
20impl std::error::Error for Interrupted {}
21
22pub fn requested(interrupted: &AtomicBool) -> bool {
23    interrupted.load(Ordering::SeqCst)
24}
25
26pub fn err() -> Error {
27    Interrupted.into()
28}
29
30pub fn is(err: &Error) -> bool {
31    err.downcast_ref::<Interrupted>().is_some()
32}
33
34pub async fn wait(interrupted: Arc<AtomicBool>) {
35    while !requested(&interrupted) {
36        tokio::time::sleep(Duration::from_millis(100)).await;
37    }
38}