]>
git.proxmox.com Git - rustc.git/blob - library/std/src/sync/once/tests.rs
3 use crate::sync
::mpsc
::channel
;
8 static O
: Once
= Once
::new();
10 O
.call_once(|| a
+= 1);
12 O
.call_once(|| a
+= 1);
18 static O
: Once
= Once
::new();
19 static mut RUN
: bool
= false;
21 let (tx
, rx
) = channel();
24 thread
::spawn(move || {
54 static O
: Once
= Once
::new();
57 let t
= panic
::catch_unwind(|| {
58 O
.call_once(|| panic
!());
62 // poisoning propagates
63 let t
= panic
::catch_unwind(|| {
68 // we can subvert poisoning, however
69 let mut called
= false;
70 O
.call_once_force(|p
| {
72 assert
!(p
.is_poisoned())
76 // once any success happens, we stop propagating the poison
81 fn wait_for_force_to_finish() {
82 static O
: Once
= Once
::new();
85 let t
= panic
::catch_unwind(|| {
86 O
.call_once(|| panic
!());
90 // make sure someone's waiting inside the once via a force
91 let (tx1
, rx1
) = channel();
92 let (tx2
, rx2
) = channel();
93 let t1
= thread
::spawn(move || {
94 O
.call_once_force(|p
| {
95 assert
!(p
.is_poisoned());
96 tx1
.send(()).unwrap();
103 // put another waiter on the once
104 let t2
= thread
::spawn(|| {
105 let mut called
= false;
112 tx2
.send(()).unwrap();
114 assert
!(t1
.join().is_ok());
115 assert
!(t2
.join().is_ok());