Struct rp2040_hal::sio::Spinlock

source ·
pub struct Spinlock<const N: usize>(/* private fields */)
where
    Spinlock<N>: SpinlockValid;
Expand description

Hardware based spinlock.

You can claim this lock by calling either claim, try_claim or claim_async. These spin-locks are hardware backed, so if you lock e.g. Spinlock<6>, then any other part of your application using Spinlock<6> will contend for the same lock, without them needing to share a reference or otherwise communicate with each other.

When the obtained spinlock goes out of scope, it is automatically unlocked.

use rp2040_hal::sio::Spinlock0;
static mut SOME_GLOBAL_VAR: u32 = 0;

/// This function is safe to call from two different cores, but is not safe
/// to call from an interrupt routine!
fn update_global_var() {
    // Do not say `let _ = ` here - it will immediately unlock!
    let _lock = Spinlock0::claim();
    // Do your thing here that Core 0 and Core 1 might want to do at the
    // same time, like update this global variable:
    unsafe { SOME_GLOBAL_VAR += 1 };
    // The lock is dropped here.
}

Warning: These spinlocks are not re-entrant, meaning that the following code will cause a deadlock:

use rp2040_hal::sio::Spinlock0;
let lock_1 = Spinlock0::claim();
let lock_2 = Spinlock0::claim(); // deadlock here

Note: The critical-section implementation uses Spinlock 31.

Implementations§

source§

impl<const N: usize> Spinlock<N>

source

pub fn try_claim() -> Option<Self>

Try to claim the spinlock. Will return Some(Self) if the lock is obtained, and None if the lock is already in use somewhere else.

source

pub fn claim() -> Self

Claim the spinlock, will block the current thread until the lock is available.

Note that calling this multiple times in a row will cause a deadlock

source

pub fn claim_async() -> Result<Self, Infallible>

Try to claim the spinlock. Will return WouldBlock until the spinlock is available.

source

pub unsafe fn release()

Clear a locked spin-lock.

§Safety

Only call this function if you hold the spin-lock.

Trait Implementations§

source§

impl<const N: usize> Drop for Spinlock<N>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl SpinlockValid for Spinlock<0>

source§

impl SpinlockValid for Spinlock<1>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<31>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

source§

impl SpinlockValid for Spinlock<$rest>

Auto Trait Implementations§

§

impl<const N: usize> RefUnwindSafe for Spinlock<N>

§

impl<const N: usize> Send for Spinlock<N>

§

impl<const N: usize> Sync for Spinlock<N>

§

impl<const N: usize> Unpin for Spinlock<N>

§

impl<const N: usize> UnwindSafe for Spinlock<N>

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<Choices> CoproductSubsetter<CNil, HNil> for Choices

§

type Remainder = Choices

source§

fn subset( self ) -> Result<CNil, <Choices as CoproductSubsetter<CNil, HNil>>::Remainder>

Extract a subset of the possible types in a coproduct (or get the remaining possibilities) 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> Is for T
where T: Sealed + Borrow<T> + BorrowMut<T>,

§

type Type = T

source§

impl<T, U, I> LiftInto<U, I> for T
where U: LiftFrom<T, I>,

source§

fn lift_into(self) -> U

Performs the indexed conversion.
source§

impl<Source> Sculptor<HNil, HNil> for Source

§

type Remainder = Source

source§

fn sculpt(self) -> (HNil, <Source as Sculptor<HNil, HNil>>::Remainder)

Consumes the current HList and returns an HList with the requested shape. Read more
source§

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

§

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>,

§

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.