From 1cb36535d3da98712e6eca885001127ec7b98250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Thu, 15 Sep 2022 16:01:40 +0200 Subject: [PATCH 1/2] Support synchronous schedule for physical actions --- src/actions.rs | 2 +- src/scheduler/context.rs | 45 ++++++++++++++++++++++++----- src/test/stuff_that_must_compile.rs | 7 +++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/actions.rs b/src/actions.rs index fe18d1ae..8352fa9b 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -22,6 +22,7 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + use std::cmp::Reverse; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; @@ -155,7 +156,6 @@ impl TriggerLike for LogicalAction { self.0.id } } - /*#[cfg(test)] //fixme mod test { use ActionPresence::{NotPresent, Present}; diff --git a/src/scheduler/context.rs b/src/scheduler/context.rs index 53e06027..c8e007d5 100644 --- a/src/scheduler/context.rs +++ b/src/scheduler/context.rs @@ -304,7 +304,9 @@ where /// Schedule an action to trigger at some point in the future. /// The action will trigger after its own implicit time delay, - /// plus an optional additional time delay (see [Offset]). + /// plus an optional additional time delay (see [Offset]). This + /// delay is added to the current logical (resp. physical) time + /// for logical (resp. physical) actions. /// /// This is like [Self::schedule_with_v], where the value is [None]. /// @@ -320,7 +322,7 @@ where /// ctx.schedule(action, After(Duration::from_millis(2))); // equivalent to the previous /// ``` #[inline] - pub fn schedule(&mut self, action: &mut LogicalAction, offset: Offset) { + pub fn schedule(&mut self, action: &mut impl SchedulableAsAction, offset: Offset) { self.schedule_with_v(action, None, offset) } @@ -334,7 +336,9 @@ where /// (see [Self::is_present]). /// /// The action will trigger after its own implicit time delay, - /// plus an optional additional time delay (see [Offset]). + /// plus an optional additional time delay (see [Offset]). This + /// delay is added to the current logical (resp. physical) time + /// for logical (resp. physical) actions. /// /// ### Examples /// @@ -350,11 +354,8 @@ where /// ctx.schedule(action, Asap); /// ``` #[inline] - pub fn schedule_with_v(&mut self, action: &mut LogicalAction, value: Option, offset: Offset) { - let eta = self.make_successor_tag(action.0.min_delay + offset.to_duration()); - action.0.schedule_future_value(eta, value); - let downstream = self.dataflow.reactions_triggered_by(&action.get_id()); - self.enqueue_later(downstream, eta); + pub fn schedule_with_v(&mut self, action: &mut impl SchedulableAsAction, value: Option, offset: Offset) { + action.schedule_with_v(self, value, offset) } /// Add new reactions to execute later (at least 1 microstep later). @@ -685,6 +686,34 @@ impl AsyncCtx<'_, '_, '_> { } } +/// Implemented by LogicalAction and PhysicalAction references +/// to give access to [ReactionCtx::schedule] and variants. +pub trait SchedulableAsAction { + #[doc(hidden)] + fn schedule_with_v(&mut self, ctx: &mut ReactionCtx, value: Option, offset: Offset); +} + +impl SchedulableAsAction for LogicalAction { + fn schedule_with_v(&mut self, ctx: &mut ReactionCtx, value: Option, offset: Offset) { + let eta = ctx.make_successor_tag(self.0.min_delay + offset.to_duration()); + self.0.schedule_future_value(eta, value); + let downstream = ctx.dataflow.reactions_triggered_by(&self.get_id()); + ctx.enqueue_later(downstream, eta); + } +} + +impl SchedulableAsAction for PhysicalActionRef { + fn schedule_with_v(&mut self, ctx: &mut ReactionCtx, value: Option, offset: Offset) { + self.use_mut_p(value, |action, value| { + let tag = EventTag::absolute(ctx.initial_time, Instant::now() + offset.to_duration()); + action.0.schedule_future_value(tag, value); + let downstream = ctx.dataflow.reactions_triggered_by(&action.get_id()); + ctx.enqueue_later(downstream, tag); + }) + .ok(); + } +} + /// An offset from the current event. /// /// This is to be used with [ReactionCtx::schedule]. diff --git a/src/test/stuff_that_must_compile.rs b/src/test/stuff_that_must_compile.rs index ccc408f0..090029fa 100644 --- a/src/test/stuff_that_must_compile.rs +++ b/src/test/stuff_that_must_compile.rs @@ -41,6 +41,13 @@ fn actions_get(ctx: &mut ReactionCtx, act_mut: &mut LogicalAction, act: &Lo assert!(ctx.get(act).is_some()); } +fn actions_schedule(ctx: &mut ReactionCtx, logical: &mut LogicalAction, physical: &mut PhysicalActionRef) { + ctx.schedule_with_v(physical, Some(2), Asap); + ctx.schedule(physical, Asap); + ctx.schedule_with_v(logical, Some(2), Asap); + ctx.schedule(logical, Asap); +} + fn actions_use_ref_mut(ctx: &mut ReactionCtx, act: &mut LogicalAction) { // the duplication is useful here, we're testing that `act` is // not moved in the first statement, which would make the From a7f47ce4ced7c6a214254e100c0392cb82d23f9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 16 Sep 2022 10:29:01 +0200 Subject: [PATCH 2/2] Format --- src/actions.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/actions.rs b/src/actions.rs index 8352fa9b..25c3d975 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -22,7 +22,6 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - use std::cmp::Reverse; use std::marker::PhantomData; use std::ops::{Deref, DerefMut};