AtomicInterval

Struct AtomicInterval 

Source
pub struct AtomicInterval { /* private fields */ }
Expand description

It implements a timer. It allows checking a periodic interval.

This structure is meant to be shared across multiple threads and does not require additional sync wrappers. Generally, it can be used with Arc<AtomicInterval>.

If you want performance maximization (when you do not need memory ordering synchronization), AtomicIntervalLight is a relaxed variant of this class.

Implementations§

Source§

impl AtomicInterval

Source

pub fn new(period: Duration) -> Self

Creates a new AtomicInterval with a fixed period interval.

The first tick is not instantaneous at the creation of the interval. It means period amount of time has to elapsed for the first tick.

Source

pub fn period(&self) -> Duration

The period set for this interval.

Source

pub fn set_period(&mut self, period: Duration)

Changes the period of this interval.

Source

pub fn is_ticked(&self, success: Ordering, failure: Ordering) -> bool

Checks whether the interval’s tick expired.

When it returns true then at least period amount of time has passed since the last tick.

When a period is passed (i.e., this function return true) the internal timer is automatically reset for the next tick.

It takes two Ordering arguments to describe the memory ordering. success describes the required ordering when the period elapsed and the timer has to be reset (read-modify-write operation). failures describes the required ordering when the period is not passed yet.

Using Ordering::Acquire as success ordering makes the store part of this operation Ordering::Relaxed, and using Ordering::Release makes the successful load Ordering::Relaxed. The failure ordering can only be Ordering::SeqCst, Ordering::Acquire or Ordering::Relaxed and must be equivalent to or weaker than the success ordering.

It can be used in a concurrency context: only one thread can tick the timer per period.

§Example
use atomic_interval::AtomicInterval;
use std::sync::atomic::Ordering;
use std::time::Duration;
use std::time::Instant;

let atomic_interval = AtomicInterval::new(Duration::from_secs(1));
let time_start = Instant::now();

let elapsed = loop {
   if atomic_interval.is_ticked(Ordering::Relaxed, Ordering::Relaxed) {
       break time_start.elapsed();
   }
};

println!("Elapsed: {:?}", elapsed);
// Elapsed: 999.842446ms

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