1
0
forked from ROMEO/nexosim

Make the clock's SyncStatus #[must_use]

This commit is contained in:
Serge Barral 2025-01-19 17:23:38 +01:00
parent c243410783
commit a71231f398
3 changed files with 20 additions and 6 deletions

View File

@ -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(());
}

View File

@ -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(),

View File

@ -30,6 +30,7 @@ impl<C: Clock + ?Sized> Clock for Box<C> {
/// 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();