1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg, doc_cfg_hide))]
#![cfg_attr(docsrs, doc(cfg_hide(docsrs, loom)))]
#![cfg_attr(not(test), no_std)]
#![cfg_attr(feature = "core-error", feature(error_in_core))]
#![warn(missing_docs, missing_debug_implementations)]

#[cfg(feature = "alloc")]
extern crate alloc;

pub(crate) mod loom;

#[macro_use]
pub mod util;

pub mod mutex;
pub mod rwlock;
pub mod semaphore;
pub mod spin;
pub mod wait_cell;
pub mod wait_map;
pub mod wait_queue;

#[cfg(feature = "alloc")]
#[doc(inline)]
pub use self::mutex::OwnedMutexGuard;
#[doc(inline)]
pub use self::mutex::{Mutex, MutexGuard};
#[cfg(feature = "alloc")]
#[doc(inline)]
pub use self::rwlock::{OwnedRwLockReadGuard, OwnedRwLockWriteGuard};
#[doc(inline)]
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
#[doc(inline)]
pub use self::semaphore::Semaphore;
#[doc(inline)]
pub use self::wait_cell::WaitCell;
#[doc(inline)]
pub use self::wait_map::WaitMap;
#[doc(inline)]
pub use self::wait_queue::WaitQueue;

use core::task::Poll;

/// An error indicating that a [`WaitCell`], [`WaitQueue`] or [`Semaphore`] was
/// closed while attempting to register a waiting task.
///
/// This error is returned by the [`WaitCell::wait`], [`WaitQueue::wait`] and
/// [`Semaphore::acquire`] methods.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Closed(());

/// The result of waiting on a [`WaitQueue`] or [`Semaphore`].
pub type WaitResult<T> = Result<T, Closed>;

pub(crate) const fn closed<T>() -> Poll<WaitResult<T>> {
    Poll::Ready(Err(Closed::new()))
}

impl Closed {
    pub(crate) const fn new() -> Self {
        Self(())
    }
}

impl core::fmt::Display for Closed {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.pad("closed")
    }
}

feature! {
    #![feature = "core-error"]
    impl core::error::Error for Closed {}
}