diff --git a/asynchronix/src/simulation.rs b/asynchronix/src/simulation.rs index 1bcd281..53e3732 100644 --- a/asynchronix/src/simulation.rs +++ b/asynchronix/src/simulation.rs @@ -132,7 +132,7 @@ pub(crate) use scheduler::{ schedule_periodic_event_at_unchecked, schedule_periodic_keyed_event_at_unchecked, KeyedOnceAction, KeyedPeriodicAction, OnceAction, PeriodicAction, SchedulerQueue, }; -pub use scheduler::{Action, ActionKey, Deadline, SchedulingError}; +pub use scheduler::{Action, ActionKey, AutoActionKey, Deadline, SchedulingError}; pub use sim_init::SimInit; use std::error::Error; diff --git a/asynchronix/src/simulation/scheduler.rs b/asynchronix/src/simulation/scheduler.rs index 79a4682..f325912 100644 --- a/asynchronix/src/simulation/scheduler.rs +++ b/asynchronix/src/simulation/scheduler.rs @@ -54,6 +54,22 @@ impl Deadline for MonotonicTime { } } +/// Managed handle to a scheduled action. +/// +/// An `AutoActionKey` is a managed handle to a scheduled action that cancels +/// action on drop. +#[derive(Debug)] +#[must_use = "managed action key shall be used"] +pub struct AutoActionKey { + is_cancelled: Arc, +} + +impl Drop for AutoActionKey { + fn drop(&mut self) { + self.is_cancelled.store(true, Ordering::Relaxed); + } +} + /// Handle to a scheduled action. /// /// An `ActionKey` can be used to cancel a scheduled action. @@ -80,6 +96,13 @@ impl ActionKey { pub fn cancel(self) { self.is_cancelled.store(true, Ordering::Relaxed); } + + /// Converts action key to a managed key. + pub fn into_auto(self) -> AutoActionKey { + AutoActionKey { + is_cancelled: self.is_cancelled, + } + } } impl PartialEq for ActionKey {