aboutsummaryrefslogtreecommitdiff
path: root/embassy-rp/src/spinlock_mutex.rs
blob: 85174cf8630bb97de3b4cd8be0ee4789d8e1d285 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Mutex implementation utilizing an hardware spinlock

use core::marker::PhantomData;
use core::sync::atomic::Ordering;

use embassy_sync::blocking_mutex::raw::RawMutex;

use crate::spinlock::{Spinlock, SpinlockValid};

/// A mutex that allows borrowing data across executors and interrupts by utilizing an hardware spinlock
///
/// # Safety
///
/// This mutex is safe to share between different executors and interrupts.
pub struct SpinlockRawMutex<const N: usize> {
    _phantom: PhantomData<()>,
}
unsafe impl<const N: usize> Send for SpinlockRawMutex<N> {}
unsafe impl<const N: usize> Sync for SpinlockRawMutex<N> {}

impl<const N: usize> SpinlockRawMutex<N> {
    /// Create a new `SpinlockRawMutex`.
    pub const fn new() -> Self {
        Self { _phantom: PhantomData }
    }
}

unsafe impl<const N: usize> RawMutex for SpinlockRawMutex<N>
where
    Spinlock<N>: SpinlockValid,
{
    const INIT: Self = Self::new();

    fn lock<R>(&self, f: impl FnOnce() -> R) -> R {
        // Store the initial interrupt state in stack variable
        let interrupts_active = cortex_m::register::primask::read().is_active();

        // Spin until we get the lock
        loop {
            // Need to disable interrupts to ensure that we will not deadlock
            // if an interrupt or higher prio locks the spinlock after we acquire the lock
            cortex_m::interrupt::disable();
            // Ensure the compiler doesn't re-order accesses and violate safety here
            core::sync::atomic::compiler_fence(Ordering::SeqCst);
            if let Some(lock) = Spinlock::<N>::try_claim() {
                // We just acquired the lock.
                // 1. Forget it, so we don't immediately unlock
                core::mem::forget(lock);
                break;
            }
            // We didn't get the lock, enable interrupts if they were enabled before we started
            if interrupts_active {
                // safety: interrupts are only enabled, if they had been enabled before
                unsafe {
                    cortex_m::interrupt::enable();
                }
            }
        }

        let retval = f();

        // Ensure the compiler doesn't re-order accesses and violate safety here
        core::sync::atomic::compiler_fence(Ordering::SeqCst);
        // Release the spinlock to allow others to lock mutex again
        // safety: this point is only reached a spinlock was acquired before
        unsafe {
            Spinlock::<N>::release();
        }

        // Re-enable interrupts if they were enabled before the mutex was locked
        if interrupts_active {
            // safety: interrupts are only enabled, if they had been enabled before
            unsafe {
                cortex_m::interrupt::enable();
            }
        }

        retval
    }
}

pub mod blocking_mutex {
    //! Mutex implementation utilizing an hardware spinlock
    use embassy_sync::blocking_mutex::Mutex;

    use crate::spinlock_mutex::SpinlockRawMutex;
    /// A mutex that allows borrowing data across executors and interrupts by utilizing an hardware spinlock.
    ///
    /// # Safety
    ///
    /// This mutex is safe to share between different executors and interrupts.
    pub type SpinlockMutex<const N: usize, T> = Mutex<SpinlockRawMutex<N>, T>;
}