From 35aa7c450ce274fef0974e30986b7289b69faa8c Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 26 May 2022 19:19:16 +0200 Subject: [PATCH] some bugfixes --- src/core/executable.rs | 10 ++-------- src/main.rs | 36 +++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/core/executable.rs b/src/core/executable.rs index 4750fe4..a94ca16 100644 --- a/src/core/executable.rs +++ b/src/core/executable.rs @@ -10,14 +10,8 @@ pub enum ExecutionType { pub trait Executable: Send { type Error; - //const EXEC_TYPE: ExecutionType; - //const TASK_NAME: &'static str; - fn exec_type(&self) -> ExecutionType;// { - // return Self::EXEC_TYPE; - //} - fn task_name(&self) -> &'static str;//{ - // return Self::TASK_NAME; - //} + fn exec_type(&self) -> ExecutionType; + fn task_name(&self) -> &'static str; fn periodic_op(&mut self, op_code: i32) -> Result; } diff --git a/src/main.rs b/src/main.rs index 8be3696..06b9f61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +60,7 @@ impl Executable for FixedCyclesTask { type Error = ExampleError; fn exec_type(&self) -> ExecutionType { - ExecutionType::Cycles(5) + ExecutionType::Cycles(3) } fn task_name(&self) -> &'static str { @@ -103,10 +103,11 @@ impl Executable for PeriodicTask { fn test0() { let exec_task = OneShotTask {}; let task_vec = vec![Box::new(exec_task)]; - let jhandle = test_thread(task_vec, Some(Duration::from_millis(500)), 0); + let jhandle = executable_scheduler(task_vec, Some(Duration::from_millis(100)), 0); let exec_task2 = FixedCyclesTask {}; - let task_vec2 = vec![Box::new(exec_task2)]; - let jhandle2 = test_thread(task_vec2, Some(Duration::from_millis(1000)), 1); + let task_vec2: Vec + Send>> = + vec![Box::new(exec_task2)]; + let jhandle2 = executable_scheduler(task_vec2, Some(Duration::from_millis(100)), 1); jhandle .join() @@ -117,21 +118,28 @@ fn test0() { .expect("Joining thread 2 failed") .expect("Task 2 failed"); } -fn main() { - - thread::sleep(Duration::from_millis(1000)); +fn test1() { let one_shot_in_vec = OneShotTask {}; let cycles_in_vec = FixedCyclesTask {}; - let test_vec: Vec>> = vec![Box::new(one_shot_in_vec), Box::new(cycles_in_vec)]; - let jhandle3 = test_thread(test_vec, Some(Duration::from_millis(500)), 3); + let test_vec: Vec>> = + vec![Box::new(one_shot_in_vec), Box::new(cycles_in_vec)]; + let jhandle3 = executable_scheduler(test_vec, Some(Duration::from_millis(100)), 3); jhandle3 .join() .expect("Joining thread 3 failed") .expect("Task 3 failed"); } +fn main() { + test0(); + thread::sleep(Duration::from_millis(1000)); + test1(); +} -fn test_thread + Send + 'static + ?Sized, E: Error + Send + 'static>( +fn executable_scheduler< + T: Executable + Send + 'static + ?Sized, + E: Error + Send + 'static, +>( mut executable_vec: Vec>, task_freq: Option, op_code: i32, @@ -157,13 +165,15 @@ fn test_thread + Send + 'static + ?Sized, E: Error + Se } } } - executable_vec.retain(|_| !*removal_flags.iter().next().unwrap()); + 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")); + let freq = task_freq.unwrap_or_else(|| panic!("No task frequency specified")); thread::sleep(freq); }) }