Skip to content

Commit ce13c0a

Browse files
jhprattgitbot
authored and
gitbot
committed
Rollup merge of rust-lang#134389 - rust-wasi-web:condvar-no-threads, r=m-ou-se
Condvar: implement wait_timeout for targets without threads This always falls back to sleeping since there is no way to notify a condvar on a target without threads. Even on a target that has no threads the following code is a legitimate use case: ```rust use std::sync::{Condvar, Mutex}; use std::time::Duration; fn main() { let cv = Condvar::new(); let mutex = Mutex::new(()); let mut guard = mutex.lock().unwrap(); cv.notify_one(); let res; (guard, res) = cv.wait_timeout(guard, Duration::from_secs(3)).unwrap(); assert!(res.timed_out()); } ```
2 parents ae6d0c8 + 981947c commit ce13c0a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

std/src/sys/sync/condvar/no_threads.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::sys::sync::Mutex;
2+
use crate::thread::sleep;
23
use crate::time::Duration;
34

45
pub struct Condvar {}
@@ -19,7 +20,8 @@ impl Condvar {
1920
panic!("condvar wait not supported")
2021
}
2122

22-
pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
23-
panic!("condvar wait not supported");
23+
pub unsafe fn wait_timeout(&self, _mutex: &Mutex, dur: Duration) -> bool {
24+
sleep(dur);
25+
false
2426
}
2527
}

0 commit comments

Comments
 (0)