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. -
[
RwLockUpgradableGuard] supports being upgraded into a [RwLockWriteGuard]. -
Guards support leaking.
-
Onceowns the value returned by itscall_onceinitializer. -
[
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_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
Modules§
- lazy
- Synchronization primitives for lazy evaluation.
- mutex
- Locks that have the same behaviour as a mutex.
- once
- Synchronization primitives for one-time evaluation.
- relax
- Strategies that determine the behaviour of locks when encountering contention.
Structs§
- Mutex
Guard - A generic guard that will protect some data access and uses either a ticket lock or a normal spin mutex.
- Spin
- A strategy that rapidly spins while informing the CPU that it should power down non-essential components via
core::hint::spin_loop.
Traits§
- Relax
Strategy - A trait implemented by spinning relax strategies.
Type Aliases§
- Lazy
- A value which is initialized on the first access. See
lazy::Lazyfor documentation. - Mutex
- A primitive that synchronizes the execution of multiple threads. See
mutex::Mutexfor documentation. - Once
- A primitive that provides lazy one-time initialization. See
once::Oncefor documentation.