1ef40d7f6Sopenharmony_ci//! Test if the OnceCell properly synchronizes.
2ef40d7f6Sopenharmony_ci//! Needs to be run in release mode.
3ef40d7f6Sopenharmony_ci//!
4ef40d7f6Sopenharmony_ci//! We create a `Vec` with `N_ROUNDS` of `OnceCell`s. All threads will walk the `Vec`, and race to
5ef40d7f6Sopenharmony_ci//! be the first one to initialize a cell.
6ef40d7f6Sopenharmony_ci//! Every thread adds the results of the cells it sees to an accumulator, which is compared at the
7ef40d7f6Sopenharmony_ci//! end.
8ef40d7f6Sopenharmony_ci//! All threads should end up with the same result.
9ef40d7f6Sopenharmony_ci
10ef40d7f6Sopenharmony_ciuse once_cell::sync::OnceCell;
11ef40d7f6Sopenharmony_ci
12ef40d7f6Sopenharmony_ciconst N_THREADS: usize = 32;
13ef40d7f6Sopenharmony_ciconst N_ROUNDS: usize = 1_000_000;
14ef40d7f6Sopenharmony_ci
15ef40d7f6Sopenharmony_cistatic CELLS: OnceCell<Vec<OnceCell<usize>>> = OnceCell::new();
16ef40d7f6Sopenharmony_cistatic RESULT: OnceCell<usize> = OnceCell::new();
17ef40d7f6Sopenharmony_ci
18ef40d7f6Sopenharmony_cifn main() {
19ef40d7f6Sopenharmony_ci    let start = std::time::Instant::now();
20ef40d7f6Sopenharmony_ci    CELLS.get_or_init(|| vec![OnceCell::new(); N_ROUNDS]);
21ef40d7f6Sopenharmony_ci    let threads =
22ef40d7f6Sopenharmony_ci        (0..N_THREADS).map(|i| std::thread::spawn(move || thread_main(i))).collect::<Vec<_>>();
23ef40d7f6Sopenharmony_ci    for thread in threads {
24ef40d7f6Sopenharmony_ci        thread.join().unwrap();
25ef40d7f6Sopenharmony_ci    }
26ef40d7f6Sopenharmony_ci    println!("{:?}", start.elapsed());
27ef40d7f6Sopenharmony_ci    println!("No races detected");
28ef40d7f6Sopenharmony_ci}
29ef40d7f6Sopenharmony_ci
30ef40d7f6Sopenharmony_cifn thread_main(i: usize) {
31ef40d7f6Sopenharmony_ci    let cells = CELLS.get().unwrap();
32ef40d7f6Sopenharmony_ci    let mut accum = 0;
33ef40d7f6Sopenharmony_ci    for cell in cells.iter() {
34ef40d7f6Sopenharmony_ci        let &value = cell.get_or_init(|| i);
35ef40d7f6Sopenharmony_ci        accum += value;
36ef40d7f6Sopenharmony_ci    }
37ef40d7f6Sopenharmony_ci    assert_eq!(RESULT.get_or_init(|| accum), &accum);
38ef40d7f6Sopenharmony_ci}
39