Struct PriorityLock

Source
pub struct PriorityLock<T> { /* private fields */ }
Expand description

A lock that allows sharing data between two interrupts at different priorities.

This is a general spinlock-like implementation that works even on architectures without compare-and-swap instructions. This is accomplished by making use of Peterson’s Algorithm.

§Drawbacks

Being a general architecture-independent implementation means that it also comes with some drawbacks due to not knowing anything about the target platform:

  • It is limited to 2 parties sharing data. Peterson’s Algorithm requires storage proportional to the number of parties competing for exclusive access. With const generics it might be possible to make this a compile-time parameter instead.
  • Locking from an interrupt can fail irrecoverably. This is a fundamental limitation of trying to ensure exclusive access via blocking mutexes in the presence of interrupts, and would also occur when using any other generic solution (like a “real” spinlock). User code must handle a failure to acquire a resource in an interrupt handler gracefully.

§Alternatives

If the drawbacks listed above are unacceptable (which is not unlikely), consider using one of these alternatives for sharing data between interrupts:

  • Lock-free datastructures such as those provided by heapless.
  • Atomics and read-modify-write operations from core::sync::atomic (if your target supports them).
  • A Mutex implementation that turns off interrupts (when targeting a single-core MCU).
  • A hardware-provided Mutex peripheral (when targeting a multi-core MCU).
  • The Real-Time For the Masses framework.

Implementations§

Source§

impl<T> PriorityLock<T>

Source

pub const fn new(data: T) -> Self

Creates a new lock protecting data.

If data consists of zeroes, the resulting PriorityLock will also be zero-initialized and can be placed in .bss by the compiler.

Source

pub fn split<'a>( &'a mut self, ) -> (LockHalf<'a, T, PLow>, LockHalf<'a, T, PHigh>)

Splits this lock into its low- and high-priority halfs.

The low-priority half provides a lock method for acquiring the lock, and is meant to be used from a lower-priority context than the high-priority half (eg. a low-priority interrupt or the application’s idle loop). The high-priority half provides a try_lock method for acquiring the lock, which may fail when preempting code holding the low-priority half of the lock.

Trait Implementations§

Source§

impl<T: Debug> Debug for PriorityLock<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for PriorityLock<T>

§

impl<T> !RefUnwindSafe for PriorityLock<T>

§

impl<T> Send for PriorityLock<T>
where T: Send,

§

impl<T> !Sync for PriorityLock<T>

§

impl<T> Unpin for PriorityLock<T>
where T: Unpin,

§

impl<T> UnwindSafe for PriorityLock<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.