#[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: AtomicBoolvalue: UnsafeCell<T>

Implementations

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

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.

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.

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

Returns the “default value” for a type. Read more
Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.