pub struct ThreadSyncTimer {
pub time: Instant,
pub frame_duration: Duration,
}
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.
Auto Trait Implementations§
impl Freeze for ThreadSyncTimer
impl RefUnwindSafe for ThreadSyncTimer
impl Send for ThreadSyncTimer
impl Sync for ThreadSyncTimer
impl Unpin for ThreadSyncTimer
impl UnwindSafe for ThreadSyncTimer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<S, T> IntoSample<S> for Twhere
S: FromSample<T>,
impl<S, T> IntoSample<S> for Twhere
S: FromSample<T>,
Source§fn into_sample(self) -> S
fn into_sample(self) -> S
S
a sample type from self
.