about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2025-01-08 00:52:45 -0500
committerGitHub <noreply@github.com>2025-01-08 00:52:45 -0500
commit5ed1fa84a50d6af682fbd3c1d0b455e87f959924 (patch)
treecd5faa5f443bf558e4494391b404d002eea5d345
parente7ee58243a53eacd5f20a668bf096f87bd45c984 (diff)
parent45c7ddfea6ba9980a1c6f4f9bd9c821e56af035e (diff)
downloadrust-5ed1fa84a50d6af682fbd3c1d0b455e87f959924.tar.gz
rust-5ed1fa84a50d6af682fbd3c1d0b455e87f959924.zip
Rollup merge of #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());
}
```
-rw-r--r--library/std/src/sys/sync/condvar/no_threads.rs6
1 files changed, 4 insertions, 2 deletions
diff --git a/library/std/src/sys/sync/condvar/no_threads.rs b/library/std/src/sys/sync/condvar/no_threads.rs
index 88ce39305e1..18d97d4b17a 100644
--- a/library/std/src/sys/sync/condvar/no_threads.rs
+++ b/library/std/src/sys/sync/condvar/no_threads.rs
@@ -1,4 +1,5 @@
 use crate::sys::sync::Mutex;
+use crate::thread::sleep;
 use crate::time::Duration;
 
 pub struct Condvar {}
@@ -19,7 +20,8 @@ impl Condvar {
         panic!("condvar wait not supported")
     }
 
-    pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
-        panic!("condvar wait not supported");
+    pub unsafe fn wait_timeout(&self, _mutex: &Mutex, dur: Duration) -> bool {
+        sleep(dur);
+        false
     }
 }