pub type BackoffSpinlock<T> = Mutex<RawSpinlock<Backoff>, T>;
Expand description
A mutual exclusion (Mutex) type based on busy-waiting with exponential backoff.
Calling lock
(or try_lock
) on this type returns a BackoffSpinlockGuard
, which
automatically frees the lock when it goes out of scope.
§Example
use spinning_top::BackoffSpinlock;
fn main() {
// Wrap some data in a spinlock
let data = String::from("Hello");
let spinlock = BackoffSpinlock::new(data);
make_uppercase(&spinlock); // only pass a shared reference
// We have ownership of the spinlock, so we can extract the data without locking
// Note: this consumes the spinlock
let data = spinlock.into_inner();
assert_eq!(data.as_str(), "HELLO");
}
fn make_uppercase(spinlock: &BackoffSpinlock<String>) {
// Lock the spinlock to get a mutable reference to the data
let mut locked_data = spinlock.lock();
assert_eq!(locked_data.as_str(), "Hello");
locked_data.make_ascii_uppercase();
// the lock is automatically freed at the end of the scope
}
§Usage in statics
BackoffSpinlock::new
is a const
function. This makes the BackoffSpinlock
type
usable in statics:
use spinning_top::BackoffSpinlock;
static DATA: BackoffSpinlock<u32> = BackoffSpinlock::new(0);
fn main() {
let mut data = DATA.lock();
*data += 1;
assert_eq!(*data, 1);
}
Aliased Type§
struct BackoffSpinlock<T> { /* private fields */ }