wip: refactor(tengri): routines!

This commit is contained in:
same mf who else 2026-03-09 02:06:30 +02:00
parent 8f0a2accce
commit 4e8d58d793
9 changed files with 1495 additions and 1669 deletions

View file

@ -1,5 +1,64 @@
use crate::*;
#[derive(Clone)] pub struct Exit(Arc<AtomicBool>);
impl Exit {
pub fn run <T> (run: impl Fn()->Usually<T>) -> Usually<T> {
run(Self(Arc::new(AtomicBool::new(false))))
}
}
#[derive(Debug)] pub struct Thread {
/// Exit flag.
pub exit: Arc<AtomicBool>,
/// Performance counter.
pub perf: Arc<PerfModel>,
/// Use this to wait for the thread to finish.
pub join: JoinHandle<()>,
}
impl Thread {
/// Spawn a TUI thread that runs `callt least one, then repeats until `exit`.
pub fn new <F> (exit: Arc<AtomicBool>, call: F) -> Result<Self, std::io::Error>
where F: Fn(&PerfModel)->() + Send + Sync + 'static
{
let perf = Arc::new(PerfModel::default());
Ok(Self {
exit: exit.clone(),
perf: perf.clone(),
join: std::thread::Builder::new().name("tengri tui output".into()).spawn(move || {
while !exit.fetch_and(true, Relaxed) {
let _ = perf.cycle(&call);
}
})?.into()
})
}
/// Spawn a TUI thread that runs `callt least one, then repeats
/// until `exit`, sleeping for `time` msec after every iteration.
pub fn new_sleep <F> (
exit: Arc<AtomicBool>, time: Duration, call: F
) -> Result<Self, std::io::Error>
where F: Fn(&PerfModel)->() + Send + Sync + 'static
{
Self::new(exit, move |perf| { let _ = call(perf); std::thread::sleep(time); })
}
/// Spawn a TUI thread that runs `callt least one, then repeats
/// until `exit`, using polling to run every `time` msec.
pub fn new_poll <F> (
exit: Arc<AtomicBool>, time: Duration, call: F
) -> Result<Self, std::io::Error>
where F: Fn(&PerfModel)->() + Send + Sync + 'static
{
Self::new(exit, move |perf| { if poll(time).is_ok() { let _ = call(perf); } })
}
pub fn join (self) -> Result<(), Box<dyn std::any::Any + Send>> {
self.join.join()
}
}
/// Define an enum containing commands, and implement [Command] trait for over given `State`.
#[macro_export] macro_rules! def_command (
($Command:ident: |$state:ident: $State:ty| {
@ -39,3 +98,48 @@ use crate::*;
}
}
}
impl<S, T: Action<S>> Action<S> for Option<T> { fn action (&self, _: &mut S) -> Perhaps<Self> { Ok(None) } }
impl_default!(PerfModel: Self {
enabled: true,
clock: quanta::Clock::new(),
used: Default::default(),
window: Default::default(),
});
impl PerfModel {
pub fn get_t0 (&self) -> Option<u64> {
if self.enabled {
Some(self.clock.raw())
} else {
None
}
}
pub fn get_t1 (&self, t0: Option<u64>) -> Option<std::time::Duration> {
if let Some(t0) = t0 {
if self.enabled {
Some(self.clock.delta(t0, self.clock.raw()))
} else {
None
}
} else {
None
}
}
pub fn update (&self, t0: Option<u64>, microseconds: f64) {
if let Some(t0) = t0 {
let t1 = self.clock.raw();
self.used.store(self.clock.delta_as_nanos(t0, t1) as f64, Relaxed);
self.window.store(microseconds, Relaxed,);
}
}
pub fn percentage (&self) -> Option<f64> {
let window = self.window.load(Relaxed) * 1000.0;
if window > 0.0 {
let used = self.used.load(Relaxed);
Some(100.0 * used / window)
} else {
None
}
}
}