diff --git a/Cargo.lock.msrv b/Cargo.lock.msrv index 9edd508..1e484a6 100644 --- a/Cargo.lock.msrv +++ b/Cargo.lock.msrv @@ -4,18 +4,18 @@ version = 3 [[package]] name = "aho-corasick" -version = "0.7.19" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e" +checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04" dependencies = [ "memchr", ] [[package]] name = "atomic-polyfill" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d299f547288d6db8d5c3a2916f7b2f66134b15b8c1ac1c4357dd3b8752af7bb2" +checksum = "c314e70d181aa6053b26e3f7fbf86d1dfff84f816a6175b967666b3506ef7289" dependencies = [ "critical-section", ] @@ -38,27 +38,11 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6548a0ad5d2549e111e1f6a11a6c2e2d00ce6a3dafe22948d67c2b443f775e52" -[[package]] -name = "crossbeam-utils" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51887d4adc7b564537b15adcfb307936f8075dfcd5f00dde9a9f1d29383682bc" -dependencies = [ - "cfg-if", - "once_cell 1.14.0", -] - -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - [[package]] name = "libc" -version = "0.2.132" +version = "0.2.144" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" +checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" [[package]] name = "memchr" @@ -66,29 +50,21 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" -[[package]] -name = "once_cell" -version = "1.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0" - [[package]] name = "once_cell" version = "1.17.2" dependencies = [ "atomic-polyfill", "critical-section", - "crossbeam-utils", - "lazy_static", "parking_lot_core", "regex", ] [[package]] name = "parking_lot_core" -version = "0.9.3" +version = "0.9.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" +checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" dependencies = [ "cfg-if", "libc", @@ -108,9 +84,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.6.0" +version = "1.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +checksum = "81ca098a9821bd52d6b24fd8b10bd081f47d39c22778cafaa75a2857a62c6390" dependencies = [ "aho-corasick", "memchr", @@ -119,73 +95,96 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.27" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" +checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" [[package]] name = "smallvec" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "windows-sys" -version = "0.36.1" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" dependencies = [ + "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", + "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_msvc" -version = "0.36.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" [[package]] name = "windows_i686_gnu" -version = "0.36.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" [[package]] name = "windows_i686_msvc" -version = "0.36.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" [[package]] name = "windows_x86_64_gnu" -version = "0.36.1" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" [[package]] name = "windows_x86_64_msvc" -version = "0.36.1" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" [[package]] name = "xshell" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d47097dc5c85234b1e41851b3422dd6d19b3befdd35b4ae5ce386724aeca981" +checksum = "962c039b3a7b16cf4e9a4248397c6585c07547412e7d6a6e035389a802dcfe90" dependencies = [ "xshell-macros", ] [[package]] name = "xshell-macros" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88301b56c26dd9bf5c43d858538f82d6f3f7764767defbc5d34e59459901c41a" +checksum = "1dbabb1cbd15a1d6d12d9ed6b35cc6777d4af87ab3ba155ea37215f20beab80c" [[package]] name = "xtask" diff --git a/Cargo.toml b/Cargo.toml index eb87276..dd2ccbf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,6 @@ atomic-polyfill = { version = "1", optional = true } critical-section = { version = "1", optional = true } [dev-dependencies] -lazy_static = "1.0.0" -crossbeam-utils = "0.8.7" regex = "1.2.0" critical-section = { version = "1.1.1", features = ["std"] } @@ -67,10 +65,6 @@ required-features = ["std"] name = "bench_acquire" required-features = ["std"] -[[example]] -name = "bench_vs_lazy_static" -required-features = ["std"] - [[example]] name = "lazy_static" required-features = ["std"] diff --git a/examples/bench_vs_lazy_static.rs b/examples/bench_vs_lazy_static.rs deleted file mode 100644 index c23b012..0000000 --- a/examples/bench_vs_lazy_static.rs +++ /dev/null @@ -1,51 +0,0 @@ -use lazy_static::lazy_static; -use once_cell::sync::Lazy; - -const N_THREADS: usize = 32; -const N_ROUNDS: usize = 100_000_000; - -static ONCE_CELL: Lazy> = Lazy::new(|| vec!["Spica".to_string(), "Hoyten".to_string()]); - -lazy_static! { - static ref LAZY_STATIC: Vec = vec!["Spica".to_string(), "Hoyten".to_string()]; -} - -fn main() { - let once_cell = { - let start = std::time::Instant::now(); - let threads = (0..N_THREADS) - .map(|_| std::thread::spawn(move || thread_once_cell())) - .collect::>(); - for thread in threads { - thread.join().unwrap(); - } - start.elapsed() - }; - let lazy_static = { - let start = std::time::Instant::now(); - let threads = (0..N_THREADS) - .map(|_| std::thread::spawn(move || thread_lazy_static())) - .collect::>(); - for thread in threads { - thread.join().unwrap(); - } - start.elapsed() - }; - - println!("once_cell: {:?}", once_cell); - println!("lazy_static: {:?}", lazy_static); -} - -fn thread_once_cell() { - for _ in 0..N_ROUNDS { - let len = ONCE_CELL.len(); - assert_eq!(len, 2) - } -} - -fn thread_lazy_static() { - for _ in 0..N_ROUNDS { - let len = LAZY_STATIC.len(); - assert_eq!(len, 2) - } -} diff --git a/tests/it/race.rs b/tests/it/race.rs index 6c5e11d..24884dd 100644 --- a/tests/it/race.rs +++ b/tests/it/race.rs @@ -3,10 +3,9 @@ use std::sync::Barrier; use std::{ num::NonZeroUsize, sync::atomic::{AtomicUsize, Ordering::SeqCst}, + thread::scope, }; -use crossbeam_utils::thread::scope; - use once_cell::race::{OnceBool, OnceNonZeroUsize}; #[test] @@ -15,7 +14,7 @@ fn once_non_zero_usize_smoke_test() { let cell = OnceNonZeroUsize::new(); let val = NonZeroUsize::new(92).unwrap(); scope(|s| { - s.spawn(|_| { + s.spawn(|| { assert_eq!( cell.get_or_init(|| { cnt.fetch_add(1, SeqCst); @@ -34,8 +33,7 @@ fn once_non_zero_usize_smoke_test() { ); assert_eq!(cnt.load(SeqCst), 1); }); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(val)); assert_eq!(cnt.load(SeqCst), 1); } @@ -66,7 +64,7 @@ fn once_non_zero_usize_first_wins() { let b2 = Barrier::new(2); let b3 = Barrier::new(2); scope(|s| { - s.spawn(|_| { + s.spawn(|| { let r1 = cell.get_or_init(|| { b1.wait(); b2.wait(); @@ -76,7 +74,7 @@ fn once_non_zero_usize_first_wins() { b3.wait(); }); b1.wait(); - s.spawn(|_| { + s.spawn(|| { let r2 = cell.get_or_init(|| { b2.wait(); b3.wait(); @@ -84,8 +82,7 @@ fn once_non_zero_usize_first_wins() { }); assert_eq!(r2, val1); }); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(val1)); } @@ -95,7 +92,7 @@ fn once_bool_smoke_test() { let cnt = AtomicUsize::new(0); let cell = OnceBool::new(); scope(|s| { - s.spawn(|_| { + s.spawn(|| { assert_eq!( cell.get_or_init(|| { cnt.fetch_add(1, SeqCst); @@ -114,8 +111,7 @@ fn once_bool_smoke_test() { ); assert_eq!(cnt.load(SeqCst), 1); }); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(false)); assert_eq!(cnt.load(SeqCst), 1); } diff --git a/tests/it/race_once_box.rs b/tests/it/race_once_box.rs index 2c21b76..0bf3852 100644 --- a/tests/it/race_once_box.rs +++ b/tests/it/race_once_box.rs @@ -5,9 +5,6 @@ use std::sync::{ Arc, }; -#[cfg(feature = "std")] -use crossbeam_utils::thread::scope; - use once_cell::race::OnceBox; #[derive(Default)] @@ -40,13 +37,15 @@ impl Heap { #[cfg(feature = "std")] #[test] fn once_box_smoke_test() { + use std::thread::scope; + let heap = Heap::default(); let global_cnt = AtomicUsize::new(0); let cell = OnceBox::new(); let b = Barrier::new(128); scope(|s| { for _ in 0..128 { - s.spawn(|_| { + s.spawn(|| { let local_cnt = AtomicUsize::new(0); cell.get_or_init(|| { global_cnt.fetch_add(1, SeqCst); @@ -64,8 +63,7 @@ fn once_box_smoke_test() { assert_eq!(local_cnt.load(SeqCst), 1); }); } - }) - .unwrap(); + }); assert!(cell.get().is_some()); assert!(global_cnt.load(SeqCst) > 10); @@ -95,6 +93,8 @@ fn once_box_set() { #[cfg(feature = "std")] #[test] fn once_box_first_wins() { + use std::thread::scope; + let cell = OnceBox::new(); let val1 = 92; let val2 = 62; @@ -103,7 +103,7 @@ fn once_box_first_wins() { let b2 = Barrier::new(2); let b3 = Barrier::new(2); scope(|s| { - s.spawn(|_| { + s.spawn(|| { let r1 = cell.get_or_init(|| { b1.wait(); b2.wait(); @@ -113,7 +113,7 @@ fn once_box_first_wins() { b3.wait(); }); b1.wait(); - s.spawn(|_| { + s.spawn(|| { let r2 = cell.get_or_init(|| { b2.wait(); b3.wait(); @@ -121,8 +121,7 @@ fn once_box_first_wins() { }); assert_eq!(*r2, val1); }); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(&val1)); } diff --git a/tests/it/sync_lazy.rs b/tests/it/sync_lazy.rs index f0b5768..44d70fa 100644 --- a/tests/it/sync_lazy.rs +++ b/tests/it/sync_lazy.rs @@ -1,10 +1,9 @@ use std::{ cell::Cell, sync::atomic::{AtomicUsize, Ordering::SeqCst}, + thread::scope, }; -use crossbeam_utils::thread::scope; - use once_cell::sync::{Lazy, OnceCell}; #[test] @@ -18,13 +17,12 @@ fn lazy_new() { assert_eq!(called.load(SeqCst), 0); scope(|s| { - s.spawn(|_| { + s.spawn(|| { let y = *x - 30; assert_eq!(y, 62); assert_eq!(called.load(SeqCst), 1); }); - }) - .unwrap(); + }); let y = *x - 30; assert_eq!(y, 62); @@ -120,11 +118,10 @@ fn static_lazy() { xs }); scope(|s| { - s.spawn(|_| { + s.spawn(|| { assert_eq!(&*XS, &vec![1, 2, 3]); }); - }) - .unwrap(); + }); assert_eq!(&*XS, &vec![1, 2, 3]); } diff --git a/tests/it/sync_once_cell.rs b/tests/it/sync_once_cell.rs index 9205e6a..252adea 100644 --- a/tests/it/sync_once_cell.rs +++ b/tests/it/sync_once_cell.rs @@ -1,4 +1,7 @@ -use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; +use std::{ + sync::atomic::{AtomicUsize, Ordering::SeqCst}, + thread::scope, +}; #[cfg(feature = "std")] use std::sync::Barrier; @@ -6,8 +9,6 @@ use std::sync::Barrier; #[cfg(not(feature = "std"))] use core::cell::Cell; -use crossbeam_utils::thread::scope; - use once_cell::sync::{Lazy, OnceCell}; #[test] @@ -15,12 +16,11 @@ fn once_cell() { let c = OnceCell::new(); assert!(c.get().is_none()); scope(|s| { - s.spawn(|_| { + s.spawn(|| { c.get_or_init(|| 92); assert_eq!(c.get(), Some(&92)); }); - }) - .unwrap(); + }); c.get_or_init(|| panic!("Kabom!")); assert_eq!(c.get(), Some(&92)); } @@ -61,13 +61,12 @@ fn once_cell_drop() { let x = OnceCell::new(); scope(|s| { - s.spawn(|_| { + s.spawn(|| { x.get_or_init(|| Dropper); assert_eq!(DROP_CNT.load(SeqCst), 0); drop(x); }); - }) - .unwrap(); + }); assert_eq!(DROP_CNT.load(SeqCst), 1); } @@ -108,11 +107,10 @@ fn get_or_try_init() { fn wait() { let cell: OnceCell = OnceCell::new(); scope(|s| { - s.spawn(|_| cell.set("hello".to_string())); + s.spawn(|| cell.set("hello".to_string())); let greeting = cell.wait(); assert_eq!(greeting, "hello") - }) - .unwrap(); + }); } #[cfg(feature = "std")] @@ -126,7 +124,7 @@ fn get_or_init_stress() { scope(|s| { for t in 0..n_threads { let cells = &cells; - s.spawn(move |_| { + s.spawn(move || { for (i, (b, s)) in cells.iter().enumerate() { b.wait(); let j = if t % 2 == 0 { s.wait() } else { s.get_or_init(|| i) }; @@ -134,8 +132,7 @@ fn get_or_init_stress() { } }); } - }) - .unwrap(); + }); } #[test] @@ -260,7 +257,7 @@ fn once_cell_does_not_leak_partially_constructed_boxes() { let cell: OnceCell = OnceCell::new(); scope(|scope| { for _ in 0..n_readers { - scope.spawn(|_| loop { + scope.spawn(|| loop { if let Some(msg) = cell.get() { assert_eq!(msg, MSG); break; @@ -268,10 +265,9 @@ fn once_cell_does_not_leak_partially_constructed_boxes() { }); } for _ in 0..n_writers { - let _ = scope.spawn(|_| cell.set(MSG.to_owned())); + let _ = scope.spawn(|| cell.set(MSG.to_owned())); } - }) - .unwrap() + }); } } @@ -281,7 +277,7 @@ fn get_does_not_block() { let cell = OnceCell::new(); let barrier = Barrier::new(2); scope(|scope| { - scope.spawn(|_| { + scope.spawn(|| { cell.get_or_init(|| { barrier.wait(); barrier.wait(); @@ -291,8 +287,7 @@ fn get_does_not_block() { barrier.wait(); assert_eq!(cell.get(), None); barrier.wait(); - }) - .unwrap(); + }); assert_eq!(cell.get(), Some(&"hello".to_string())); }