pub struct Backoff {
step: Cell<u32>,
}
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 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.
§Examples
Backing off in a lock-free loop:
use crossbeam_utils::Backoff;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
fn fetch_mul(a: &AtomicUsize, b: usize) -> usize {
let backoff = Backoff::new();
loop {
let val = a.load(SeqCst);
if a.compare_exchange(val, val.wrapping_mul(b), SeqCst, SeqCst).is_ok() {
return val;
}
backoff.spin();
}
}
Waiting for an AtomicBool
to become true
:
use crossbeam_utils::Backoff;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
fn spin_wait(ready: &AtomicBool) {
let backoff = Backoff::new();
while !ready.load(SeqCst) {
backoff.snooze();
}
}
Waiting for an AtomicBool
to become true
and parking the thread after a long wait.
Note that whoever sets the atomic variable to true
must notify the parked thread by calling
unpark()
:
use crossbeam_utils::Backoff;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
use std::thread;
fn blocking_wait(ready: &AtomicBool) {
let backoff = Backoff::new();
while !ready.load(SeqCst) {
if backoff.is_completed() {
thread::park();
} else {
backoff.snooze();
}
}
}
Fields§
§step: Cell<u32>
Implementations§
§impl Backoff
impl Backoff
pub fn reset(&self)
pub fn reset(&self)
Resets the Backoff
.
§Examples
use crossbeam_utils::Backoff;
let backoff = Backoff::new();
backoff.reset();
pub 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.
§Examples
Backing off in a lock-free loop:
use crossbeam_utils::Backoff;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::SeqCst;
fn fetch_mul(a: &AtomicUsize, b: usize) -> usize {
let backoff = Backoff::new();
loop {
let val = a.load(SeqCst);
if a.compare_exchange(val, val.wrapping_mul(b), SeqCst, SeqCst).is_ok() {
return val;
}
backoff.spin();
}
}
let a = AtomicUsize::new(7);
assert_eq!(fetch_mul(&a, 8), 7);
assert_eq!(a.load(SeqCst), 56);
pub 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.
§Examples
Waiting for an AtomicBool
to become true
:
use crossbeam_utils::Backoff;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
use std::thread;
use std::time::Duration;
fn spin_wait(ready: &AtomicBool) {
let backoff = Backoff::new();
while !ready.load(SeqCst) {
backoff.snooze();
}
}
let ready = Arc::new(AtomicBool::new(false));
let ready2 = ready.clone();
thread::spawn(move || {
thread::sleep(Duration::from_millis(100));
ready2.store(true, SeqCst);
});
assert_eq!(ready.load(SeqCst), false);
spin_wait(&ready);
assert_eq!(ready.load(SeqCst), true);
pub fn is_completed(&self) -> bool
pub fn is_completed(&self) -> bool
Returns true
if exponential backoff has completed and blocking the thread is advised.
§Examples
Waiting for an AtomicBool
to become true
and parking the thread after a long wait:
use crossbeam_utils::Backoff;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::SeqCst;
use std::thread;
use std::time::Duration;
fn blocking_wait(ready: &AtomicBool) {
let backoff = Backoff::new();
while !ready.load(SeqCst) {
if backoff.is_completed() {
thread::park();
} else {
backoff.snooze();
}
}
}
let ready = Arc::new(AtomicBool::new(false));
let ready2 = ready.clone();
let waiter = thread::current();
thread::spawn(move || {
thread::sleep(Duration::from_millis(100));
ready2.store(true, SeqCst);
waiter.unpark();
});
assert_eq!(ready.load(SeqCst), false);
blocking_wait(&ready);
assert_eq!(ready.load(SeqCst), true);
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Backoff
impl !RefUnwindSafe for Backoff
impl Send for Backoff
impl !Sync for Backoff
impl Unpin for Backoff
impl UnwindSafe for Backoff
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request