std/sys/sync/condvar/
futex.rs1use crate::sync::atomic::Ordering::Relaxed;
2use crate::sys::futex::{Futex, futex_wait, futex_wake, futex_wake_all};
3use crate::sys::sync::Mutex;
4use crate::time::Duration;
5
6pub struct Condvar {
7    futex: Futex,
11}
12
13impl Condvar {
14    #[inline]
15    pub const fn new() -> Self {
16        Self { futex: Futex::new(0) }
17    }
18
19    pub fn notify_one(&self) {
23        self.futex.fetch_add(1, Relaxed);
24        futex_wake(&self.futex);
25    }
26
27    pub fn notify_all(&self) {
28        self.futex.fetch_add(1, Relaxed);
29        futex_wake_all(&self.futex);
30    }
31
32    pub unsafe fn wait(&self, mutex: &Mutex) {
33        self.wait_optional_timeout(mutex, None);
34    }
35
36    pub unsafe fn wait_timeout(&self, mutex: &Mutex, timeout: Duration) -> bool {
37        self.wait_optional_timeout(mutex, Some(timeout))
38    }
39
40    unsafe fn wait_optional_timeout(&self, mutex: &Mutex, timeout: Option<Duration>) -> bool {
41        let futex_value = self.futex.load(Relaxed);
43
44        mutex.unlock();
46
47        let r = futex_wait(&self.futex, futex_value, timeout);
50
51        mutex.lock();
53
54        r
55    }
56}