Type Alias spinning_top::BackoffSpinlock

source ·
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 */ }

Implementations

source§

impl<R, T> Mutex<R, T>
where R: RawMutex,

source

pub const fn new(val: T) -> Mutex<R, T>

Creates a new mutex in an unlocked state ready for use.

source

pub fn into_inner(self) -> T

Consumes this mutex, returning the underlying data.

source§

impl<R, T> Mutex<R, T>

source

pub const fn from_raw(raw_mutex: R, val: T) -> Mutex<R, T>

Creates a new mutex based on a pre-existing raw mutex.

source

pub const fn const_new(raw_mutex: R, val: T) -> Mutex<R, T>

Creates a new mutex based on a pre-existing raw mutex.

This allows creating a mutex in a constant context on stable Rust.

This method is a legacy alias for from_raw.

source§

impl<R, T> Mutex<R, T>
where R: RawMutex, T: ?Sized,

source

pub unsafe fn make_guard_unchecked(&self) -> MutexGuard<'_, R, T>

Creates a new MutexGuard without checking if the mutex is locked.

§Safety

This method must only be called if the thread logically holds the lock.

Calling this function when a guard has already been produced is undefined behaviour unless the guard was forgotten with mem::forget.

source

pub fn lock(&self) -> MutexGuard<'_, R, T>

Acquires a mutex, blocking the current thread until it is able to do so.

This function will block the local thread until it is available to acquire the mutex. Upon returning, the thread is the only thread with the mutex held. An RAII guard is returned to allow scoped unlock of the lock. When the guard goes out of scope, the mutex will be unlocked.

Attempts to lock a mutex in the thread which already holds the lock will result in a deadlock.

source

pub fn try_lock(&self) -> Option<MutexGuard<'_, R, T>>

Attempts to acquire this lock.

If the lock could not be acquired at this time, then None is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

This function does not block.

source

pub fn get_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying data.

Since this call borrows the Mutex mutably, no actual locking needs to take place—the mutable borrow statically guarantees no locks exist.

source

pub fn is_locked(&self) -> bool

Checks whether the mutex is currently locked.

source

pub unsafe fn force_unlock(&self)

Forcibly unlocks the mutex.

This is useful when combined with mem::forget to hold a lock without the need to maintain a MutexGuard object alive, for example when dealing with FFI.

§Safety

This method must only be called if the current thread logically owns a MutexGuard but that guard has been discarded using mem::forget. Behavior is undefined if a mutex is unlocked when not locked.

source

pub unsafe fn raw(&self) -> &R

Returns the underlying raw mutex object.

Note that you will most likely need to import the RawMutex trait from lock_api to be able to call functions on the raw mutex.

§Safety

This method is unsafe because it allows unlocking a mutex while still holding a reference to a MutexGuard.

source

pub fn data_ptr(&self) -> *mut T

Returns a raw pointer to the underlying data.

This is useful when combined with mem::forget to hold a lock without the need to maintain a MutexGuard object alive, for example when dealing with FFI.

§Safety

You must ensure that there are no data races when dereferencing the returned pointer, for example if the current thread logically owns a MutexGuard but that guard has been discarded using mem::forget.

source§

impl<R, T> Mutex<R, T>
where R: RawMutexFair, T: ?Sized,

source

pub unsafe fn force_unlock_fair(&self)

Forcibly unlocks the mutex using a fair unlock protocol.

This is useful when combined with mem::forget to hold a lock without the need to maintain a MutexGuard object alive, for example when dealing with FFI.

§Safety

This method must only be called if the current thread logically owns a MutexGuard but that guard has been discarded using mem::forget. Behavior is undefined if a mutex is unlocked when not locked.

source§

impl<R, T> Mutex<R, T>
where R: RawMutexTimed, T: ?Sized,

source

pub fn try_lock_for( &self, timeout: <R as RawMutexTimed>::Duration, ) -> Option<MutexGuard<'_, R, T>>

Attempts to acquire this lock until a timeout is reached.

If the lock could not be acquired before the timeout expired, then None is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

source

pub fn try_lock_until( &self, timeout: <R as RawMutexTimed>::Instant, ) -> Option<MutexGuard<'_, R, T>>

Attempts to acquire this lock until a timeout is reached.

If the lock could not be acquired before the timeout expired, then None is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped.

Trait Implementations

source§

impl<R, T> Debug for Mutex<R, T>
where R: RawMutex, T: Debug + ?Sized,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<R, T> Default for Mutex<R, T>
where R: RawMutex, T: Default + ?Sized,

source§

fn default() -> Mutex<R, T>

Returns the “default value” for a type. Read more
source§

impl<R, T> From<T> for Mutex<R, T>
where R: RawMutex,

source§

fn from(t: T) -> Mutex<R, T>

Converts to this type from the input type.
source§

impl<R, T> Send for Mutex<R, T>
where R: RawMutex + Send, T: Send + ?Sized,

source§

impl<R, T> Sync for Mutex<R, T>
where R: RawMutex + Sync, T: Send + ?Sized,