Skip to content

Commit

Permalink
feat: add arc_lock feature and typedefs
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Oct 18, 2023
1 parent 9d803fe commit 38e4a5c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository = "https://github.com/rust-osdev/spinning_top"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
arc_lock = ["lock_api/arc_lock"]
owning_ref = ["lock_api/owning_ref"]

[dependencies]
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,19 @@ pub use spinlock::{BackoffSpinlock, RawSpinlock, Spinlock};

/// Type aliases for guards.
pub mod guard {
#[cfg(feature = "arc_lock")]
pub use super::rw_spinlock::{
ArcBackoffRwSpinlockReadGuard, ArcBackoffRwSpinlockUpgradableReadGuard,
ArcBackoffRwSpinlockWriteGuard, ArcRwSpinlockReadGuard, ArcRwSpinlockUpgradableReadGuard,
ArcRwSpinlockWriteGuard,
};
pub use super::rw_spinlock::{
BackoffRwSpinlockReadGuard, BackoffRwSpinlockUpgradableReadGuard,
BackoffRwSpinlockWriteGuard, RwSpinlockReadGuard, RwSpinlockUpgradableReadGuard,
RwSpinlockWriteGuard,
};
#[cfg(feature = "arc_lock")]
pub use super::spinlock::{ArcBackoffSpinlockGuard, ArcSpinlockGuard};
pub use super::spinlock::{
BackoffSpinlockGuard, MappedBackoffSpinlockGuard, MappedSpinlockGuard, SpinlockGuard,
};
Expand Down
27 changes: 27 additions & 0 deletions src/rw_spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ pub type RwSpinlockUpgradableReadGuard<'a, T> =
/// A [`lock_api::RwLockWriteGuard`] based on [`RawRwSpinlock`].
pub type RwSpinlockWriteGuard<'a, T> = lock_api::RwLockWriteGuard<'a, RawRwSpinlock<Spin>, T>;

/// A [`lock_api::ArcRwLockReadGuard`] based on [`RawRwSpinlock`].
#[cfg(feature = "arc_lock")]
pub type ArcRwSpinlockReadGuard<T> = lock_api::ArcRwLockReadGuard<RawRwSpinlock<Spin>, T>;

/// A [`lock_api::ArcRwLockUpgradableReadGuard`] based on [`RawRwSpinlock`].
#[cfg(feature = "arc_lock")]
pub type ArcRwSpinlockUpgradableReadGuard<T> =
lock_api::ArcRwLockUpgradableReadGuard<RawRwSpinlock<Spin>, T>;

/// A [`lock_api::ArcRwLockWriteGuard`] based on [`RawRwSpinlock`].
#[cfg(feature = "arc_lock")]
pub type ArcRwSpinlockWriteGuard<T> = lock_api::ArcRwLockWriteGuard<RawRwSpinlock<Spin>, T>;

/// A [`lock_api::RwLock`] based on [`RawRwSpinlock`]`<`[`Backoff`]`>`.
pub type BackoffRwSpinlock<T> = lock_api::RwLock<RawRwSpinlock<Backoff>, T>;

Expand All @@ -247,6 +260,20 @@ pub type BackoffRwSpinlockUpgradableReadGuard<'a, T> =
pub type BackoffRwSpinlockWriteGuard<'a, T> =
lock_api::RwLockWriteGuard<'a, RawRwSpinlock<Backoff>, T>;

/// A [`lock_api::ArcRwLockReadGuard`] based on [`RawRwSpinlock`]`<`[`Backoff`]`>`.
#[cfg(feature = "arc_lock")]
pub type ArcBackoffRwSpinlockReadGuard<T> = lock_api::ArcRwLockReadGuard<RawRwSpinlock<Backoff>, T>;

/// A [`lock_api::ArcRwLockUpgradableReadGuard`] based on [`RawRwSpinlock`]`<`[`Backoff`]`>`.
#[cfg(feature = "arc_lock")]
pub type ArcBackoffRwSpinlockUpgradableReadGuard<T> =
lock_api::ArcRwLockUpgradableReadGuard<RawRwSpinlock<Backoff>, T>;

/// A [`lock_api::ArcRwLockWriteGuard`] based on [`RawRwSpinlock`]`<`[`Backoff`]`>`.
#[cfg(feature = "arc_lock")]
pub type ArcBackoffRwSpinlockWriteGuard<T> =
lock_api::ArcRwLockWriteGuard<RawRwSpinlock<Backoff>, T>;

// Adapted from `spin::rwlock`.
#[cfg(test)]
mod tests {
Expand Down
8 changes: 8 additions & 0 deletions src/spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ pub type SpinlockGuard<'a, T> = lock_api::MutexGuard<'a, RawSpinlock<Spin>, T>;
/// ```
pub type MappedSpinlockGuard<'a, T> = lock_api::MappedMutexGuard<'a, RawSpinlock<Spin>, T>;

/// A [`lock_api::ArcMutexGuard`] based on [`RawSpinlock`]`.
#[cfg(feature = "arc_lock")]
pub type ArcSpinlockGuard<T> = lock_api::ArcMutexGuard<RawSpinlock<Spin>, T>;

/// A mutual exclusion (Mutex) type based on busy-waiting with exponential backoff.
///
/// Calling `lock` (or `try_lock`) on this type returns a [`BackoffSpinlockGuard`], which
Expand Down Expand Up @@ -310,6 +314,10 @@ pub type BackoffSpinlockGuard<'a, T> = lock_api::MutexGuard<'a, RawSpinlock<Back
pub type MappedBackoffSpinlockGuard<'a, T> =
lock_api::MappedMutexGuard<'a, RawSpinlock<Backoff>, T>;

/// A [`lock_api::ArcMutexGuard`] based on [`RawSpinlock`]`<`[`Backoff`]`>`.
#[cfg(feature = "arc_lock")]
pub type ArcBackoffSpinlockGuard<T> = lock_api::ArcMutexGuard<RawSpinlock<Backoff>, T>;

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 38e4a5c

Please sign in to comment.