adds initial rcs implementation

This commit is contained in:
2026-01-05 20:26:54 -05:00
parent 252db5993d
commit 98541737a1
23 changed files with 1490 additions and 229 deletions

View File

@@ -13,8 +13,6 @@ use std::time::{Duration, Instant};
#[derive(Clone, Debug)]
pub struct TaskHandle<Message, Data> {
#[allow(dead_code)]
pub name: String,
pub sender: Sender<Message>,
data: Data,
}
@@ -46,7 +44,7 @@ pub trait CyclicTask {
impl<F> CyclicTask for F
where
F: Fn(),
F: FnMut(),
{
type Message = ();
type Data = ();
@@ -58,7 +56,10 @@ where
}
}
pub struct Scheduler<'s, 'e> {
pub struct Scheduler<'s, 'e>
where
'e: 's,
{
scope: &'s Scope<'s, 'e>,
running: Arc<AtomicBool>,
}
@@ -68,7 +69,7 @@ impl<'s> Scheduler<'s, '_> {
where
F: FnOnce(Scheduler<'_, 'env>) -> R,
{
trace!("Scheduler::new(running: {running:?}, f)");
trace!("Scheduler::scope(running: {running:?}, f)");
thread::scope(|scope: &Scope| {
let running_result = running.clone();
// This will automatically set running to false when it drops
@@ -83,16 +84,29 @@ impl<'s> Scheduler<'s, '_> {
})
}
// pub fn inner_scope<'env, F, R>(&self, f: F) -> R
// where
// F: FnOnce(Scheduler<'_, 'env>) -> R,
// 's: 'env,
// {
// trace!("Scheduler::inner_scope(self)");
// thread::scope(|scope: &Scope<'_, 'env>| {
// f(Scheduler {
// scope,
// running: self.running.clone(),
// })
// })
// }
#[allow(dead_code)]
pub fn run<'t, T>(
pub fn run<T>(
&self,
name: impl Into<String>,
task: T,
) -> Result<TaskHandle<T::Message, T::Data>>
where
T: Task + Send + Debug + 't,
T: Task + Send + Debug + 's,
T::Message: Send,
't: 's,
{
let name = name.into();
trace!(
@@ -107,19 +121,18 @@ impl<'s> Scheduler<'s, '_> {
.spawn_scoped(self.scope, move || {
task.run(receiver, running);
})?;
Ok(TaskHandle { name, sender, data })
Ok(TaskHandle { sender, data })
}
pub fn run_cyclic<'t, T>(
pub fn run_cyclic<T>(
&self,
name: impl Into<String>,
mut task: T,
frequency: u64,
) -> Result<TaskHandle<T::Message, T::Data>>
where
T: CyclicTask + Send + 't,
T: CyclicTask + Send + 's,
T::Message: Send,
't: 's,
{
let name = name.into();
trace!(
@@ -144,6 +157,6 @@ impl<'s> Scheduler<'s, '_> {
}
}
})?;
Ok(TaskHandle { name, sender, data })
Ok(TaskHandle { sender, data })
}
}