Spinlock

Struct 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 rp235x_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 rp235x_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<Spinlock<N>>

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() -> Spinlock<N>

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<Spinlock<N>, Error<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<10>

Source§

impl SpinlockValid for Spinlock<11>

Source§

impl SpinlockValid for Spinlock<12>

Source§

impl SpinlockValid for Spinlock<13>

Source§

impl SpinlockValid for Spinlock<14>

Source§

impl SpinlockValid for Spinlock<15>

Source§

impl SpinlockValid for Spinlock<16>

Source§

impl SpinlockValid for Spinlock<17>

Source§

impl SpinlockValid for Spinlock<18>

Source§

impl SpinlockValid for Spinlock<19>

Source§

impl SpinlockValid for Spinlock<2>

Source§

impl SpinlockValid for Spinlock<20>

Source§

impl SpinlockValid for Spinlock<21>

Source§

impl SpinlockValid for Spinlock<22>

Source§

impl SpinlockValid for Spinlock<23>

Source§

impl SpinlockValid for Spinlock<24>

Source§

impl SpinlockValid for Spinlock<25>

Source§

impl SpinlockValid for Spinlock<26>

Source§

impl SpinlockValid for Spinlock<27>

Source§

impl SpinlockValid for Spinlock<28>

Source§

impl SpinlockValid for Spinlock<29>

Source§

impl SpinlockValid for Spinlock<3>

Source§

impl SpinlockValid for Spinlock<30>

Source§

impl SpinlockValid for Spinlock<31>

Source§

impl SpinlockValid for Spinlock<4>

Source§

impl SpinlockValid for Spinlock<5>

Source§

impl SpinlockValid for Spinlock<6>

Source§

impl SpinlockValid for Spinlock<7>

Source§

impl SpinlockValid for Spinlock<8>

Source§

impl SpinlockValid for Spinlock<9>

Auto Trait Implementations§

§

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

§

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

Source§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Is for T
where T: Sealed + Borrow<T> + BorrowMut<T>,

Source§

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

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

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.