maitake_sync/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg, doc_cfg_hide))]
3#![cfg_attr(docsrs, doc(cfg_hide(docsrs, loom)))]
4#![cfg_attr(not(any(test, feature = "std")), no_std)]
5#![warn(missing_docs, missing_debug_implementations)]
6
7#[cfg(any(feature = "alloc", test))]
8extern crate alloc;
9
10pub(crate) mod loom;
11
12#[macro_use]
13pub mod util;
14
15pub mod blocking;
16pub mod mutex;
17pub mod rwlock;
18pub mod semaphore;
19pub mod spin;
20pub mod wait_cell;
21pub mod wait_map;
22pub mod wait_queue;
23
24#[cfg(feature = "alloc")]
25#[doc(inline)]
26pub use self::mutex::OwnedMutexGuard;
27#[doc(inline)]
28pub use self::mutex::{Mutex, MutexGuard};
29#[cfg(feature = "alloc")]
30#[doc(inline)]
31pub use self::rwlock::{OwnedRwLockReadGuard, OwnedRwLockWriteGuard};
32#[doc(inline)]
33pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
34#[doc(inline)]
35pub use self::semaphore::Semaphore;
36#[doc(inline)]
37pub use self::wait_cell::WaitCell;
38#[doc(inline)]
39pub use self::wait_map::WaitMap;
40#[doc(inline)]
41pub use self::wait_queue::WaitQueue;
42
43use core::task::Poll;
44
45/// An error indicating that a [`WaitCell`], [`WaitQueue`] or [`Semaphore`] was
46/// closed while attempting to register a waiting task.
47///
48/// This error is returned by the [`WaitCell::wait`], [`WaitQueue::wait`] and
49/// [`Semaphore::acquire`] methods.
50#[derive(Copy, Clone, Debug, Eq, PartialEq)]
51pub struct Closed(());
52
53/// The result of waiting on a [`WaitQueue`] or [`Semaphore`].
54pub type WaitResult<T> = Result<T, Closed>;
55
56pub(crate) const fn closed<T>() -> Poll<WaitResult<T>> {
57    Poll::Ready(Err(Closed::new()))
58}
59
60impl Closed {
61    pub(crate) const fn new() -> Self {
62        Self(())
63    }
64}
65
66impl core::fmt::Display for Closed {
67    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
68        f.pad("closed")
69    }
70}
71
72feature! {
73    #![feature = "core-error"]
74    impl core::error::Error for Closed {}
75}