stm32f1_hal/common/
mutex.rs

1use core::marker::PhantomData;
2use mutex_traits::{ConstInit, RawMutex};
3
4/// Wrap `BlockingMutex`` and only provide try_lock and try_with_lock methods.
5pub struct Mutex<R, T: ?Sized>(mutex::BlockingMutex<R, T>);
6
7impl<R: ConstInit, T> Mutex<R, T> {
8    #[inline(always)]
9    pub const fn new(data: T) -> Self {
10        Self(mutex::BlockingMutex::<R, T>::new(data))
11    }
12}
13
14impl<R: RawMutex, T: ?Sized> Mutex<R, T> {
15    #[inline(always)]
16    pub fn try_lock(&self) -> Option<mutex::MutexGuard<'_, R, T>> {
17        self.0.try_lock()
18    }
19
20    #[inline(always)]
21    pub fn try_with_lock<U>(&self, f: impl FnOnce(&mut T) -> U) -> Option<U> {
22        self.0.try_with_lock(f)
23    }
24}
25
26// ----------------------------------------------------
27
28/// A fake mutex that allows borrowing data in local context.
29///
30/// Which means it does not provide any synchronization between threads,
31pub struct FakeRawMutex {
32    /// Prevent this from being sync
33    _phantom: PhantomData<*mut ()>,
34}
35
36impl FakeRawMutex {
37    /// Create a new `FakeRawMutex`.
38    pub const fn new() -> Self {
39        Self {
40            _phantom: PhantomData,
41        }
42    }
43}
44
45unsafe impl Send for FakeRawMutex {}
46
47impl ConstInit for FakeRawMutex {
48    const INIT: Self = Self::new();
49}
50
51unsafe impl RawMutex for FakeRawMutex {
52    type GuardMarker = *mut ();
53
54    #[inline]
55    fn lock(&self) {}
56
57    #[inline]
58    fn try_lock(&self) -> bool {
59        true
60    }
61
62    #[inline]
63    unsafe fn unlock(&self) {}
64
65    #[inline]
66    fn is_locked(&self) -> bool {
67        true
68    }
69}