Struct spectrusty_core::chip::ThreadSyncTimer
source · Expand description
A tool for synchronizing emulation with a running thread.
Fields§
§time: Instant
The start time of a current synchronization period.
frame_duration: Duration
The desired duration of a single synchronization period.
Implementations§
source§impl ThreadSyncTimer
impl ThreadSyncTimer
sourcepub fn new(frame_duration_nanos: u32) -> Self
pub fn new(frame_duration_nanos: u32) -> Self
Pass the real time duration of a desired synchronization period (usually a duration of a video frame).
sourcepub fn set_frame_duration(&mut self, frame_duration_nanos: u32)
pub fn set_frame_duration(&mut self, frame_duration_nanos: u32)
Sets ThreadSyncTimer::frame_duration from the provided frame_duration_nanos
.
sourcepub fn restart(&mut self) -> Instant
pub fn restart(&mut self) -> Instant
Restarts the synchronization period. Useful e.g. for resuming paused emulation.
sourcepub fn synchronize_thread_to_frame(&mut self) -> Result<(), u32>
pub fn synchronize_thread_to_frame(&mut self) -> Result<(), u32>
Calculates the difference between the desired duration of a synchronization period and the real time that has passed from the start of the current period and levels the difference by calling std::thread::sleep.
This method may be called at the end of each iteration of an emulation loop to synchronize the running thread with a desired iteration period.
Returns Ok
if the thread is in sync with the emulation. In this instance the value
of ThreadSyncTimer::frame_duration is being added to ThreadSyncTimer::time to mark
the beginning of a new period.
Returns Err(missed_periods)
if the elapsed time exceeds the desired period duration.
In this intance the start of a new period is set to Instant::now.
sourcepub fn check_frame_elapsed(&mut self) -> Option<Duration>
pub fn check_frame_elapsed(&mut self) -> Option<Duration>
Returns Some(excess_duration)
if the time elapsed from the beginning of the current
period exceeds or is equal to the desired duration of a synchronization period.
Otherwise returns None
.
The value returned is the excess time that has elapsed above the desired duration.
If the time elapsed equals to the frame_duration
the returned value equals to zero.
In case Some
variant is returned the value of ThreadSyncTimer::frame_duration is
being added to ThreadSyncTimer::time to mark the beginning of a new period.