]> git.lizzy.rs Git - rust.git/commitdiff
hermit: Implement Condvar::wait_timeout
authorMartin Kröning <mkroening@posteo.net>
Sat, 29 May 2021 20:16:10 +0000 (22:16 +0200)
committerMartin Kröning <mkroening@posteo.net>
Fri, 29 Oct 2021 15:20:03 +0000 (17:20 +0200)
library/std/src/sys/hermit/condvar.rs

index fa8ef8fc37a95d4ab3a1e4c75bd95346cbb3e042..b62f21a9dac02d705989c77295020fe630da2cc7 100644 (file)
@@ -55,8 +55,20 @@ pub unsafe fn wait(&self, mutex: &Mutex) {
         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) {