Expand description
This library provides type-safe and fully-featured Mutex, RwLock,
and Condvar types which wrap simple raw synchronization primitives. This
has several
benefits: not only does it eliminate a large portion of the work in
implementing custom lock types, it also allows users to write code which is
generic with regard to different lock implementations.
Basic usage of this crate is very straightforward:
- Create a raw lock type. This should only contain the lock state, not any data protected by the lock.
- Implement the
RawMutextrait for your custom lock type. - Export your mutex as a type alias for
lock_api::Mutex, and your mutex guard as a type alias forlock_api::MutexGuard. See the example below for details.
This process is similar for RwLocks, except that two guards need to be
exported instead of one (or three guards if your type supports upgradable
read locks; see extension traits below for details).
A Condvar additionally requires a RawCondvar implementation tied to
the raw mutex type with which it can be used.
§Example
use lock_api::{RawMutex, Mutex, GuardSend};
use std::sync::atomic::{AtomicBool, Ordering};
// 1. Define our raw lock type
pub struct RawSpinlock(AtomicBool);
// 2. Implement RawMutex for this type
unsafe impl RawMutex for RawSpinlock {
const INIT: RawSpinlock = RawSpinlock(AtomicBool::new(false));
// A spinlock guard can be sent to another thread and unlocked there
type Guard = GuardSend;
fn lock(&self) -> Self::Guard {
// Note: This isn't the best way of implementing a spinlock, but it
// suffices for the sake of this example.
loop {
if let Some(guard) = self.try_lock() {
return guard;
}
}
}
fn try_lock(&self) -> Option<Self::Guard> {
self.0
.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
.ok().map(|_| GuardSend::new())
}
unsafe fn unlock(&self, _guard: Self::Guard) {
self.0.store(false, Ordering::Release);
}
fn is_locked(&self) -> bool {
self.0.load(Ordering::Relaxed)
}
}
// 3. Export the wrappers. These are the types that users will actually use.
pub type Spinlock<T> = lock_api::Mutex<RawSpinlock, T>;
pub type SpinlockGuard<'a, T> = lock_api::MutexGuard<'a, RawSpinlock, T>;§Extension traits
In addition to basic locking & unlocking functionality, you have the option of exposing additional functionality in your lock types by implementing additional traits for it. Examples of extension features include:
- Fair unlocking (
RawMutexFair,RawRwLockFair) - Lock timeouts (
RawMutexTimed,RawRwLockTimed) - Condition-variable timeouts (
RawCondvarTimed) - Downgradable write locks (
RawRwLockDowngrade) - Upgradable read locks (
RawRwLockUpgrade)
The Mutex and RwLock wrappers will automatically expose this additional
functionality if the raw lock type implements these extension traits.
§Cargo features
This crate provides the following Cargo features:
atomic_usize: Enables reentrant mutex types, which require pointer-sized atomic operations. This feature is enabled by default.arc_lock: Enables locking from anArcand types such asArcMutexGuard. This requires thealloccrate.owning_ref: Allows lock guards to be used with theowning_refcrate.serde: Enables serialization and deserialization of lock types.
Structs§
- ArcMutex
Guard arc_lock - An RAII mutex guard returned by the
Arclocking operations onMutex. - ArcReentrant
Mutex Guard arc_lockandatomic_usize - An RAII mutex guard returned by the
Arclocking operations onReentrantMutex. - ArcRw
Lock Read Guard arc_lock - An RAII read guard returned by the
Arclocking operations onRwLock. - ArcRw
Lock Upgradable Read Guard arc_lock - An RAII upgradable read guard returned by the
Arclocking operations onRwLock. UnlikeRwLockUpgradableReadGuard, this guard owns theArcused to unlock the reader-writer lock instead of borrowing it, so its lifetime is not tied to a lock reference. - ArcRw
Lock Write Guard arc_lock - An RAII write guard returned by the
Arclocking operations onRwLock. UnlikeRwLockWriteGuard, this guard owns theArcused to unlock the reader-writer lock instead of borrowing it, so its lifetime is not tied to a lock reference. - Condvar
- A condition variable.
- Guard
NoSend - A stateless guard which is not
Send. - Guard
Send - A stateless guard which is
Send. - Mapped
Mutex Guard - An RAII mutex guard returned by
MutexGuard::map, which can point to a subfield of the protected data. - Mapped
Reentrant Mutex Guard atomic_usize - An RAII mutex guard returned by
ReentrantMutexGuard::map, which can point to a subfield of the protected data. - Mapped
RwLock Read Guard - An RAII read lock guard returned by
RwLockReadGuard::map, which can point to a subfield of the protected data. - Mapped
RwLock Write Guard - An RAII write lock guard returned by
RwLockWriteGuard::map, which can point to a subfield of the protected data. - Mutex
- A mutual exclusion primitive useful for protecting shared data.
- Mutex
Guard - An RAII guard which unlocks the mutex when dropped.
- RawReentrant
Mutex atomic_usize - A raw mutex type that wraps another raw mutex to provide reentrancy.
- Reentrant
Mutex atomic_usize - A reentrant mutual exclusion lock.
- Reentrant
Mutex Guard atomic_usize - An RAII guard which releases one level of recursive locking when dropped.
- RwLock
- A reader-writer lock.
- RwLock
Read Guard - An RAII guard which releases shared read access when dropped.
- RwLock
Upgradable Read Guard - An RAII guard which releases upgradable read access when dropped.
- RwLock
Write Guard - An RAII guard which releases exclusive write access when dropped.
- Wait
Timeout Result - A type indicating whether a timed wait on a condition variable timed out.
Traits§
- GetThread
Id atomic_usize - Helper trait which returns a non-zero thread ID.
- RawCondvar
- Basic operations for a condition variable associated with a particular
RawMuteximplementation. - RawCondvar
Timed - Additional methods for condition variables which support timeouts.
- RawMutex
- Basic operations for a mutex.
- RawMutex
Fair - Additional methods for mutexes which support fair unlocking.
- RawMutex
Timed - Additional methods for mutexes which support locking with timeouts.
- RawRw
Lock - Basic operations for a reader-writer lock.
- RawRw
Lock Downgrade - Additional methods for
RwLocks which support atomically downgrading an exclusive lock to a shared lock. - RawRw
Lock Fair - Additional methods for
RwLocks which support fair unlocking. - RawRw
Lock Timed - Additional methods for
RwLocks which support locking with timeouts. - RawRw
Lock Upgrade - Additional methods for
RwLocks which support atomically upgrading a shared lock to an exclusive lock. - RawRw
Lock Upgrade Downgrade - Additional methods for
RwLocks which support upgradable locks and lock downgrading. - RawRw
Lock Upgrade Fair - Additional methods for
RwLocks which support upgradable locks and fair unlocking. - RawRw
Lock Upgrade Timed - Additional methods for
RwLocks which support upgradable locks and locking with timeouts.