Skip to content

Commit

Permalink
feat: add RwSpinlock readers-writer lock
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
  • Loading branch information
mkroening committed Oct 17, 2023
1 parent 3f995c6 commit c056c67
Show file tree
Hide file tree
Showing 3 changed files with 535 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -15,6 +15,9 @@ owning_ref = ["lock_api/owning_ref"]
[dependencies]
lock_api = "0.4.7"

[dev-dependencies]
rand = "0.8"

[package.metadata.release]
dev-version = false
pre-release-replacements = [
Expand Down
40 changes: 36 additions & 4 deletions src/lib.rs
@@ -1,10 +1,12 @@
//! Provides a simple spinlock based on the abstractions provided by the [`lock_api`] crate.
//! Provides simple spinlocks based on the abstractions provided by the [`lock_api`] crate.
//!
//! [`lock_api`]: https://docs.rs/lock_api/
//!
//! ## Usage Example
//! # Examples
//!
//! ```rust
//! Use [`Spinlock`] for mutual exclusion:
//!
//! ```
//! use spinning_top::Spinlock;
//!
//! fn main() {
Expand All @@ -24,22 +26,52 @@
//! // the guard automatically frees the lock at the end of the scope
//! }
//! ```
//!
//! Use [`RwSpinlock`] if you need a readers-writer lock:
//!
//! ```
//! use spinning_top::RwSpinlock;
//!
//! let lock = RwSpinlock::new(5);
//!
//! // many reader locks can be held at once
//! {
//! let r1 = lock.read();
//! let r2 = lock.read();
//! assert_eq!(*r1, 5);
//! assert_eq!(*r2, 5);
//! } // read locks are dropped at this point
//!
//! // only one write lock may be held, however
//! {
//! let mut w = lock.write();
//! *w += 1;
//! assert_eq!(*w, 6);
//! } // write lock is dropped here
//! ```

#![no_std]
#![cfg_attr(not(test), no_std)]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]

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

pub use rw_spinlock::{BackoffRwSpinlock, RawRwSpinlock, RwSpinlock};
pub use spinlock::{BackoffSpinlock, RawSpinlock, Spinlock};

/// Type aliases for guards.
pub mod guard {
pub use super::rw_spinlock::{
BackoffRwSpinlockReadGuard, BackoffRwSpinlockUpgradableReadGuard,
BackoffRwSpinlockWriteGuard, RwSpinlockReadGuard, RwSpinlockUpgradableReadGuard,
RwSpinlockWriteGuard,
};
pub use super::spinlock::{
BackoffSpinlockGuard, MappedBackoffSpinlockGuard, MappedSpinlockGuard, SpinlockGuard,
};
}

pub mod relax;
mod rw_spinlock;
mod spinlock;

0 comments on commit c056c67

Please sign in to comment.