Threading/Executable module #1

Merged
muellerr merged 12 commits from threading into main 2022-06-09 12:50:06 +02:00
4 changed files with 228 additions and 11 deletions
Showing only changes of commit 40ba758a4e - Show all commits

179
Cargo.lock generated
View File

@ -2,13 +2,158 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "atomic-option"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0db678acb667b525ac40a324fc5f7d3390e29239b31c7327bb8157f5b4fff593"
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bus"
version = "2.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1e66e1779f5b1440f1a58220ba3b3ded4427175f0a9fb8d7066521f8b4e8f2b"
dependencies = [
"atomic-option",
"crossbeam-channel 0.4.4",
"num_cpus",
"parking_lot_core",
]
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
dependencies = [
"bitflags",
]
[[package]]
name = "crossbeam-channel"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b153fe7cbef478c567df0f972e02e6d736db11affe43dfc9c56a9374d1adfb87"
dependencies = [
"crossbeam-utils 0.7.2",
"maybe-uninit",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aaa7bd5fb665c6864b5f963dd9097905c54125909c7aa94c9e18507cdbe6c53"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils 0.8.8",
]
[[package]]
name = "crossbeam-utils"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
dependencies = [
"autocfg",
"cfg-if 0.1.10",
"lazy_static",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf124c720b7686e3c2663cf54062ab0f68a88af2fb6a030e87e30bf721fcb38"
dependencies = [
"cfg-if 1.0.0",
"lazy_static",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "launchpad"
version = "0.1.0"
dependencies = [
"bus",
"crossbeam-channel 0.5.4",
"thiserror",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "maybe-uninit"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
[[package]]
name = "num_cpus"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "parking_lot_core"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3"
dependencies = [
"cfg-if 0.1.10",
"cloudabi",
"libc",
"redox_syscall",
"smallvec",
"winapi",
]
[[package]]
name = "proc-macro2"
version = "1.0.38"
@ -27,6 +172,18 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "redox_syscall"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "smallvec"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]]
name = "syn"
version = "1.0.94"
@ -63,3 +220,25 @@ name = "unicode-xid"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -7,3 +7,5 @@ edition = "2021"
[dependencies]
thiserror = "1.0"
bus = "2.2.3"
crossbeam-channel = "0.5.4"

View File

@ -1,10 +1,13 @@
use bus::BusReader;
use std::error::Error;
use std::sync::mpsc::TryRecvError;
use std::thread;
use std::thread::JoinHandle;
use std::time::Duration;
pub enum OpResult {
Ok,
TerminationRequested,
}
pub enum ExecutionType {
@ -28,10 +31,17 @@ pub fn executable_scheduler<
mut executable_vec: Vec<Box<T>>,
task_freq: Option<Duration>,
op_code: i32,
mut termination: BusReader<()>,
) -> 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 {
match termination.try_recv() {
Ok(_) | Err(TryRecvError::Disconnected) => {
removal_flags.iter_mut().for_each(|x| *x = true);
}
Err(TryRecvError::Empty) => (),
}
for (idx, executable) in executable_vec.iter_mut().enumerate() {
match executable.exec_type() {
ExecutionType::OneShot => {

View File

@ -1,6 +1,9 @@
use bus::{Bus, BusReader};
use crossbeam_channel::{unbounded, Receiver, Sender};
use launchpad::core::executable::{executable_scheduler, Executable, ExecutionType, OpResult};
use std::error::Error;
use std::fmt;
use std::mem::transmute;
use std::thread;
use std::time::Duration;
@ -99,14 +102,24 @@ impl Executable for PeriodicTask {
}
}
fn test0() {
fn test0(term_bus: &mut Bus<()>) {
let exec_task = OneShotTask {};
let task_vec = vec![Box::new(exec_task)];
let jhandle = executable_scheduler(task_vec, Some(Duration::from_millis(100)), 0);
let jhandle = executable_scheduler(
task_vec,
Some(Duration::from_millis(100)),
0,
term_bus.add_rx(),
);
let exec_task2 = FixedCyclesTask {};
let task_vec2: Vec<Box<dyn Executable<Error = ExampleError> + Send>> =
vec![Box::new(exec_task2)];
let jhandle2 = executable_scheduler(task_vec2, Some(Duration::from_millis(100)), 1);
let jhandle2 = executable_scheduler(
task_vec2,
Some(Duration::from_millis(100)),
1,
term_bus.add_rx(),
);
jhandle
.join()
@ -118,19 +131,32 @@ fn test0() {
.expect("Task 2 failed");
}
fn test1() {
fn test1(term_bus: &mut Bus<()>) {
let one_shot_in_vec = OneShotTask {};
let cycles_in_vec = FixedCyclesTask {};
let test_vec: Vec<Box<dyn Executable<Error = ExampleError>>> =
vec![Box::new(one_shot_in_vec), Box::new(cycles_in_vec)];
let jhandle3 = executable_scheduler(test_vec, Some(Duration::from_millis(100)), 3);
let periodic_in_vec = PeriodicTask {};
let test_vec: Vec<Box<dyn Executable<Error = ExampleError>>> = vec![
Box::new(one_shot_in_vec),
Box::new(cycles_in_vec),
Box::new(periodic_in_vec),
];
let jhandle3 = executable_scheduler(
test_vec,
Some(Duration::from_millis(100)),
3,
term_bus.add_rx(),
);
thread::sleep(Duration::from_millis(5000));
println!("Broadcasting cancel");
term_bus.broadcast(());
jhandle3
.join()
.expect("Joining thread 3 failed")
.expect("Task 3 failed");
}
fn main() {
test0();
thread::sleep(Duration::from_millis(1000));
test1();
let mut tx = Bus::new(5);
test0(&mut tx);
test1(&mut tx);
}