Expand description
This crate provides spin-based versions of the
primitives in std::sync and std::lazy. Because synchronization is done through spinning,
the primitives are suitable for use in no_std environments.
§Features
-
Mutex,RwLock,Once/SyncOnceCell, andSyncLazyequivalents -
Support for
no_stdenvironments -
lock_apicompatibility -
Upgradeable
RwLockguards -
Guards can be sent and shared between threads
-
Guard leaking
-
Ticket locks
-
Different strategies for dealing with contention
§Relationship with std::sync
While spin is not a drop-in replacement for std::sync (and
should not be considered as such)
an effort is made to keep this crate reasonably consistent with std::sync.
Many of the types defined in this crate have ‘additional capabilities’ when compared to std::sync:
-
Because spinning does not depend on the thread-driven model of
std::sync, guards (MutexGuard,RwLockReadGuard,RwLockWriteGuard, etc.) may be sent and shared between threads. -
RwLockUpgradableGuardsupports being upgraded into aRwLockWriteGuard. -
Guards support leaking.
-
Onceowns the value returned by itscall_onceinitializer. -
RwLocksupports counting readers and writers.
Conversely, the types in this crate do not have some of the features std::sync has:
- Locks do not track panic poisoning.
§Feature flags
The crate comes with a few feature flags that you may wish to use.
-
lock_apienables support forlock_api -
ticket_mutexuses a ticket lock for the implementation ofMutex -
fair_mutexenables a fairer implementation ofMutexthat uses eventual fairness to avoid starvation -
stdenables support for thread yielding instead of spinning
Re-exports§
pub use mutex::MutexGuard;mutexpub use relax::RelaxStrategy;pub use relax::Spin;pub use rwlock::RwLockReadGuard;rwlock
Modules§
- barrier
barrierSynchronization primitive allowing multiple threads to synchronize the beginning of some computation. - lazy
lazySynchronization primitives for lazy evaluation. - lock_api
lock_apiSpin synchronisation primitives, but compatible withlock_api. - mutex
mutexLocks that have the same behaviour as a mutex. - once
onceSynchronization primitives for one-time evaluation. - Strategies that determine the behaviour of locks when encountering contention.
- rwlock
rwlockA lock that provides data access to either one writer or many readers.
Type Aliases§
- Barrier
barrierA primitive that synchronizes the execution of multiple threads. Seebarrier::Barrierfor documentation. - Lazy
lazyA value which is initialized on the first access. Seelazy::Lazyfor documentation. - Mutex
mutexA primitive that synchronizes the execution of multiple threads. Seemutex::Mutexfor documentation. - Once
onceA primitive that provides lazy one-time initialization. Seeonce::Oncefor documentation. - RwLock
rwlockA lock that provides data access to either one writer or many readers. Seerwlock::RwLockfor documentation. - RwLockUpgradableGuard
rwlockA guard that provides immutable data access but can be upgraded toRwLockWriteGuard. Seerwlock::RwLockUpgradableGuardfor documentation. - RwLockWriteGuard
rwlockA guard that provides mutable data access. Seerwlock::RwLockWriteGuardfor documentation.