1
0
forked from ROMEO/nexosim

Merge pull request #80 from asynchronics/feature/must_use_sync_status

Make the clock's SyncStatus #[must_use]
This commit is contained in:
Serge Barral 2025-01-19 17:26:14 +01:00 committed by GitHub
commit 4531c21f02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 6 deletions

View File

@ -519,13 +519,21 @@ impl Simulation {
loop { loop {
match self.step_to_next(target_time) { match self.step_to_next(target_time) {
// The target time was reached exactly. // 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. // No actions are scheduled before or at the target time.
Ok(None) => { Ok(None) => {
if let Some(target_time) = target_time { if let Some(target_time) = target_time {
// Update the simulation time. // Update the simulation time.
self.time.write(target_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(()); return Ok(());
} }

View File

@ -6,8 +6,7 @@ use std::time::Duration;
use crate::channel::ChannelObserver; use crate::channel::ChannelObserver;
use crate::executor::{Executor, SimulationContext}; use crate::executor::{Executor, SimulationContext};
use crate::model::ProtoModel; use crate::model::ProtoModel;
use crate::time::{AtomicTime, MonotonicTime, TearableAtomicTime}; use crate::time::{AtomicTime, Clock, MonotonicTime, NoClock, SyncStatus, TearableAtomicTime};
use crate::time::{Clock, NoClock};
use crate::util::priority_queue::PriorityQueue; use crate::util::priority_queue::PriorityQueue;
use crate::util::sync_cell::SyncCell; use crate::util::sync_cell::SyncCell;
@ -162,7 +161,13 @@ impl SimInit {
start_time: MonotonicTime, start_time: MonotonicTime,
) -> Result<(Simulation, Scheduler), ExecutionError> { ) -> Result<(Simulation, Scheduler), ExecutionError> {
self.time.write(start_time); 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( let scheduler = Scheduler::new(
self.scheduler_queue.clone(), self.scheduler_queue.clone(),

View File

@ -30,6 +30,7 @@ impl<C: Clock + ?Sized> Clock for Box<C> {
/// The current synchronization status of a clock. /// The current synchronization status of a clock.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[must_use]
pub enum SyncStatus { pub enum SyncStatus {
/// The clock is synchronized. /// The clock is synchronized.
Synchronized, Synchronized,
@ -207,7 +208,7 @@ mod test {
let now = Instant::now(); let now = Instant::now();
let mut clock = SystemClock::from_instant(t0, now); let mut clock = SystemClock::from_instant(t0, now);
let t1 = t0 + Duration::from_millis(200); 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 elapsed = now.elapsed().as_secs_f64();
let dt = t1.duration_since(t0).as_secs_f64(); let dt = t1.duration_since(t0).as_secs_f64();