Simple to use abstractions on low level atomic locks:
- Spinlock
: blocking lock to secure cross core mutual exclusive access (requires a configured MMU on Raspberry Pi)
- Semaphore
: counting blocking or non-blocking lock to secure cross core exclusive access
- DataLock
: data container guarded by a non-blocking atomic lock to secure cross core mutual exclusive access
To use this crate simply add the dependency to your Cargo.toml
file:
[dependencies]
ruspiro-lock = "0.3"
Once done the definition and usage of the locks is as follows:
Using a Spinlock to ensure exclusive access: ``` use ruspiro_lock::*;
static SPIN: Spinlock = Spinlock::new();
fn main() { SPIN.aquire(); // following code is only executed if the lock could be aquired, the executing core pause till then let _ = 10 + 3; SPIN.release(); } ```
Using a Semaphore to specify how often specific access is valid: ``` use ruspriro_lock::*;
static mut SEMA: Semaphore = Semaphore::new(1);
fn main() { unsafe { // unsafe necessary as accessing mut static's is unsafe if SEMA.trydown().isok() { // we gained access to the semaphore, do something let _ = 20 /4; SEMA.up(); } } } ```
Using data container with atmic lock guard: ``` use ruspiro_lock::*;
static DATA: DataLock
fn main() { if let Some(mut data) = DATA.trylock() { *data = 20; } // once the data goes ot of scope the lock will be released if let Some(data) = DATA.trylock() { println!("data: {}", *data);
// another lock should fail inside this scope
assert_eq!(DATA.try_lock(), None);
}
} ```
Licensed under Apache License, Version 2.0, (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)