Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support for synchronous scheduling of physical actions #30

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ impl<T: Sync> TriggerLike for LogicalAction<T> {
self.0.id
}
}

/*#[cfg(test)] //fixme
mod test {
use ActionPresence::{NotPresent, Present};
Expand Down
45 changes: 37 additions & 8 deletions src/scheduler/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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].
///
Expand All @@ -320,7 +322,7 @@ where
/// ctx.schedule(action, After(Duration::from_millis(2))); // equivalent to the previous
/// ```
#[inline]
pub fn schedule<T: Sync>(&mut self, action: &mut LogicalAction<T>, offset: Offset) {
pub fn schedule<T: Sync>(&mut self, action: &mut impl SchedulableAsAction<T>, offset: Offset) {
self.schedule_with_v(action, None, offset)
}

Expand All @@ -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
///
Expand All @@ -350,11 +354,8 @@ where
/// ctx.schedule(action, Asap);
/// ```
#[inline]
pub fn schedule_with_v<T: Sync>(&mut self, action: &mut LogicalAction<T>, value: Option<T>, 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<T: Sync>(&mut self, action: &mut impl SchedulableAsAction<T>, value: Option<T>, offset: Offset) {
action.schedule_with_v(self, value, offset)
}

/// Add new reactions to execute later (at least 1 microstep later).
Expand Down Expand Up @@ -685,6 +686,34 @@ impl AsyncCtx<'_, '_, '_> {
}
}

/// Implemented by LogicalAction and PhysicalAction references
/// to give access to [ReactionCtx::schedule] and variants.
pub trait SchedulableAsAction<T: Sync> {
#[doc(hidden)]
fn schedule_with_v(&mut self, ctx: &mut ReactionCtx, value: Option<T>, offset: Offset);
}

impl<T: Sync> SchedulableAsAction<T> for LogicalAction<T> {
fn schedule_with_v(&mut self, ctx: &mut ReactionCtx, value: Option<T>, 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<T: Sync> SchedulableAsAction<T> for PhysicalActionRef<T> {
fn schedule_with_v(&mut self, ctx: &mut ReactionCtx, value: Option<T>, 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].
Expand Down
7 changes: 7 additions & 0 deletions src/test/stuff_that_must_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ fn actions_get(ctx: &mut ReactionCtx, act_mut: &mut LogicalAction<u32>, act: &Lo
assert!(ctx.get(act).is_some());
}

fn actions_schedule(ctx: &mut ReactionCtx, logical: &mut LogicalAction<u32>, physical: &mut PhysicalActionRef<u32>) {
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<u32>) {
// the duplication is useful here, we're testing that `act` is
// not moved in the first statement, which would make the
Expand Down