about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-10-31 00:33:25 +0200
committerGitHub <noreply@github.com>2021-10-31 00:33:25 +0200
commit0da75bcc9cdb3e9986fcf870e52a2303e5a29b7e (patch)
treef6a2763ba66a56542591e56564cdf0db2f0f87f6
parent1adb6643928dd3bacdd079f87f7f4cdacf510823 (diff)
parent42cab439f564812be0c3957edcc57140aac5a4c7 (diff)
downloadrust-0da75bcc9cdb3e9986fcf870e52a2303e5a29b7e.tar.gz
rust-0da75bcc9cdb3e9986fcf870e52a2303e5a29b7e.zip
Rollup merge of #90401 - mkroening:hermit-condvar, r=joshtriplett
hermit: Implement Condvar::wait_timeout

This implements `Condvar::wait_timeout` for the `hermit` target.

See
* https://github.com/hermitcore/rust/pull/2
* https://github.com/hermitcore/rust/pull/5

CC: `@stlankes`
-rw-r--r--library/std/src/sys/hermit/condvar.rs16
1 files changed, 14 insertions, 2 deletions
diff --git a/library/std/src/sys/hermit/condvar.rs b/library/std/src/sys/hermit/condvar.rs
index fa8ef8fc37a..b62f21a9dac 100644
--- a/library/std/src/sys/hermit/condvar.rs
+++ b/library/std/src/sys/hermit/condvar.rs
@@ -55,8 +55,20 @@ impl Condvar {
         mutex.lock();
     }
 
-    pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
-        panic!("wait_timeout not supported on hermit");
+    pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
+        self.counter.fetch_add(1, SeqCst);
+        mutex.unlock();
+        let millis = dur.as_millis().min(u32::MAX as u128) as u32;
+
+        let res = if millis > 0 {
+            abi::sem_timedwait(self.sem1, millis)
+        } else {
+            abi::sem_trywait(self.sem1)
+        };
+
+        abi::sem_post(self.sem2);
+        mutex.lock();
+        res == 0
     }
 
     pub unsafe fn destroy(&self) {