Skip to content

Commit

Permalink
test: improve relax testing
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromfedricci committed Sep 29, 2024
1 parent d7c19e1 commit ee20977
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .config/nextest.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[profile.default-miri]
test-threads = "num-cpus"
slow-timeout = { period = "60s", terminate-after = 2 }
slow-timeout = { period = "120s", terminate-after = 2 }
41 changes: 41 additions & 0 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,47 @@ pub mod cell {
}
}

pub mod debug_abort {
#[cfg(all(test, panic = "unwind"))]
use std::panic::Location;

/// Runs the closure, aborting the process if a unwinding panic occurs.
#[cfg(all(test, panic = "unwind"))]
#[track_caller]
pub fn on_unwind<T, F: FnOnce() -> T>(may_unwind: F) -> T {
let location = Location::caller();
let abort = DebugAbort { location };
let value = may_unwind();
core::mem::forget(abort);
value
}

/// Runs the closure, without aborting the process if a unwinding panic
/// occurs.
#[cfg(not(all(test, panic = "unwind")))]
#[cfg(not(tarpaulin_include))]
pub fn on_unwind<T, F: FnOnce() -> T>(may_unwind: F) -> T {
may_unwind()
}

/// A test only type that will abort the program execution once dropped.
///
/// To avoid aborting the proccess, callers must `forget` all instances of
/// the `Abort` type.
#[cfg(all(test, panic = "unwind"))]
struct DebugAbort {
location: &'static Location<'static>,
}

#[cfg(all(test, panic = "unwind"))]
#[cfg(not(tarpaulin_include))]
impl Drop for DebugAbort {
fn drop(&mut self) {
panic!("thread exits are forbidden inside {:?}, aborting", self.location);
}
}
}

pub mod hint {
#[cfg(not(all(loom, test)))]
pub use core::hint::spin_loop;
Expand Down
12 changes: 9 additions & 3 deletions src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ pub trait Lock {

/// The waiting policy that should be applied while the lock state has not
/// reached some target state.
pub trait Wait: Relax {}
pub trait Wait {
/// The relax operation that will be excuted during lock waiting loops.
type LockRelax: Relax;

/// The relax operation that will be excuted during unlock waiting loops.
type UnlockRelax: Relax;
}

impl Lock for AtomicBool {
#[cfg(not(all(loom, test)))]
Expand All @@ -73,9 +79,9 @@ impl Lock for AtomicBool {
fn lock_wait_relaxed<W: Wait>(&self) {
// Block the thread with a relaxed loop until the load returns `false`,
// indicating that the lock was handed off to the current thread.
let mut relaxed_waiter = W::new();
let mut relax = W::LockRelax::new();
while self.load(Relaxed) {
relaxed_waiter.relax();
relax.relax();
}
}

Expand Down
Loading

0 comments on commit ee20977

Please sign in to comment.