Crate hermit_sync

Source
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:

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 a OnceCell 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§

CallOnce
A synchronization primitive that can only be called once sucessfully.
CallOnceError
The CallOnceError error indicates that CallOnce::call_once has been called more than once.
ExclusiveCell
A synchronization primitive which can be accessed only once.
RawInterruptMutex
A mutex for sharing data with interrupt handlers or signal handlers.
RawOneShotMutex
A one-shot mutex that panics instead of (dead)locking on contention.
RawOneShotRwLock
A one-shot readers-writer lock that panics instead of (dead)locking on contention.
RawTicketMutex
A fair ticket lock with exponential backoff.

Functions§

without_interrupts
Run a closure with disabled interrupts.

Type Aliases§

InterruptLazy
A generic_once_cell::Lazy, initialized using RawInterruptSpinMutex.
InterruptMutex
A lock_api::Mutex based on RawInterruptMutex.
InterruptMutexGuard
A lock_api::MutexGuard based on RawInterruptMutex.
InterruptOnceCell
A generic_once_cell::OnceCell, initialized using RawInterruptSpinMutex.
InterruptOneShotMutex
A lock_api::Mutex based on RawInterruptOneShotMutex.
InterruptOneShotMutexGuard
A lock_api::MutexGuard based on RawInterruptOneShotMutex.
InterruptSpinMutex
A lock_api::Mutex based on RawInterruptSpinMutex.
InterruptSpinMutexGuard
A lock_api::MutexGuard based on RawInterruptSpinMutex.
InterruptTicketMutex
A lock_api::Mutex based on RawInterruptTicketMutex.
InterruptTicketMutexGuard
A lock_api::MutexGuard based on RawInterruptTicketMutex.
Lazy
A generic_once_cell::Lazy, initialized using RawSpinMutex.
OnceCell
A generic_once_cell::OnceCell, initialized using RawSpinMutex.
OneShotMutex
A lock_api::Mutex based on RawOneShotMutex.
OneShotMutexGuard
A lock_api::MutexGuard based on RawOneShotMutex.
OneShotRwLock
A lock_api::RwLock based on RawOneShotRwLock.
OneShotRwLockReadGuard
A lock_api::RwLockReadGuard based on RawOneShotRwLock.
OneShotRwLockUpgradableReadGuard
A lock_api::RwLockUpgradableReadGuard based on RawOneShotRwLock.
OneShotRwLockWriteGuard
A lock_api::RwLockWriteGuard based on RawOneShotRwLock.
RawInterruptOneShotMutex
An interrupt-safe RawOneShotMutex.
RawInterruptSpinMutex
An interrupt-safe RawSpinMutex.
RawInterruptTicketMutex
An interrupt-safe RawTicketMutex.
RawRwSpinLock
A simple spinning, read-preferring readers-writer lock with exponential backoff.
RawSpinMutex
A simple spinlock with exponential backoff.
RwSpinLock
A lock_api::RwLock based on RawRwSpinLock.
RwSpinLockReadGuard
A lock_api::RwLockReadGuard based on RawRwSpinLock.
RwSpinLockUpgradableReadGuard
A lock_api::RwLockUpgradableReadGuard based on RawRwSpinLock.
RwSpinLockWriteGuard
A lock_api::RwLockWriteGuard based on RawRwSpinLock.
SpinMutex
A lock_api::Mutex based on RawSpinMutex.
SpinMutexGuard
A lock_api::MutexGuard based on RawSpinMutex.
TicketMutex
A lock_api::Mutex based on RawTicketMutex.
TicketMutexGuard
A lock_api::MutexGuard based on RawTicketMutex.