Backoff

Struct Backoff 

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

Performs exponential backoff in spin loops.

Backing off in spin loops reduces contention and improves overall performance.

This primitive can execute YIELD and PAUSE instructions, yield the current thread to the OS scheduler, and tell when it is a good time to block the thread using a different synchronization mechanism. Each step of the back off procedure takes roughly twice as long as the previous step.

Implementations§

Source§

impl Backoff

Source

pub fn new() -> Self

Creates a new Backoff instance.

Source

pub fn step(&self) -> u32

Returns the current backoff step (how many times it has been called since the last reset).

Source

pub fn reset(&self)

Resets the backoff state.

Source

pub fn spin(&self)

Backs off in a lock-free loop.

This method should be used when we need to retry an operation because another thread made progress.

The processor may yield using the YIELD or PAUSE instruction.

Source

pub fn spin_or<F>(&self, f: F)
where F: FnOnce(),

It spins or calls the provided function if it should be used.

If the provided function is std::thread::yield_now, then this function has the same behaviour as snooze.

Source

pub fn snooze(&self)

Backs off in a blocking loop.

This method should be used when we need to wait for another thread to make progress.

The processor may yield using the YIELD or PAUSE instruction, and the current thread may yield by giving up a timeslice to the OS scheduler.

In #[no_std] environments, this method is equivalent to spin.

If possible, use is_completed to check when it is advised to stop using backoff and block the current thread using a different synchronization mechanism instead.

Source

pub fn is_completed(&self) -> bool

Returns true if exponential backoff has completed and blocking the thread is advised.

Trait Implementations§

Source§

impl Debug for Backoff

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Backoff

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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.