Skip to main content

flag_bearer_mutex/
lib.rs

1#![no_std]
2#![warn(
3    unsafe_op_in_unsafe_fn,
4    clippy::missing_safety_doc,
5    clippy::multiple_unsafe_ops_per_block,
6    clippy::undocumented_unsafe_blocks
7)]
8
9cfg_if::cfg_if! {
10    if #[cfg(target_os = "linux")] {
11        // linux seems to have better perf with just futex than with parking_lot.
12        #[path = "futex.rs"]
13        mod default_raw;
14    } else {
15        #[path = "parking_lot.rs"]
16        mod default_raw;
17    }
18}
19
20pub use lock_api;
21
22/// A [`lock_api::RawMutex`] that is tuned for good performance for expected flag_bearer semaphore use cases.
23///
24/// # Implementation details
25/// * On linux, this uses a futex
26/// * On all other platforms, this uses parking-lot.
27pub struct RawMutex(default_raw::RawMutex);
28
29/// Safety: This forwards all mutual exclusion responsilibity to the inner type.
30unsafe impl lock_api::RawMutex for RawMutex {
31    #[allow(clippy::declare_interior_mutable_const)]
32    const INIT: RawMutex = RawMutex(default_raw::RawMutex::INIT);
33
34    type GuardMarker = lock_api::GuardNoSend;
35
36    #[inline]
37    fn lock(&self) {
38        self.0.lock();
39    }
40
41    #[inline]
42    fn try_lock(&self) -> bool {
43        self.0.try_lock()
44    }
45
46    #[inline]
47    unsafe fn unlock(&self) {
48        // Safety: from caller
49        unsafe { self.0.unlock() };
50    }
51
52    #[inline]
53    fn is_locked(&self) -> bool {
54        self.0.is_locked()
55    }
56}