From a71231f39808bfe511f9322341cbfefe9296ec61 Mon Sep 17 00:00:00 2001 From: Serge Barral Date: Sun, 19 Jan 2025 17:23:38 +0100 Subject: [PATCH] Make the clock's SyncStatus #[must_use] --- nexosim/src/simulation.rs | 12 ++++++++++-- nexosim/src/simulation/sim_init.rs | 11 ++++++++--- nexosim/src/time/clock.rs | 3 ++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/nexosim/src/simulation.rs b/nexosim/src/simulation.rs index 55ea54d..4bc52d2 100644 --- a/nexosim/src/simulation.rs +++ b/nexosim/src/simulation.rs @@ -519,13 +519,21 @@ impl Simulation { loop { match self.step_to_next(target_time) { // The target time was reached exactly. - Ok(reached_time) if reached_time == target_time => return Ok(()), + Ok(time) if time == target_time => return Ok(()), // No actions are scheduled before or at the target time. Ok(None) => { if let Some(target_time) = target_time { // Update the simulation time. self.time.write(target_time); - self.clock.synchronize(target_time); + if let SyncStatus::OutOfSync(lag) = self.clock.synchronize(target_time) { + if let Some(tolerance) = &self.clock_tolerance { + if &lag > tolerance { + self.is_terminated = true; + + return Err(ExecutionError::OutOfSync(lag)); + } + } + } } return Ok(()); } diff --git a/nexosim/src/simulation/sim_init.rs b/nexosim/src/simulation/sim_init.rs index 9969dd4..7200320 100644 --- a/nexosim/src/simulation/sim_init.rs +++ b/nexosim/src/simulation/sim_init.rs @@ -6,8 +6,7 @@ use std::time::Duration; use crate::channel::ChannelObserver; use crate::executor::{Executor, SimulationContext}; use crate::model::ProtoModel; -use crate::time::{AtomicTime, MonotonicTime, TearableAtomicTime}; -use crate::time::{Clock, NoClock}; +use crate::time::{AtomicTime, Clock, MonotonicTime, NoClock, SyncStatus, TearableAtomicTime}; use crate::util::priority_queue::PriorityQueue; use crate::util::sync_cell::SyncCell; @@ -162,7 +161,13 @@ impl SimInit { start_time: MonotonicTime, ) -> Result<(Simulation, Scheduler), ExecutionError> { self.time.write(start_time); - self.clock.synchronize(start_time); + if let SyncStatus::OutOfSync(lag) = self.clock.synchronize(start_time) { + if let Some(tolerance) = &self.clock_tolerance { + if &lag > tolerance { + return Err(ExecutionError::OutOfSync(lag)); + } + } + } let scheduler = Scheduler::new( self.scheduler_queue.clone(), diff --git a/nexosim/src/time/clock.rs b/nexosim/src/time/clock.rs index 789558d..a93ac93 100644 --- a/nexosim/src/time/clock.rs +++ b/nexosim/src/time/clock.rs @@ -30,6 +30,7 @@ impl Clock for Box { /// The current synchronization status of a clock. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +#[must_use] pub enum SyncStatus { /// The clock is synchronized. Synchronized, @@ -207,7 +208,7 @@ mod test { let now = Instant::now(); let mut clock = SystemClock::from_instant(t0, now); let t1 = t0 + Duration::from_millis(200); - clock.synchronize(t1); + assert_eq!(clock.synchronize(t1), SyncStatus::Synchronized); let elapsed = now.elapsed().as_secs_f64(); let dt = t1.duration_since(t0).as_secs_f64();