Skip to content

Commit

Permalink
fix loom tests not compiling with no-default-features
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Jul 27, 2024
1 parent 146be31 commit ea65e8e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion maitake-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![cfg_attr(feature = "core-error", feature(error_in_core))]
#![warn(missing_docs, missing_debug_implementations)]

#[cfg(feature = "alloc")]
#[cfg(any(feature = "alloc", test))]
extern crate alloc;

pub(crate) mod loom;
Expand Down
20 changes: 18 additions & 2 deletions maitake-sync/src/loom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ mod inner {
use core::{
marker::PhantomData,
ops::{Deref, DerefMut},
panic::Location,
};

use alloc::fmt;
#[cfg(feature = "tracing")]
use core::panic::Location;

use core::fmt;

/// Mock version of mycelium's spinlock, but using
/// `loom::sync::Mutex`. The API is slightly different, since the
Expand All @@ -91,6 +93,7 @@ mod inner {

pub(crate) struct MutexGuard<'a, T, Lock = crate::spin::Spinlock> {
guard: loom::sync::MutexGuard<'a, T>,
#[cfg(feature = "tracing")]
location: &'static Location<'static>,
_p: PhantomData<Lock>,
}
Expand All @@ -115,19 +118,25 @@ mod inner {

#[track_caller]
pub fn try_lock(&self) -> Option<MutexGuard<'_, T, Lock>> {
#[cfg(feature = "tracing")]
let location = Location::caller();
#[cfg(feature = "tracing")]
tracing::debug!(%location, "Mutex::try_lock");

match self.0.try_lock() {
Ok(guard) => {
#[cfg(feature = "tracing")]
tracing::debug!(%location, "Mutex::try_lock -> locked!");
Some(MutexGuard {
guard,

#[cfg(feature = "tracing")]
location,
_p: PhantomData,
})
}
Err(_) => {
#[cfg(feature = "tracing")]
tracing::debug!(%location, "Mutex::try_lock -> already locked");
None
}
Expand All @@ -136,19 +145,25 @@ mod inner {

#[track_caller]
pub fn lock(&self) -> MutexGuard<'_, T, Lock> {
#[cfg(feature = "tracing")]
let location = Location::caller();

#[cfg(feature = "tracing")]
tracing::debug!(%location, "Mutex::lock");

let guard = self
.0
.lock()
.map(|guard| MutexGuard {
guard,

#[cfg(feature = "tracing")]
location,
_p: PhantomData,
})
.expect("loom mutex will never poison");

#[cfg(feature = "tracing")]
tracing::debug!(%location, "Mutex::lock -> locked");
guard
}
Expand Down Expand Up @@ -184,6 +199,7 @@ mod inner {
impl<T, Lock> Drop for MutexGuard<'_, T, Lock> {
#[track_caller]
fn drop(&mut self) {
#[cfg(feature = "tracing")]
tracing::debug!(
location.dropped = %Location::caller(),
location.locked = %self.location,
Expand Down
3 changes: 1 addition & 2 deletions maitake-sync/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use core::{
pin::Pin,
task::{Context, Poll},
};
use mutex_traits::ConstInit;
use pin_project::pin_project;
#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -202,7 +201,7 @@ impl<T> Mutex<T> {
#[cfg(not(loom))]
impl<T, L> Mutex<T, L>
where
L: ScopedRawMutex + ConstInit,
L: ScopedRawMutex + mutex_traits::ConstInit,
{
/// Returns a new `Mutex` protecting the provided `data`, using the provided
/// [`ScopedRawMutex`] implementation as the raw mutex.
Expand Down
1 change: 1 addition & 0 deletions maitake-sync/src/wait_map/tests/loom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn wake_two_sequential() {
}

#[test]
#[cfg(feature = "alloc")]
fn wake_close() {
use ::alloc::sync::Arc;

Expand Down
19 changes: 9 additions & 10 deletions maitake-sync/src/wait_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,16 +415,15 @@ where
}
}

loom_const_fn! {
/// Returns a new `WaitQueue` with a single stored wakeup.
///
/// The first call to [`wait`] on this queue will immediately succeed.
///
/// [`wait`]: Self::wait
#[must_use]
pub(crate) fn new_woken_with_raw_mutex() -> Self {
Self::make(State::Woken, Mutex::with_raw_mutex(List::new()))
}
/// Returns a new `WaitQueue` with a single stored wakeup.
///
/// The first call to [`wait`] on this queue will immediately succeed.
///
/// [`wait`]: Self::wait
#[must_use]
#[cfg(not(loom))]
pub(crate) const fn new_woken_with_raw_mutex() -> Self {
Self::make(State::Woken, Mutex::with_raw_mutex(List::new()))
}
}

Expand Down
2 changes: 2 additions & 0 deletions maitake-sync/src/wait_queue/tests/loom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fn wake_all_sequential() {
}

#[test]
#[cfg(feature = "alloc")]
fn wake_all_concurrent() {
use alloc::sync::Arc;
// must be higher than the number of threads in a `WakeBatch`, but below
Expand Down Expand Up @@ -108,6 +109,7 @@ fn wake_all_reregistering() {
}

#[test]
#[cfg(feature = "alloc")]
fn wake_close() {
use alloc::sync::Arc;

Expand Down

0 comments on commit ea65e8e

Please sign in to comment.