pub struct Backoff { /* private fields */ }
Expand description
An exponential backoff for spin loops.
This is a helper struct for spinning in a busy loop, with an exponentially increasing number of spins up to a maximum value.
Implementations§
Source§impl Backoff
impl Backoff
Sourcepub const DEFAULT_MAX_EXPONENT: u8 = 8u8
pub const DEFAULT_MAX_EXPONENT: u8 = 8u8
The default maximum exponent (2^8).
This is the maximum exponent returned by Backoff::new()
and
Backoff::default()
. To override the maximum exponent, use
Backoff::with_max_exponent()
.
Sourcepub const fn new() -> Backoff
pub const fn new() -> Backoff
Returns a new exponential backoff with the maximum exponent set to
Self::DEFAULT_MAX_EXPONENT
.
Sourcepub fn with_max_exponent(max: u8) -> Backoff
pub fn with_max_exponent(max: u8) -> Backoff
Returns a new exponential backoff with the provided max exponent.
Sourcepub fn spin(&mut self)
pub fn spin(&mut self)
Backs off in a spin loop.
This should be used when an operation needs to be retried because
another thread or core made progress. Depending on the target
architecture, this will generally issue a sequence of yield
or pause
instructions.
Each time this function is called, it will issue 2^exp
[spin loop
hints], where exp
is the current exponent value (starting at 0). If
exp
is less than the configured maximum exponent, the exponent is
incremented once the spin is complete.