rustix_futex_sync/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5// Re-export this so that our users can use the same version we do.
6#[cfg(feature = "lock_api")]
7pub use lock_api;
8
9// If we don't have the real `lock_api` crate, use our polyfills.
10#[cfg(not(feature = "lock_api"))]
11pub mod lock_api;
12
13#[cfg(feature = "lock_api")]
14pub use condvar::WaitTimeoutResult;
15pub use once::OnceState;
16
17// Non-shared API.
18
19pub type Once = generic::Once<false>;
20#[cfg(feature = "lock_api")]
21pub type Condvar = generic::Condvar<false>;
22pub type RawCondvar = generic::RawCondvar<false>;
23pub type RawMutex = generic::RawMutex<false>;
24pub type RawRwLock = generic::RawRwLock<false>;
25pub type OnceLock<T> = generic::OnceLock<T, false>;
26#[cfg(feature = "lock_api")]
27pub type Mutex<T> = generic::Mutex<T, false>;
28#[cfg(feature = "lock_api")]
29pub type RwLock<T> = generic::RwLock<T, false>;
30#[cfg(feature = "lock_api")]
31pub type MutexGuard<'a, T> = generic::MutexGuard<'a, T, false>;
32#[cfg(feature = "lock_api")]
33pub type MappedMutexGuard<'a, T> = generic::MappedMutexGuard<'a, T, false>;
34#[cfg(feature = "lock_api")]
35pub type RwLockReadGuard<'a, T> = generic::RwLockReadGuard<'a, T, false>;
36#[cfg(feature = "lock_api")]
37pub type RwLockWriteGuard<'a, T> = generic::RwLockWriteGuard<'a, T, false>;
38#[cfg(feature = "lock_api")]
39pub type MappedRwLockReadGuard<'a, T> = generic::MappedRwLockReadGuard<'a, T, false>;
40#[cfg(feature = "lock_api")]
41pub type MappedRwLockWriteGuard<'a, T> = generic::MappedRwLockWriteGuard<'a, T, false>;
42#[cfg(feature = "lock_api")]
43#[cfg(feature = "atomic_usize")]
44#[cfg_attr(docsrs, doc(cfg(feature = "atomic_usize")))]
45pub type ReentrantMutex<G, T> = generic::ReentrantMutex<G, T, false>;
46#[cfg(feature = "lock_api")]
47#[cfg(feature = "atomic_usize")]
48#[cfg_attr(docsrs, doc(cfg(feature = "atomic_usize")))]
49pub type ReentrantMutexGuard<'a, G, T> = generic::ReentrantMutexGuard<'a, G, T, false>;
50
51/// Shared-memory API.
52///
53/// The types in this module behave the same as the types defined at the top
54/// level of this crate, except that they don't set the `FUTEX_PRIVATE_FLAG`
55/// flag, so they can be used on memory shared with other processes.
56///
57/// See [the Linux documentation] for more information about
58/// `FUTEX_PRIVATE_FLAG`.
59///
60/// [the Linux documentation]: https://man7.org/linux/man-pages/man2/futex.2.html
61#[cfg(feature = "shm")]
62#[cfg_attr(docsrs, doc(cfg(feature = "shm")))]
63pub mod shm {
64    use crate::generic;
65
66    pub use super::lock_api;
67
68    pub type Once = generic::Once<true>;
69    #[cfg(feature = "lock_api")]
70    pub type Condvar = generic::Condvar<true>;
71    pub type RawCondvar = generic::RawCondvar<true>;
72    pub type RawMutex = generic::RawMutex<true>;
73    pub type RawRwLock = generic::RawRwLock<true>;
74    pub type OnceLock<T> = generic::OnceLock<T, true>;
75    #[cfg(feature = "lock_api")]
76    pub type Mutex<T> = generic::Mutex<T, true>;
77    #[cfg(feature = "lock_api")]
78    pub type RwLock<T> = generic::RwLock<T, true>;
79    #[cfg(feature = "lock_api")]
80    pub type MutexGuard<'a, T> = generic::MutexGuard<'a, T, true>;
81    #[cfg(feature = "lock_api")]
82    pub type MappedMutexGuard<'a, T> = generic::MappedMutexGuard<'a, T, true>;
83    #[cfg(feature = "lock_api")]
84    pub type RwLockReadGuard<'a, T> = generic::RwLockReadGuard<'a, T, true>;
85    #[cfg(feature = "lock_api")]
86    pub type RwLockWriteGuard<'a, T> = generic::RwLockWriteGuard<'a, T, true>;
87    #[cfg(feature = "lock_api")]
88    pub type MappedRwLockReadGuard<'a, T> = generic::MappedRwLockReadGuard<'a, T, true>;
89    #[cfg(feature = "lock_api")]
90    pub type MappedRwLockWriteGuard<'a, T> = generic::MappedRwLockWriteGuard<'a, T, true>;
91    #[cfg(feature = "lock_api")]
92    #[cfg(feature = "atomic_usize")]
93    #[cfg_attr(docsrs, doc(cfg(feature = "atomic_usize")))]
94    pub type ReentrantMutex<G, T> = generic::ReentrantMutex<G, T, true>;
95    #[cfg(feature = "lock_api")]
96    #[cfg(feature = "atomic_usize")]
97    #[cfg_attr(docsrs, doc(cfg(feature = "atomic_usize")))]
98    pub type ReentrantMutexGuard<'a, G, T> = generic::ReentrantMutexGuard<'a, G, T, true>;
99}
100
101/// Types and traits with a `const SHM: bool>` generic paramters.
102///
103/// These are the generic types that are parameterized on whether they support
104/// shared memory or not. They are aliased as non-parameterized types in the
105/// top-level crate and in the `shm` module for better ergonomics.
106pub mod generic {
107    #[cfg(feature = "lock_api")]
108    pub use crate::condvar::Condvar;
109    pub use crate::futex_condvar::Condvar as RawCondvar;
110    pub use crate::once::Once;
111    pub use crate::once_lock::OnceLock;
112    pub use crate::raw_mutex::RawMutex;
113    pub use crate::raw_rwlock::RawRwLock;
114
115    #[cfg(feature = "lock_api")]
116    pub type Mutex<T, const SHM: bool> = lock_api::Mutex<RawMutex<SHM>, T>;
117    #[cfg(feature = "lock_api")]
118    pub type RwLock<T, const SHM: bool> = lock_api::RwLock<RawRwLock<SHM>, T>;
119    #[cfg(feature = "lock_api")]
120    pub type MutexGuard<'a, T, const SHM: bool> = lock_api::MutexGuard<'a, RawMutex<SHM>, T>;
121    #[cfg(feature = "lock_api")]
122    pub type MappedMutexGuard<'a, T, const SHM: bool> =
123        lock_api::MappedMutexGuard<'a, RawMutex<SHM>, T>;
124    #[cfg(feature = "lock_api")]
125    pub type RwLockReadGuard<'a, T, const SHM: bool> =
126        lock_api::RwLockReadGuard<'a, RawRwLock<SHM>, T>;
127    #[cfg(feature = "lock_api")]
128    pub type RwLockWriteGuard<'a, T, const SHM: bool> =
129        lock_api::RwLockWriteGuard<'a, RawRwLock<SHM>, T>;
130    #[cfg(feature = "lock_api")]
131    pub type MappedRwLockReadGuard<'a, T, const SHM: bool> =
132        lock_api::MappedRwLockReadGuard<'a, RawRwLock<SHM>, T>;
133    #[cfg(feature = "lock_api")]
134    pub type MappedRwLockWriteGuard<'a, T, const SHM: bool> =
135        lock_api::MappedRwLockWriteGuard<'a, RawRwLock<SHM>, T>;
136    #[cfg(feature = "lock_api")]
137    #[cfg(feature = "atomic_usize")]
138    #[cfg_attr(docsrs, doc(cfg(feature = "atomic_usize")))]
139    pub type ReentrantMutex<G, T, const SHM: bool> = lock_api::ReentrantMutex<RawMutex<SHM>, G, T>;
140    #[cfg(feature = "lock_api")]
141    #[cfg(feature = "atomic_usize")]
142    #[cfg_attr(docsrs, doc(cfg(feature = "atomic_usize")))]
143    pub type ReentrantMutexGuard<'a, G, T, const SHM: bool> =
144        lock_api::ReentrantMutexGuard<'a, RawMutex<SHM>, G, T>;
145}
146
147// std's implementation code.
148#[cfg(feature = "lock_api")]
149mod condvar;
150mod futex_condvar;
151mod futex_mutex;
152mod futex_once;
153mod futex_rwlock;
154mod once;
155mod once_lock;
156mod raw_mutex;
157mod raw_rwlock;
158mod wait_wake;