mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-08-07 22:17:07 +02:00
65 lines
2 KiB
Rust
65 lines
2 KiB
Rust
use std::{
|
|
time::Duration,
|
|
sync::{Arc, atomic::{AtomicBool, Ordering::*}},
|
|
thread::{Builder, JoinHandle, sleep},
|
|
};
|
|
#[cfg(feature = "term")] use ::crossterm::event::poll;
|
|
use crate::time::PerfModel;
|
|
|
|
#[derive(Debug)] pub struct Task {
|
|
/// 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 Task {
|
|
/// Spawn a TUI thread that runs `callt least one, then repeats until `exit`.
|
|
pub fn new <F> (exit: Arc<AtomicBool>, mut call: F) -> Result<Self, std::io::Error>
|
|
where F: FnMut(&PerfModel)->() + Send + Sync + 'static
|
|
{
|
|
let perf = Arc::new(PerfModel::default());
|
|
Ok(Self {
|
|
exit: exit.clone(),
|
|
perf: perf.clone(),
|
|
join: Builder::new().name("tengri tui output".into()).spawn(move || {
|
|
while !exit.fetch_and(true, Relaxed) {
|
|
let _ = perf.cycle(&mut call);
|
|
}
|
|
})?.into()
|
|
})
|
|
}
|
|
|
|
/// Spawn a thread that runs `call` least one, then repeats
|
|
/// until `exit`, sleeping for `time` msec after every iteration.
|
|
pub fn new_sleep <F> (
|
|
exit: Arc<AtomicBool>, time: Duration, mut call: F
|
|
) -> Result<Self, std::io::Error>
|
|
where F: FnMut(&PerfModel)->() + Send + Sync + 'static
|
|
{
|
|
Self::new(exit, move |perf| {
|
|
let _ = call(perf);
|
|
sleep(time);
|
|
})
|
|
}
|
|
|
|
/// Spawn a thread that uses [crossterm::event::poll]
|
|
/// to run `call` every `time` msec.
|
|
#[cfg(feature = "term")] pub fn new_poll <F> (
|
|
exit: Arc<AtomicBool>, time: Duration, mut call: F
|
|
) -> Result<Self, std::io::Error>
|
|
where F: FnMut(&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()
|
|
}
|
|
}
|