Add missing documentation for all public modules and types

This commit is contained in:
Ulf Lilleengen 2022-06-15 10:24:36 +02:00
parent aaebea00eb
commit 72eb16b46d
6 changed files with 86 additions and 11 deletions

View file

@ -1,14 +1,23 @@
//! Blocking mutex (not async)
//! Blocking mutex.
//!
//! This module provides a blocking mutex that can be used to synchronize data.
pub mod raw;
use core::cell::UnsafeCell;
use self::raw::RawMutex;
/// Any object implementing this trait guarantees exclusive access to the data contained
/// within the mutex for the duration of the lock.
/// Adapted from <https://github.com/rust-embedded/mutex-trait>.
/// Blocking mutex (not async)
///
/// Provides a blocking mutual exclusion primitive backed by an implementation of [`raw::RawMutex`].
///
/// Which implementation you select depends on the context in which you're using the mutex.
///
/// Use [`CriticalSectionMutex`] when data can be shared between threads and interrupts.
///
/// Use [`NoopMutex`] when data is only shared between tasks running on the same executor.
///
/// Use [`ThreadModeMutex`] when data is shared between tasks running on the same executor but you want a global singleton.
pub struct Mutex<R, T: ?Sized> {
// NOTE: `raw` must be FIRST, so when using ThreadModeMutex the "can't drop in non-thread-mode" gets
// to run BEFORE dropping `data`.
@ -78,7 +87,18 @@ impl<R, T> Mutex<R, T> {
}
}
/// A mutex that allows borrowing data across executors and interrupts.
///
/// # Safety
///
/// This mutex is safe to share between different executors and interrupts.
pub type CriticalSectionMutex<T> = Mutex<raw::CriticalSectionRawMutex, T>;
/// A mutex that allows borrowing data in the context of a single executor.
///
/// # Safety
///
/// **This Mutex is only safe within a single executor.**
pub type NoopMutex<T> = Mutex<raw::NoopRawMutex, T>;
impl<T> Mutex<raw::CriticalSectionRawMutex, T> {

View file

@ -1,11 +1,22 @@
//! Mutex primitives.
//!
//! This module provides a trait for mutexes that can be used in different contexts.
use core::marker::PhantomData;
/// Any object implementing this trait guarantees exclusive access to the data contained
/// within the mutex for the duration of the lock.
/// Adapted from <https://github.com/rust-embedded/mutex-trait>.
pub trait RawMutex {
const INIT: Self;
fn lock<R>(&self, f: impl FnOnce() -> R) -> R;
}
/// A mutex that allows borrowing data across executors and interrupts.
///
/// # Safety
///
/// This mutex is safe to share between different executors and interrupts.
pub struct CriticalSectionRawMutex {
_phantom: PhantomData<()>,
}
@ -28,6 +39,11 @@ impl RawMutex for CriticalSectionRawMutex {
// ================
/// A mutex that allows borrowing data in the context of a single executor.
///
/// # Safety
///
/// **This Mutex is only safe within a single executor.**
pub struct NoopRawMutex {
_phantom: PhantomData<*mut ()>,
}
@ -53,6 +69,13 @@ impl RawMutex for NoopRawMutex {
mod thread_mode {
use super::*;
/// A "mutex" that only allows borrowing from thread mode.
///
/// # Safety
///
/// **This Mutex is only safe on single-core systems.**
///
/// On multi-core systems, a `ThreadModeRawMutex` **is not sufficient** to ensure exclusive access.
pub struct ThreadModeRawMutex {
_phantom: PhantomData<()>,
}

View file

@ -1,3 +1,4 @@
//! A synchronization primitive for passing the latest value to a task.
use core::cell::UnsafeCell;
use core::future::Future;
use core::mem;

View file

@ -1,9 +1,6 @@
/// Async mutex.
///
/// The mutex is generic over a blocking [`RawMutex`](crate::blocking_mutex::raw::RawMutex).
/// The raw mutex is used to guard access to the internal "is locked" flag. It
/// is held for very short periods only, while locking and unlocking. It is *not* held
/// for the entire time the async Mutex is locked.
//! Async mutex.
//!
//! This module provides a mutex that can be used to synchronize data between asynchronous tasks.
use core::cell::{RefCell, UnsafeCell};
use core::ops::{Deref, DerefMut};
use core::task::Poll;
@ -24,6 +21,21 @@ struct State {
waker: WakerRegistration,
}
/// Async mutex.
///
/// The mutex is generic over a blocking [`RawMutex`](crate::blocking_mutex::raw::RawMutex).
/// The raw mutex is used to guard access to the internal "is locked" flag. It
/// is held for very short periods only, while locking and unlocking. It is *not* held
/// for the entire time the async Mutex is locked.
///
/// Which implementation you select depends on the context in which you're using the mutex.
///
/// Use [`CriticalSectionRawMutex`](crate::blocking_mutex::raw::CriticalSectionRawMutex) when data can be shared between threads and interrupts.
///
/// Use [`NoopRawMutex`](crate::blocking_mutex::raw::NoopRawMutex) when data is only shared between tasks running on the same executor.
///
/// Use [`ThreadModeRawMutex`](crate::blocking_mutex::raw::ThreadModeRawMutex) when data is shared between tasks running on the same executor but you want a singleton.
///
pub struct Mutex<M, T>
where
M: RawMutex,

View file

@ -1,3 +1,13 @@
/// A type that can retrieved unsafely from anywhere.
pub trait Steal {
/// Retrieve and instance of this type.
///
/// # Safety
///
/// It is the responsibility of the application to ensure that the
/// usage of the returned instance is not in conflict with other uses
/// of this instance.
///
/// The implementation may panic if the instance is already in use.
unsafe fn steal() -> Self;
}

View file

@ -6,6 +6,10 @@ use atomic_polyfill::{compiler_fence, AtomicPtr, Ordering};
use crate::executor::raw::{task_from_waker, wake_task, TaskHeader};
/// Utility struct to register and wake a waker.
///
/// # Safety
///
/// This type is optimized for (and only works with) embassy tasks.
#[derive(Debug)]
pub struct WakerRegistration {
waker: Option<NonNull<TaskHeader>>,
@ -53,6 +57,11 @@ impl WakerRegistration {
unsafe impl Send for WakerRegistration {}
unsafe impl Sync for WakerRegistration {}
/// Utility struct to atomically register and wake a waker.
///
/// # Safety
///
/// This type is optimized for (and only works with) embassy tasks.
pub struct AtomicWaker {
waker: AtomicPtr<TaskHeader>,
}