Skip to content

Implement const_spinlock convenience function #5

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 1 commit into from Jul 7, 2020
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
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! // because the spinlock ensures mutual exclusion
//! locked_data.make_ascii_uppercase();
//! assert_eq!(locked_data.as_str(), "HELLO");
//!
//!
//! // the guard automatically frees the lock at the end of the scope
//! }
//! ```
Expand All @@ -32,6 +32,6 @@
/// The spinlock implemenation is based on the abstractions provided by the `lock_api` crate.
pub use lock_api;

pub use spinlock::{RawSpinlock, Spinlock, SpinlockGuard};
pub use spinlock::{const_spinlock, RawSpinlock, Spinlock, SpinlockGuard};

mod spinlock;
13 changes: 13 additions & 0 deletions src/spinlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ pub type Spinlock<T> = lock_api::Mutex<RawSpinlock, T>;
/// assert!(spinlock.try_lock().is_some());
pub type SpinlockGuard<'a, T> = lock_api::MutexGuard<'a, RawSpinlock, T>;

/// Create an unlocked `Spinlock` in a `const` context.
///
/// ## Example
///
/// ```rust
/// use spinning_top::{const_spinlock, Spinlock};
///
/// static SPINLOCK: Spinlock<i32> = const_spinlock(42);
/// ```
pub const fn const_spinlock<T>(val: T) -> Spinlock<T> {
Spinlock::const_new(<RawSpinlock as lock_api::RawMutex>::INIT, val)
}

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