Struct maitake_sync::util::Backoff
source · 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() -> Self
pub const fn new() -> Self
Returns a new exponential backoff with the maximum exponent set to
Self::DEFAULT_MAX_EXPONENT
.
sourcepub fn with_max_exponent(max: u8) -> Self
pub fn with_max_exponent(max: u8) -> Self
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.