Skip to content

feat: add arc_lock feature and typedefs #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ license = "MIT/Apache-2.0"
description = "A simple spinlock crate based on the abstractions provided by `lock_api`."
repository = "https://github.com/rust-osdev/spinning_top"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs", "--generate-link-to-definition"]

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

[dependencies]
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
//! ```

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

Expand All @@ -62,11 +63,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