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 hereNote: The critical-section implementation uses Spinlock 31.
Implementations§
Source§impl<const N: usize> Spinlock<N>where
Spinlock<N>: SpinlockValid,
impl<const N: usize> Spinlock<N>where
Spinlock<N>: SpinlockValid,
Sourcepub fn try_claim() -> Option<Spinlock<N>>
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.
Sourcepub fn claim() -> Spinlock<N>
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
Sourcepub fn claim_async() -> Result<Spinlock<N>, Error<Infallible>>
pub fn claim_async() -> Result<Spinlock<N>, Error<Infallible>>
Try to claim the spinlock. Will return WouldBlock until the spinlock is available.
Trait Implementations§
impl SpinlockValid for Spinlock<0>
impl SpinlockValid for Spinlock<1>
impl SpinlockValid for Spinlock<10>
impl SpinlockValid for Spinlock<11>
impl SpinlockValid for Spinlock<12>
impl SpinlockValid for Spinlock<13>
impl SpinlockValid for Spinlock<14>
impl SpinlockValid for Spinlock<15>
impl SpinlockValid for Spinlock<16>
impl SpinlockValid for Spinlock<17>
impl SpinlockValid for Spinlock<18>
impl SpinlockValid for Spinlock<19>
impl SpinlockValid for Spinlock<2>
impl SpinlockValid for Spinlock<20>
impl SpinlockValid for Spinlock<21>
impl SpinlockValid for Spinlock<22>
impl SpinlockValid for Spinlock<23>
impl SpinlockValid for Spinlock<24>
impl SpinlockValid for Spinlock<25>
impl SpinlockValid for Spinlock<26>
impl SpinlockValid for Spinlock<27>
impl SpinlockValid for Spinlock<28>
impl SpinlockValid for Spinlock<29>
impl SpinlockValid for Spinlock<3>
impl SpinlockValid for Spinlock<30>
impl SpinlockValid for Spinlock<31>
impl SpinlockValid for Spinlock<4>
impl SpinlockValid for Spinlock<5>
impl SpinlockValid for Spinlock<6>
impl SpinlockValid for Spinlock<7>
impl SpinlockValid for Spinlock<8>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
impl<Choices> CoproductSubsetter<CNil, HNil> for Choices
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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