API providing simple to use locks:
Spinlock
: blocking lockSemaphore
: atomic lock counter blocking or non-blockingMutex
: blocking lock to ensure mutual exclusive to its interior.RWLock
: blocking lock to provide multiple immutable and exclusive mutable access to its interior.To use this crate simply add the dependency to your Cargo.toml
file:
toml
[dependencies]
ruspiro-lock = "0.4.2"
Once done the definition and usage of the locks is as follows. Keep in mind to share those locking primitives accross cores or threads they should be wrapped in an Arc
.
```rust use ruspiro_lock::Spinlock;
fn main() { let spin = Spinlock::new(); spin.aquire(); // following code is only executed if the lock could be aquired, the executing core pause till then let _ = 10 + 3; spin.release(); } ```
```rust use ruspiro_lock::Semaphore;
fn main() { let sema = Semaphore::new(1); if sema.trydown().isok() { // we gained access to the semaphore, do something let _ = 20 /4; sema.up(); } } ```
```rust use ruspiro_lock::Mutex;
fn main() { let mutex = Mutex::new(0u32); if let Some(mut data) = mutex.trylock() { *data = 20; } // once the data goes ot of scope the lock will be released if let Some(data) = mutex.trylock() { println!("data: {}", *data);
// another lock should fail inside this scope
assert!(mutex.try_lock().is_none());
}
// a blocking lock on the data will block the current execution until
// the lock get's available
let mut data = mutex.lock();
*data = 12;
} ```
```rust use ruspiro_lock::RWLock;
fn main() { let rwlock = Arc::new(RWLock::new(0u32)); let rwlockclone = Arc::clone(&rwlock); { // trylock and lock will provide a WriteLockGuard let mut data = rwlock.lock(); *data = 20; // if a write lock exists no other write or read lock's could be aquired assert!(rwlockclone.trylock().isnone()); assert!(rwlockclone.tryread().isnone()); } { // multiple read locks are possible let data = rwlock.read(); // if a write lock exists no other write or read lock's could be aquired assert!(rwlockclone.tryread().is_some()); println!("{}", *data); } } ```
Licensed under Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) or MIT (LICENSE-MIT or http://opensource.org/licenses/MIT)) at your choice.