From 08b37abff58ff2b92525585e8a91c5b2e2eee0c5 Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Sun, 12 Apr 2020 13:43:38 -0400 Subject: [PATCH] epoch: Make loom able to test treiber stack With this change, loom runs to completion on the treiber test! I ran it with `LOOM_MAX_PREEMPTIONS=3`, and it took _forever_ (well, it took 2h), but eventually finished with: ================== Iteration 112280000 ================== Completed in 112291514 iterations ok With `LOOM_MAX_PREEMPTIONS=2` it "only" took 13 minutes, and finished with: ================== Iteration 9680000 ================== Completed in 9690926 iterations ok I updated the CI script to run with `LOOM_MAX_PREEMPTIONS=2`. Note that this change depends on https://github.com/tokio-rs/loom/pull/125 and https://github.com/tokio-rs/loom/pull/128. The `patch` in the root `Cargo.toml` should be removed once a new loom release is made. --- Cargo.toml | 3 +++ ci/crossbeam-epoch-loom.sh | 2 +- crossbeam-epoch/src/default.rs | 2 +- crossbeam-epoch/src/lib.rs | 14 +++++++++++++- crossbeam-epoch/tests/loom.rs | 5 +++-- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 12352de6d..f5c3bdf9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,3 +74,6 @@ members = [ "crossbeam-skiplist", "crossbeam-utils", ] + +[patch.crates-io] +loom = { git = "https://github.com/jonhoo/loom.git", branch = "lazy_static-race" } diff --git a/ci/crossbeam-epoch-loom.sh b/ci/crossbeam-epoch-loom.sh index 1fc935b3a..9605929bc 100755 --- a/ci/crossbeam-epoch-loom.sh +++ b/ci/crossbeam-epoch-loom.sh @@ -5,4 +5,4 @@ set -ex export RUSTFLAGS="-D warnings" -env LOOM_MAX_PREEMPTIONS=3 cargo test --test loom --features with-loom --release -- --nocapture +env LOOM_MAX_PREEMPTIONS=2 cargo test --test loom --features with-loom --release -- --nocapture diff --git a/crossbeam-epoch/src/default.rs b/crossbeam-epoch/src/default.rs index fd1a8ced5..30478a287 100644 --- a/crossbeam-epoch/src/default.rs +++ b/crossbeam-epoch/src/default.rs @@ -7,7 +7,7 @@ use collector::{Collector, LocalHandle}; use guard::Guard; -lazy_static! { +::concurrency::lazy_static! { /// The global data for the default garbage collector. static ref COLLECTOR: Collector = Collector::new(); } diff --git a/crossbeam-epoch/src/lib.rs b/crossbeam-epoch/src/lib.rs index 867451fc1..582b4ea8c 100644 --- a/crossbeam-epoch/src/lib.rs +++ b/crossbeam-epoch/src/lib.rs @@ -92,6 +92,12 @@ pub(crate) mod concurrency { pub(crate) use loom::sync::Arc; } pub(crate) use loom::thread_local; + + cfg_if! { + if #[cfg(feature = "std")] { + pub(crate) use loom::lazy_static; + } + } } #[cfg(not(feature = "with-loom"))] pub(crate) mod concurrency { @@ -126,6 +132,13 @@ pub(crate) mod concurrency { pub(crate) use alloc::sync::Arc; } pub(crate) use std::thread_local; + pub(crate) use std::thread_local; + + cfg_if! { + if #[cfg(feature = "std")] { + pub(crate) use lazy_static::lazy_static; + } + } } cfg_if! { @@ -161,7 +174,6 @@ cfg_if! { cfg_if! { if #[cfg(feature = "std")] { - #[macro_use] extern crate lazy_static; mod default; diff --git a/crossbeam-epoch/tests/loom.rs b/crossbeam-epoch/tests/loom.rs index b7209568c..e0b6ae76a 100644 --- a/crossbeam-epoch/tests/loom.rs +++ b/crossbeam-epoch/tests/loom.rs @@ -127,14 +127,15 @@ fn treiber_stack() { let stack1 = Arc::new(TreiberStack::new()); let stack2 = Arc::clone(&stack1); + // use 5 since it's greater than the 4 used for the sanitize feature let jh = spawn(move || { - for i in 0..20 { + for i in 0..5 { stack2.push(i); assert!(stack2.pop().is_some()); } }); - for i in 0..20 { + for i in 0..5 { stack1.push(i); assert!(stack1.pop().is_some()); }