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
, andSyncLazy
equivalents -
Support for
no_std
environments -
lock_api
compatibility -
Upgradeable
RwLock
guards -
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. -
RwLockUpgradableGuard
supports being upgraded into aRwLockWriteGuard
. -
Guards support leaking.
-
Once
owns the value returned by itscall_once
initializer. -
RwLock
supports 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_api
enables support forlock_api
-
ticket_mutex
uses a ticket lock for the implementation ofMutex
-
fair_mutex
enables a fairer implementation ofMutex
that uses eventual fairness to avoid starvation -
std
enables support for thread yielding instead of spinning
Re-exports
pub use mutex::MutexGuard;
pub use relax::Yield;
std
pub use relax::RelaxStrategy;
pub use relax::Spin;
pub use rwlock::RwLockReadGuard;
Modules
barrier
lazy
mutex
once
rwlock
Type Definitions
barrier
barrier::Barrier
for documentation.lazy
lazy::Lazy
for documentation.mutex
mutex::Mutex
for documentation.once
once::Once
for documentation.rwlock
rwlock::RwLock
for documentation.rwlock
RwLockWriteGuard
. See
rwlock::RwLockUpgradableGuard
for documentation.rwlock
rwlock::RwLockWriteGuard
for documentation.