sat-rs/src/core/executable.rs

75 lines
2.3 KiB
Rust
Raw Normal View History

2022-05-26 21:10:08 +02:00
use bus::BusReader;
2022-05-26 19:23:31 +02:00
use std::error::Error;
2022-05-26 21:10:08 +02:00
use std::sync::mpsc::TryRecvError;
2022-05-26 19:23:31 +02:00
use std::thread;
use std::thread::JoinHandle;
use std::time::Duration;
2022-05-16 11:24:59 +02:00
pub enum OpResult {
Ok,
2022-05-26 21:10:08 +02:00
TerminationRequested,
2022-05-16 11:24:59 +02:00
}
pub enum ExecutionType {
Infinite,
Cycles(u32),
OneShot,
}
2022-05-26 18:53:07 +02:00
pub trait Executable: Send {
2022-05-16 11:24:59 +02:00
type Error;
2022-05-26 19:19:16 +02:00
fn exec_type(&self) -> ExecutionType;
fn task_name(&self) -> &'static str;
2022-05-26 18:53:07 +02:00
fn periodic_op(&mut self, op_code: i32) -> Result<OpResult, Self::Error>;
2022-05-16 11:24:59 +02:00
}
2022-05-26 19:23:31 +02:00
pub fn executable_scheduler<
T: Executable<Error = E> + Send + 'static + ?Sized,
E: Error + Send + 'static,
>(
mut executable_vec: Vec<Box<T>>,
task_freq: Option<Duration>,
op_code: i32,
2022-05-26 21:10:08 +02:00
mut termination: BusReader<()>,
2022-05-26 19:23:31 +02:00
) -> JoinHandle<Result<OpResult, E>> {
let mut cycle_counts = vec![0; executable_vec.len()];
let mut removal_flags = vec![false; executable_vec.len()];
thread::spawn(move || loop {
2022-05-26 21:10:08 +02:00
match termination.try_recv() {
Ok(_) | Err(TryRecvError::Disconnected) => {
removal_flags.iter_mut().for_each(|x| *x = true);
}
Err(TryRecvError::Empty) => (),
}
2022-05-26 19:23:31 +02:00
for (idx, executable) in executable_vec.iter_mut().enumerate() {
match executable.exec_type() {
ExecutionType::OneShot => {
executable.periodic_op(op_code)?;
removal_flags[idx] = true;
}
ExecutionType::Infinite => {
executable.periodic_op(op_code)?;
}
ExecutionType::Cycles(cycles) => {
executable.periodic_op(op_code)?;
cycle_counts[idx] += 1;
if cycle_counts[idx] == cycles {
removal_flags[idx] = true;
}
}
}
}
let mut removal_iter = removal_flags.iter();
executable_vec.retain(|_| !*removal_iter.next().unwrap());
removal_iter = removal_flags.iter();
cycle_counts.retain(|_| !*removal_iter.next().unwrap());
removal_flags.retain(|&i| !i);
if executable_vec.is_empty() {
return Ok(OpResult::Ok);
}
let freq = task_freq.unwrap_or_else(|| panic!("No task frequency specified"));
thread::sleep(freq);
})
}