Skip to content

Commit b564c51

Browse files
authored
Rollup merge of #127447 - RalfJung:once_lock_miri, r=joboet
once_lock: make test not take as long in Miri Allocating 1000 list elements takes a while (`@zachs18` reported >5min), so let's reduce the iteration count when running in Miri. Unfortunately due to this clever `while let i @ 0..LEN =` thing, the count needs to be a constants, and constants cannot be shadowed, so we need to use another trick to hide the `cfg!(miri)` from the docs. (I think this loop condition may be a bit too clever, it took me a bit to decipher. Ideally this would be `while let i = ... && i < LEN`, but that is not stable yet.)
2 parents 1ee6345 + bee9120 commit b564c51

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

library/std/src/sync/once_lock.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,21 @@ use crate::sync::Once;
8080
/// static LIST: OnceList<u32> = OnceList::new();
8181
/// static COUNTER: AtomicU32 = AtomicU32::new(0);
8282
///
83-
/// let vec = (0..thread::available_parallelism().unwrap().get()).map(|_| thread::spawn(|| {
84-
/// while let i @ 0..=1000 = COUNTER.fetch_add(1, Ordering::Relaxed) {
85-
/// LIST.push(i);
83+
/// # const LEN: u32 = if cfg!(miri) { 50 } else { 1000 };
84+
/// # /*
85+
/// const LEN: u32 = 1000;
86+
/// # */
87+
/// thread::scope(|s| {
88+
/// for _ in 0..thread::available_parallelism().unwrap().get() {
89+
/// s.spawn(|| {
90+
/// while let i @ 0..LEN = COUNTER.fetch_add(1, Ordering::Relaxed) {
91+
/// LIST.push(i);
92+
/// }
93+
/// });
8694
/// }
87-
/// })).collect::<Vec<thread::JoinHandle<_>>>();
88-
/// vec.into_iter().for_each(|handle| handle.join().unwrap());
95+
/// });
8996
///
90-
/// for i in 0..=1000 {
97+
/// for i in 0..LEN {
9198
/// assert!(LIST.contains(&i));
9299
/// }
93100
///

0 commit comments

Comments
 (0)