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
impl Backoff
Sourcepub fn step(&self) -> u32
pub fn step(&self) -> u32
Returns the current backoff step
(how many times it has been called since the last reset).
Sourcepub fn spin(&self)
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.
Sourcepub fn spin_or<F>(&self, f: F)where
F: FnOnce(),
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.
Sourcepub fn snooze(&self)
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.
Sourcepub fn is_completed(&self) -> bool
pub fn is_completed(&self) -> bool
Returns true if exponential backoff has completed and blocking the thread is advised.