diff options
| author | joboet <jonasboettiger@icloud.com> | 2024-07-12 13:17:43 +0200 |
|---|---|---|
| committer | joboet <jonasboettiger@icloud.com> | 2024-07-31 17:44:10 +0200 |
| commit | 1d49aad8445881ac060cfae0e351207c05f82dfd (patch) | |
| tree | a9e70337c173b5423c56b599c19fc68fb8047cff /library/std/src/sync/once | |
| parent | cf11f499b37e557e41f5eeac157c623d08e0068d (diff) | |
std: fix busy-waiting in `Once::wait_force`, add more tests
Diffstat (limited to 'library/std/src/sync/once')
| -rw-r--r-- | library/std/src/sync/once/tests.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/library/std/src/sync/once/tests.rs b/library/std/src/sync/once/tests.rs index d43dabc1cf1..ce96468aeb6 100644 --- a/library/std/src/sync/once/tests.rs +++ b/library/std/src/sync/once/tests.rs @@ -1,5 +1,8 @@ use super::Once; +use crate::sync::atomic::AtomicBool; +use crate::sync::atomic::Ordering::Relaxed; use crate::sync::mpsc::channel; +use crate::time::Duration; use crate::{panic, thread}; #[test] @@ -113,3 +116,47 @@ fn wait_for_force_to_finish() { assert!(t1.join().is_ok()); assert!(t2.join().is_ok()); } + +#[test] +fn wait() { + for _ in 0..50 { + let val = AtomicBool::new(false); + let once = Once::new(); + + thread::scope(|s| { + for _ in 0..4 { + s.spawn(|| { + once.wait(); + assert!(val.load(Relaxed)); + }); + } + + once.call_once(|| val.store(true, Relaxed)); + }); + } +} + +#[test] +fn wait_on_poisoned() { + let once = Once::new(); + + panic::catch_unwind(|| once.call_once(|| panic!())).unwrap_err(); + panic::catch_unwind(|| once.wait()).unwrap_err(); +} + +#[test] +fn wait_force_on_poisoned() { + let once = Once::new(); + + thread::scope(|s| { + panic::catch_unwind(|| once.call_once(|| panic!())).unwrap_err(); + + s.spawn(|| { + thread::sleep(Duration::from_millis(100)); + + once.call_once_force(|_| {}); + }); + + once.wait_force(); + }) +} |
