Expand description
§Overview
hermit-sync provides synchronization primitives targeted at operating system kernels.
§Interrupts
without_interrupts
runs a closure with disabled interrupts.
§Mutexes
This crate provides three kinds of mutexes based on lock_api::RawMutex
:
RawSpinMutex
is a simple test and test-and-set spinlock with exponential backoff.RawTicketMutex
is a fair ticket lock with exponential backoff.RawInterruptMutex
wraps another mutex and disables interrupts while locked.
For API documentation see lock_api::Mutex
.
§Examples
use hermit_sync::InterruptSpinMutex;
static NUMBER: InterruptSpinMutex<usize> = InterruptSpinMutex::new(0);
// Modify the data
*NUMBER.lock() = 2;
// Read the data
let answer = *NUMBER.lock();
assert_eq!(2, answer);
§Initializing Static Data
There are two primitives for safely initializing static data based on generic_once_cell
and RawSpinMutex
:
OnceCell
can be written to only once and can then be accessed without locking.Lazy
wraps aOnceCell
and is initialized on the first access from a closure.
For API documentation see generic_once_cell::OnceCell
and generic_once_cell::Lazy
.
§Examples
use std::collections::HashMap;
use hermit_sync::InterruptLazy;
static MAP: InterruptLazy<HashMap<usize, String>> = InterruptLazy::new(|| {
// This is run on the first access of MAP.
let mut map = HashMap::new();
map.insert(42, "Ferris".to_string());
map.insert(3, "やれやれだぜ".to_string());
map
});
assert_eq!("Ferris", MAP.get(&42).unwrap());
§Accessing Static Data Mutably
There is ExclusiveCell
for safely accessing static data mutable once.
§Type Definitions
This crate provides a lot of type definitions for ease of use:
Structs§
- Call
Once - A synchronization primitive that can only be called once sucessfully.
- Call
Once Error - The
CallOnceError
error indicates thatCallOnce::call_once
has been called more than once. - Exclusive
Cell - A synchronization primitive which can be accessed only once.
- RawInterrupt
Mutex - A mutex for sharing data with interrupt handlers or signal handlers.
- RawOne
Shot Mutex - A one-shot mutex that panics instead of (dead)locking on contention.
- RawOne
Shot RwLock - A one-shot readers-writer lock that panics instead of (dead)locking on contention.
- RawTicket
Mutex - A fair ticket lock with exponential backoff.
Functions§
- without_
interrupts - Run a closure with disabled interrupts.
Type Aliases§
- Interrupt
Lazy - A
generic_once_cell::Lazy
, initialized usingRawInterruptSpinMutex
. - Interrupt
Mutex - A
lock_api::Mutex
based onRawInterruptMutex
. - Interrupt
Mutex Guard - A
lock_api::MutexGuard
based onRawInterruptMutex
. - Interrupt
Once Cell - A
generic_once_cell::OnceCell
, initialized usingRawInterruptSpinMutex
. - Interrupt
OneShot Mutex - A
lock_api::Mutex
based onRawInterruptOneShotMutex
. - Interrupt
OneShot Mutex Guard - A
lock_api::MutexGuard
based onRawInterruptOneShotMutex
. - Interrupt
Spin Mutex - A
lock_api::Mutex
based onRawInterruptSpinMutex
. - Interrupt
Spin Mutex Guard - A
lock_api::MutexGuard
based onRawInterruptSpinMutex
. - Interrupt
Ticket Mutex - A
lock_api::Mutex
based onRawInterruptTicketMutex
. - Interrupt
Ticket Mutex Guard - A
lock_api::MutexGuard
based onRawInterruptTicketMutex
. - Lazy
- A
generic_once_cell::Lazy
, initialized usingRawSpinMutex
. - Once
Cell - A
generic_once_cell::OnceCell
, initialized usingRawSpinMutex
. - OneShot
Mutex - A
lock_api::Mutex
based onRawOneShotMutex
. - OneShot
Mutex Guard - A
lock_api::MutexGuard
based onRawOneShotMutex
. - OneShot
RwLock - A
lock_api::RwLock
based onRawOneShotRwLock
. - OneShot
RwLock Read Guard - A
lock_api::RwLockReadGuard
based onRawOneShotRwLock
. - OneShot
RwLock Upgradable Read Guard - A
lock_api::RwLockUpgradableReadGuard
based onRawOneShotRwLock
. - OneShot
RwLock Write Guard - A
lock_api::RwLockWriteGuard
based onRawOneShotRwLock
. - RawInterrupt
OneShot Mutex - An interrupt-safe
RawOneShotMutex
. - RawInterrupt
Spin Mutex - An interrupt-safe
RawSpinMutex
. - RawInterrupt
Ticket Mutex - An interrupt-safe
RawTicketMutex
. - RawRw
Spin Lock - A simple spinning, read-preferring readers-writer lock with exponential backoff.
- RawSpin
Mutex - A simple spinlock with exponential backoff.
- RwSpin
Lock - A
lock_api::RwLock
based onRawRwSpinLock
. - RwSpin
Lock Read Guard - A
lock_api::RwLockReadGuard
based onRawRwSpinLock
. - RwSpin
Lock Upgradable Read Guard - A
lock_api::RwLockUpgradableReadGuard
based onRawRwSpinLock
. - RwSpin
Lock Write Guard - A
lock_api::RwLockWriteGuard
based onRawRwSpinLock
. - Spin
Mutex - A
lock_api::Mutex
based onRawSpinMutex
. - Spin
Mutex Guard - A
lock_api::MutexGuard
based onRawSpinMutex
. - Ticket
Mutex - A
lock_api::Mutex
based onRawTicketMutex
. - Ticket
Mutex Guard - A
lock_api::MutexGuard
based onRawTicketMutex
.