Struct cros_async::sync::SpinLock

source ·
#[repr(align(128))]
pub struct SpinLock<T: ?Sized> { lock: AtomicBool, value: UnsafeCell<T>, }
Expand description

A primitive that provides safe, mutable access to a shared resource.

Unlike Mutex, a SpinLock will not voluntarily yield its CPU time until the resource is available and will instead keep spinning until the resource is acquired. For the vast majority of cases, Mutex is a better choice than SpinLock. If a SpinLock must be used then users should try to do as little work as possible while holding the SpinLock and avoid any sort of blocking at all costs as it can severely penalize performance.

§Poisoning

This SpinLock does not implement lock poisoning so it is possible for threads to access poisoned data if a thread panics while holding the lock. If lock poisoning is needed, it can be implemented by wrapping the SpinLock in a new type that implements poisoning. See the implementation of std::sync::Mutex for an example of how to do this.

Fields§

§lock: AtomicBool§value: UnsafeCell<T>

Implementations§

source§

impl<T> SpinLock<T>

source

pub fn new(value: T) -> SpinLock<T>

Creates a new, unlocked SpinLock that’s ready for use.

source

pub fn into_inner(self) -> T

Consumes the SpinLock and returns the value guarded by it. This method doesn’t perform any locking as the compiler guarantees that there are no references to self.

source§

impl<T: ?Sized> SpinLock<T>

source

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

Acquires exclusive, mutable access to the resource protected by the SpinLock, blocking the current thread until it is able to do so. Upon returning, the current thread will be the only thread with access to the resource. The SpinLock will be released when the returned SpinLockGuard is dropped. Attempting to call lock while already holding the SpinLock will cause a deadlock.

source

fn unlock(&self)

source

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

Returns a mutable reference to the contained value. This method doesn’t perform any locking as the compiler will statically guarantee that there are no other references to self.

Trait Implementations§

source§

impl<T: Default> Default for SpinLock<T>

source§

fn default() -> Self

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

impl<T> From<T> for SpinLock<T>

source§

fn from(source: T) -> Self

Converts to this type from the input type.
source§

impl<T: ?Sized + Send> Send for SpinLock<T>

source§

impl<T: ?Sized + Send> Sync for SpinLock<T>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for SpinLock<T>

§

impl<T: ?Sized> Unpin for SpinLock<T>
where T: Unpin,

§

impl<T: ?Sized> UnwindSafe for SpinLock<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.