Skip to main content

aither_sandbox/
foreground_wait.rs

1//! The foreground window of a terminal, adjustable while commands run.
2
3use std::sync::Arc;
4use std::sync::atomic::{AtomicU64, Ordering};
5use std::time::Duration;
6
7/// How long a `terminal` call waits in the foreground before a still-running
8/// command promotes to a background task.
9///
10/// The window is a normal one plus an optional longer one that applies while
11/// it is held. Every clone shares both, so a terminal, the terminals derived
12/// from it and a host that lengthens the window while the machine is loaded
13/// all see one setting; a change applies to the next foreground wait.
14#[derive(Debug, Clone)]
15pub struct ForegroundWait(Arc<Shared>);
16
17#[derive(Debug)]
18struct Shared {
19    normal_nanos: AtomicU64,
20    /// The longer window in force, or `0` when the normal one applies.
21    extended_nanos: AtomicU64,
22}
23
24fn nanos(window: Duration) -> u64 {
25    u64::try_from(window.as_nanos()).expect("a foreground window fits in u64 nanoseconds")
26}
27
28impl ForegroundWait {
29    /// A window of `normal` with no longer window in force.
30    #[must_use]
31    pub fn new(normal: Duration) -> Self {
32        Self(Arc::new(Shared {
33            normal_nanos: AtomicU64::new(nanos(normal)),
34            extended_nanos: AtomicU64::new(0),
35        }))
36    }
37
38    /// Changes the normal window for every handle.
39    pub fn set_normal(&self, normal: Duration) {
40        self.0.normal_nanos.store(nanos(normal), Ordering::Relaxed);
41    }
42
43    /// The normal window, whether or not a longer one is in force.
44    #[must_use]
45    pub fn normal(&self) -> Duration {
46        Duration::from_nanos(self.0.normal_nanos.load(Ordering::Relaxed))
47    }
48
49    /// Puts a longer window in force until [`Self::restore`]. A zero
50    /// `window` leaves the normal one in force.
51    pub fn extend(&self, window: Duration) {
52        self.0
53            .extended_nanos
54            .store(nanos(window), Ordering::Relaxed);
55    }
56
57    /// Ends the longer window; the normal one applies again.
58    pub fn restore(&self) {
59        self.0.extended_nanos.store(0, Ordering::Relaxed);
60    }
61
62    /// The window a foreground wait that starts now uses.
63    #[must_use]
64    pub fn get(&self) -> Duration {
65        match self.0.extended_nanos.load(Ordering::Relaxed) {
66            0 => self.normal(),
67            extended => Duration::from_nanos(extended),
68        }
69    }
70}
71
72#[cfg(test)]
73mod tests {
74    use super::*;
75
76    #[test]
77    fn a_longer_window_applies_until_restored() {
78        let wait = ForegroundWait::new(Duration::from_secs(10));
79        let shared = wait.clone();
80        assert_eq!(wait.get(), Duration::from_secs(10));
81        shared.extend(Duration::from_secs(60));
82        assert_eq!(wait.get(), Duration::from_secs(60));
83        assert_eq!(wait.normal(), Duration::from_secs(10));
84        shared.restore();
85        assert_eq!(wait.get(), Duration::from_secs(10));
86    }
87}