ruspiro_lock/lib.rs
1/***********************************************************************************************************************
2 * Copyright (c) 2020 by the authors
3 *
4 * Author: André Borrmann <pspwizard@gmx.de>
5 * License: Apache License 2.0 / MIT
6 **********************************************************************************************************************/
7#![doc(html_root_url = "https://docs.rs/ruspiro-lock/0.5.0")]
8#![cfg_attr(not(any(test, doctest)), no_std)]
9
10//! # Atomic locks for Raspberry Pi baremetal systems
11//!
12//! This crate provides different kinds of locks and access guards to secure data access across cores and/or threads.
13//! Those locks are *Spinlock*, *Semaphore*, *Mutex* and *RWLock*. The * Semaphore*, *Mutex* and *RWLock* are available
14//! as `async` version as well to allow them to work in a *non blocking* fashion as required by `async` code.
15//!
16//! ## Usage Hint:
17//! As the locks depend on low level atomics they do only work on the Raspberry Pi if the MMU is properly configured.
18//! Otherwise using either of the lock functions will hang the core it has been used on.
19//!
20//! ## Features
21//!
22//! Feature | Usage
23//! --------|--------
24//! async_locks | allows usage of the `async` lock versions.
25//!
26//!
27//! To share those locking primitives accross the Rasperry Pi cores they should be wrapped in an `Arc`.
28//!
29//! # Usage
30//!
31//! ## Spinlock
32//! ```
33//! use ruspiro_lock::sync::Spinlock;
34//!
35//! fn main() {
36//! let spin = Spinlock::new();
37//! spin.aquire();
38//! // following code is only executed if the lock could be aquired, the executing core pause till then
39//! let _ = 10 + 3;
40//! spin.release();
41//! }
42//! ```
43//!
44//! ## Semaphore
45//! ```
46//! use ruspiro_lock::sync::Semaphore;
47//!
48//! fn main() {
49//! let sema = Semaphore::new(1);
50//! if sema.try_down().is_ok() {
51//! // we gained access to the semaphore, do something
52//! let _ = 20 /4;
53//! sema.up();
54//! }
55//! }
56//! ```
57//!
58//! ## Mutex
59//! ```
60//! use ruspiro_lock::sync::Mutex;
61//!
62//! fn main() {
63//! let mutex = Mutex::new(0u32);
64//! if let Some(mut data) = mutex.try_lock() {
65//! *data = 20;
66//! }
67//! // once the data goes ot of scope the lock will be released
68//! if let Some(data) = mutex.try_lock() {
69//! println!("data: {}", *data);
70//!
71//! // another lock should fail inside this scope
72//! assert!(mutex.try_lock().is_none());
73//! }
74//!
75//! // a blocking lock on the data will block the current execution until the lock get's available
76//! let mut data = mutex.lock();
77//! *data = 12;
78//! }
79//! ```
80
81// re-export the sync lock types, always at root level and witin the sync module
82pub mod sync;
83pub use sync::*;
84
85#[cfg(any(feature = "async_locks", doc))]
86pub mod r#async;