1 use crate::sync::atomic::{AtomicBool, Ordering};
2 use crate::sync::mpsc::channel;
3 use crate::sync::{Arc, Condvar, Mutex};
5 use crate::time::Duration;
9 let c = Condvar::new();
15 #[cfg_attr(target_os = "emscripten", ignore)]
17 let m = Arc::new(Mutex::new(()));
19 let c = Arc::new(Condvar::new());
22 let g = m.lock().unwrap();
23 let _t = thread::spawn(move || {
24 let _g = m2.lock().unwrap();
27 let g = c.wait(g).unwrap();
32 #[cfg_attr(target_os = "emscripten", ignore)]
36 let data = Arc::new((Mutex::new(0), Condvar::new()));
37 let (tx, rx) = channel();
39 let data = data.clone();
41 thread::spawn(move || {
42 let &(ref lock, ref cond) = &*data;
43 let mut cnt = lock.lock().unwrap();
49 cnt = cond.wait(cnt).unwrap();
56 let &(ref lock, ref cond) = &*data;
58 let mut cnt = lock.lock().unwrap();
69 #[cfg_attr(target_os = "emscripten", ignore)]
71 let pair = Arc::new((Mutex::new(false), Condvar::new()));
72 let pair2 = pair.clone();
74 // Inside of our lock, spawn a new thread, and then wait for it to start.
75 thread::spawn(move || {
76 let &(ref lock, ref cvar) = &*pair2;
77 let mut started = lock.lock().unwrap();
79 // We notify the condvar that the value has changed.
83 // Wait for the thread to start up.
84 let &(ref lock, ref cvar) = &*pair;
85 let guard = cvar.wait_while(lock.lock().unwrap(), |started| !*started);
86 assert!(*guard.unwrap());
90 #[cfg_attr(target_os = "emscripten", ignore)]
91 fn wait_timeout_wait() {
92 let m = Arc::new(Mutex::new(()));
93 let c = Arc::new(Condvar::new());
96 let g = m.lock().unwrap();
97 let (_g, no_timeout) = c.wait_timeout(g, Duration::from_millis(1)).unwrap();
98 // spurious wakeups mean this isn't necessarily true
99 // so execute test again, if not timeout
100 if !no_timeout.timed_out() {
109 #[cfg_attr(target_os = "emscripten", ignore)]
110 fn wait_timeout_while_wait() {
111 let m = Arc::new(Mutex::new(()));
112 let c = Arc::new(Condvar::new());
114 let g = m.lock().unwrap();
115 let (_g, wait) = c.wait_timeout_while(g, Duration::from_millis(1), |_| true).unwrap();
116 // no spurious wakeups. ensure it timed-out
117 assert!(wait.timed_out());
121 #[cfg_attr(target_os = "emscripten", ignore)]
122 fn wait_timeout_while_instant_satisfy() {
123 let m = Arc::new(Mutex::new(()));
124 let c = Arc::new(Condvar::new());
126 let g = m.lock().unwrap();
127 let (_g, wait) = c.wait_timeout_while(g, Duration::from_millis(0), |_| false).unwrap();
128 // ensure it didn't time-out even if we were not given any time.
129 assert!(!wait.timed_out());
133 #[cfg_attr(target_os = "emscripten", ignore)]
134 fn wait_timeout_while_wake() {
135 let pair = Arc::new((Mutex::new(false), Condvar::new()));
136 let pair_copy = pair.clone();
138 let &(ref m, ref c) = &*pair;
139 let g = m.lock().unwrap();
140 let _t = thread::spawn(move || {
141 let &(ref lock, ref cvar) = &*pair_copy;
142 let mut started = lock.lock().unwrap();
143 thread::sleep(Duration::from_millis(1));
148 .wait_timeout_while(g, Duration::from_millis(u64::MAX), |&mut notified| !notified)
150 // ensure it didn't time-out even if we were not given any time.
151 assert!(!wait.timed_out());
156 #[cfg_attr(target_os = "emscripten", ignore)]
157 fn wait_timeout_wake() {
158 let m = Arc::new(Mutex::new(()));
159 let c = Arc::new(Condvar::new());
162 let g = m.lock().unwrap();
167 let notified = Arc::new(AtomicBool::new(false));
168 let notified_copy = notified.clone();
170 let t = thread::spawn(move || {
171 let _g = m2.lock().unwrap();
172 thread::sleep(Duration::from_millis(1));
173 notified_copy.store(true, Ordering::SeqCst);
176 let (g, timeout_res) = c.wait_timeout(g, Duration::from_millis(u64::MAX)).unwrap();
177 assert!(!timeout_res.timed_out());
178 // spurious wakeups mean this isn't necessarily true
179 // so execute test again, if not notified
180 if !notified.load(Ordering::SeqCst) {