|
1 |
| -//! Provides a simple spinlock based on the abstractions provided by the [`lock_api`] crate. |
| 1 | +//! Provides simple spinlocks based on the abstractions provided by the [`lock_api`] crate. |
2 | 2 | //!
|
3 | 3 | //! [`lock_api`]: https://docs.rs/lock_api/
|
4 | 4 | //!
|
5 |
| -//! ## Usage Example |
| 5 | +//! # Examples |
6 | 6 | //!
|
7 |
| -//! ```rust |
| 7 | +//! Use [`Spinlock`] for mutual exclusion: |
| 8 | +//! |
| 9 | +//! ``` |
8 | 10 | //! use spinning_top::Spinlock;
|
9 | 11 | //!
|
10 | 12 | //! fn main() {
|
|
24 | 26 | //! // the guard automatically frees the lock at the end of the scope
|
25 | 27 | //! }
|
26 | 28 | //! ```
|
| 29 | +//! |
| 30 | +//! Use [`RwSpinlock`] if you need a readers-writer lock: |
| 31 | +//! |
| 32 | +//! ``` |
| 33 | +//! use spinning_top::RwSpinlock; |
| 34 | +//! |
| 35 | +//! let lock = RwSpinlock::new(5); |
| 36 | +//! |
| 37 | +//! // many reader locks can be held at once |
| 38 | +//! { |
| 39 | +//! let r1 = lock.read(); |
| 40 | +//! let r2 = lock.read(); |
| 41 | +//! assert_eq!(*r1, 5); |
| 42 | +//! assert_eq!(*r2, 5); |
| 43 | +//! } // read locks are dropped at this point |
| 44 | +//! |
| 45 | +//! // only one write lock may be held, however |
| 46 | +//! { |
| 47 | +//! let mut w = lock.write(); |
| 48 | +//! *w += 1; |
| 49 | +//! assert_eq!(*w, 6); |
| 50 | +//! } // write lock is dropped here |
| 51 | +//! ``` |
27 | 52 |
|
28 |
| -#![no_std] |
| 53 | +#![cfg_attr(not(test), no_std)] |
29 | 54 | #![warn(missing_docs)]
|
30 | 55 | #![warn(missing_debug_implementations)]
|
31 | 56 |
|
32 | 57 | /// The spinlock implemenation is based on the abstractions provided by the `lock_api` crate.
|
33 | 58 | pub use lock_api;
|
34 | 59 |
|
| 60 | +pub use rw_spinlock::{BackoffRwSpinlock, RawRwSpinlock, RwSpinlock}; |
35 | 61 | pub use spinlock::{BackoffSpinlock, RawSpinlock, Spinlock};
|
36 | 62 |
|
37 | 63 | /// Type aliases for guards.
|
38 | 64 | pub mod guard {
|
| 65 | + pub use super::rw_spinlock::{ |
| 66 | + BackoffRwSpinlockReadGuard, BackoffRwSpinlockUpgradableReadGuard, |
| 67 | + BackoffRwSpinlockWriteGuard, RwSpinlockReadGuard, RwSpinlockUpgradableReadGuard, |
| 68 | + RwSpinlockWriteGuard, |
| 69 | + }; |
39 | 70 | pub use super::spinlock::{
|
40 | 71 | BackoffSpinlockGuard, MappedBackoffSpinlockGuard, MappedSpinlockGuard, SpinlockGuard,
|
41 | 72 | };
|
42 | 73 | }
|
43 | 74 |
|
44 | 75 | pub mod relax;
|
| 76 | +mod rw_spinlock; |
45 | 77 | mod spinlock;
|
0 commit comments