Struct cros_async::sync::spin::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>
impl<T> SpinLock<T>
sourcepub fn into_inner(self) -> T
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>
impl<T: ?Sized> SpinLock<T>
sourcepub fn lock(&self) -> SpinLockGuard<'_, T>
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.