summary refs log tree commit diff
path: root/library/std/src/sys/sync/condvar/sgx.rs
blob: e60715e4b592ee9f72e160ea58aaa9a5ab84bef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::sys::pal::waitqueue::{SpinMutex, WaitQueue, WaitVariable};
use crate::sys::sync::{Mutex, OnceBox};
use crate::time::Duration;

pub struct Condvar {
    // FIXME: `UnsafeList` is not movable.
    inner: OnceBox<SpinMutex<WaitVariable<()>>>,
}

impl Condvar {
    pub const fn new() -> Condvar {
        Condvar { inner: OnceBox::new() }
    }

    fn get(&self) -> &SpinMutex<WaitVariable<()>> {
        self.inner.get_or_init(|| Box::new(SpinMutex::new(WaitVariable::new(()))))
    }

    #[inline]
    pub fn notify_one(&self) {
        let _ = WaitQueue::notify_one(self.get().lock());
    }

    #[inline]
    pub fn notify_all(&self) {
        let _ = WaitQueue::notify_all(self.get().lock());
    }

    pub unsafe fn wait(&self, mutex: &Mutex) {
        let guard = self.get().lock();
        WaitQueue::wait(guard, || unsafe { mutex.unlock() });
        mutex.lock()
    }

    pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
        let success = WaitQueue::wait_timeout(self.get(), dur, || unsafe { mutex.unlock() });
        mutex.lock();
        success
    }
}