Skip to content

Commit 43dae1e

Browse files
committed
feat: add RwSpinlock readers-writer lock
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
1 parent 3f995c6 commit 43dae1e

File tree

3 files changed

+511
-4
lines changed

3 files changed

+511
-4
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ owning_ref = ["lock_api/owning_ref"]
1515
[dependencies]
1616
lock_api = "0.4.7"
1717

18+
[dev-dependencies]
19+
rand = "0.8"
20+
1821
[package.metadata.release]
1922
dev-version = false
2023
pre-release-replacements = [

src/lib.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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.
22
//!
33
//! [`lock_api`]: https://docs.rs/lock_api/
44
//!
5-
//! ## Usage Example
5+
//! # Examples
66
//!
7-
//! ```rust
7+
//! Use [`Spinlock`] for mutual exclusion:
8+
//!
9+
//! ```
810
//! use spinning_top::Spinlock;
911
//!
1012
//! fn main() {
@@ -24,22 +26,52 @@
2426
//! // the guard automatically frees the lock at the end of the scope
2527
//! }
2628
//! ```
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+
//! ```
2752
28-
#![no_std]
53+
#![cfg_attr(not(test), no_std)]
2954
#![warn(missing_docs)]
3055
#![warn(missing_debug_implementations)]
3156

3257
/// The spinlock implemenation is based on the abstractions provided by the `lock_api` crate.
3358
pub use lock_api;
3459

60+
pub use rw_spinlock::{BackoffRwSpinlock, RawRwSpinlock, RwSpinlock};
3561
pub use spinlock::{BackoffSpinlock, RawSpinlock, Spinlock};
3662

3763
/// Type aliases for guards.
3864
pub mod guard {
65+
pub use super::rw_spinlock::{
66+
BackoffRwSpinlockReadGuard, BackoffRwSpinlockUpgradableReadGuard,
67+
BackoffRwSpinlockWriteGuard, RwSpinlockReadGuard, RwSpinlockUpgradableReadGuard,
68+
RwSpinlockWriteGuard,
69+
};
3970
pub use super::spinlock::{
4071
BackoffSpinlockGuard, MappedBackoffSpinlockGuard, MappedSpinlockGuard, SpinlockGuard,
4172
};
4273
}
4374

4475
pub mod relax;
76+
mod rw_spinlock;
4577
mod spinlock;

0 commit comments

Comments
 (0)