asynchronix/time.rs
1//! Simulation time and scheduling.
2//!
3//! This module provides most notably:
4//!
5//! * [`MonotonicTime`]: a monotonic timestamp based on the [TAI] time standard,
6//! * [`Clock`]: a trait for types that can synchronize a simulation,
7//! implemented for instance by [`SystemClock`] and [`AutoSystemClock`],
8//! * [`Scheduler`]: a model-local handle to the global scheduler that can be
9//! used by models to schedule future actions onto themselves.
10//!
11//! [TAI]: https://en.wikipedia.org/wiki/International_Atomic_Time
12//!
13//!
14//! # Examples
15//!
16//! An alarm clock model that prints a message when the simulation time reaches
17//! the specified timestamp.
18//!
19//! ```
20//! use asynchronix::model::Model;
21//! use asynchronix::time::{MonotonicTime, Scheduler};
22//!
23//! // An alarm clock model.
24//! pub struct AlarmClock {
25//! msg: String
26//! }
27//!
28//! impl AlarmClock {
29//! // Creates a new alarm clock.
30//! pub fn new(msg: String) -> Self {
31//! Self { msg }
32//! }
33//!
34//! // Sets an alarm [input port].
35//! pub fn set(&mut self, setting: MonotonicTime, scheduler: &Scheduler<Self>) {
36//! if scheduler.schedule_event(setting, Self::ring, ()).is_err() {
37//! println!("The alarm clock can only be set for a future time");
38//! }
39//! }
40//!
41//! // Rings the alarm [private input port].
42//! fn ring(&mut self) {
43//! println!("{}", self.msg);
44//! }
45//! }
46//!
47//! impl Model for AlarmClock {}
48//! ```
49
50mod clock;
51mod monotonic_time;
52mod scheduler;
53
54pub use clock::{AutoSystemClock, Clock, NoClock, SyncStatus, SystemClock};
55pub(crate) use monotonic_time::TearableAtomicTime;
56pub use monotonic_time::{MonotonicTime, SystemTimeError};
57pub(crate) use scheduler::{
58 schedule_event_at_unchecked, schedule_keyed_event_at_unchecked,
59 schedule_periodic_event_at_unchecked, schedule_periodic_keyed_event_at_unchecked,
60 ScheduledEvent, SchedulerQueue,
61};
62pub use scheduler::{Deadline, EventKey, Scheduler, SchedulingError};