Struct ThreadSyncTimer

Source
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

Source

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).

Source

pub fn set_frame_duration(&mut self, frame_duration_nanos: u32)

Sets ThreadSyncTimer::frame_duration from the provided frame_duration_nanos.

Source

pub fn restart(&mut self) -> Instant

Restarts the synchronization period. Useful e.g. for resuming paused emulation.

Source

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.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<S, T> IntoSample<S> for T
where S: FromSample<T>,

Source§

fn into_sample(self) -> S

Convert to S a sample type from self.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.