Skip to main content

mofa_runtime/
interrupt.rs

1use std::sync::Arc;
2use tokio::sync::Notify;
3
4// 共享中断标记(用于外部触发中断)
5#[derive(Clone)]
6pub struct AgentInterrupt {
7    pub notify: Arc<Notify>,
8    pub is_interrupted: Arc<std::sync::atomic::AtomicBool>,
9}
10
11impl Default for AgentInterrupt {
12    fn default() -> Self {
13        Self::new()
14    }
15}
16
17impl AgentInterrupt {
18    pub fn new() -> Self {
19        Self {
20            notify: Arc::new(Notify::new()),
21            is_interrupted: Arc::new(std::sync::atomic::AtomicBool::new(false)),
22        }
23    }
24
25    // 触发中断
26    pub fn trigger(&self) {
27        self.is_interrupted
28            .store(true, std::sync::atomic::Ordering::SeqCst);
29        self.notify.notify_one();
30    }
31
32    // 检查是否中断
33    pub fn check(&self) -> bool {
34        self.is_interrupted
35            .load(std::sync::atomic::Ordering::SeqCst)
36    }
37
38    // 重置中断状态
39    pub fn reset(&self) {
40        self.is_interrupted
41            .store(false, std::sync::atomic::Ordering::SeqCst);
42    }
43}