Skip to main content

im_core/realtime/
control.rs

1use std::sync::{
2    atomic::{AtomicBool, Ordering},
3    Arc,
4};
5
6#[derive(Clone)]
7pub struct RealtimeControl {
8    closed: Arc<AtomicBool>,
9}
10
11impl RealtimeControl {
12    pub(crate) fn new() -> Self {
13        Self {
14            closed: Arc::new(AtomicBool::new(false)),
15        }
16    }
17
18    pub fn shutdown(&self) {
19        self.closed.store(true, Ordering::SeqCst);
20    }
21
22    pub fn close(&self) {
23        self.shutdown();
24    }
25
26    pub fn is_closed(&self) -> bool {
27        self.closed.load(Ordering::SeqCst)
28    }
29}
30
31impl Default for RealtimeControl {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37impl std::fmt::Debug for RealtimeControl {
38    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        formatter
40            .debug_struct("RealtimeControl")
41            .field("closed", &self.is_closed())
42            .finish()
43    }
44}
45
46impl PartialEq for RealtimeControl {
47    fn eq(&self, other: &Self) -> bool {
48        self.is_closed() == other.is_closed()
49    }
50}
51
52impl Eq for RealtimeControl {}
53
54#[derive(Clone)]
55pub struct ShutdownSignal {
56    requested: Arc<AtomicBool>,
57}
58
59impl ShutdownSignal {
60    pub fn pending() -> Self {
61        Self {
62            requested: Arc::new(AtomicBool::new(false)),
63        }
64    }
65
66    pub fn requested() -> Self {
67        let signal = Self::pending();
68        signal.request();
69        signal
70    }
71
72    pub fn request(&self) {
73        self.requested.store(true, Ordering::SeqCst);
74    }
75
76    pub fn is_requested(&self) -> bool {
77        self.requested.load(Ordering::SeqCst)
78    }
79}
80
81impl std::fmt::Debug for ShutdownSignal {
82    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        formatter
84            .debug_struct("ShutdownSignal")
85            .field("requested", &self.is_requested())
86            .finish()
87    }
88}
89
90impl PartialEq for ShutdownSignal {
91    fn eq(&self, other: &Self) -> bool {
92        self.is_requested() == other.is_requested()
93    }
94}
95
96impl Eq for ShutdownSignal {}