]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/sgx/condvar.rs
Rollup merge of #105482 - wesleywiser:fix_debuginfo_ub, r=tmiasko
[rust.git] / library / std / src / sys / sgx / condvar.rs
1 use crate::sys::locks::Mutex;
2 use crate::sys_common::lazy_box::{LazyBox, LazyInit};
3 use crate::time::Duration;
4
5 use super::waitqueue::{SpinMutex, WaitQueue, WaitVariable};
6
7 /// FIXME: `UnsafeList` is not movable.
8 struct AllocatedCondvar(SpinMutex<WaitVariable<()>>);
9
10 pub struct Condvar {
11     inner: LazyBox<AllocatedCondvar>,
12 }
13
14 impl LazyInit for AllocatedCondvar {
15     fn init() -> Box<Self> {
16         Box::new(AllocatedCondvar(SpinMutex::new(WaitVariable::new(()))))
17     }
18 }
19
20 impl Condvar {
21     pub const fn new() -> Condvar {
22         Condvar { inner: LazyBox::new() }
23     }
24
25     #[inline]
26     pub fn notify_one(&self) {
27         let _ = WaitQueue::notify_one(self.inner.0.lock());
28     }
29
30     #[inline]
31     pub fn notify_all(&self) {
32         let _ = WaitQueue::notify_all(self.inner.0.lock());
33     }
34
35     pub unsafe fn wait(&self, mutex: &Mutex) {
36         let guard = self.inner.0.lock();
37         WaitQueue::wait(guard, || unsafe { mutex.unlock() });
38         mutex.lock()
39     }
40
41     pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
42         let success = WaitQueue::wait_timeout(&self.inner.0, dur, || unsafe { mutex.unlock() });
43         mutex.lock();
44         success
45     }
46 }