1 use crate::sys::condvar as imp;
2 use crate::sys::mutex as mutex_imp;
3 use crate::sys_common::mutex::MovableMutex;
4 use crate::time::Duration;
8 type CondvarCheck = <mutex_imp::MovableMutex as check::CondvarCheck>::Check;
10 /// An OS-based condition variable.
12 inner: Box<imp::Condvar>,
17 /// Creates a new condition variable for use.
18 pub fn new() -> Self {
19 let mut c = box imp::Condvar::new();
21 Self { inner: c, check: CondvarCheck::new() }
24 /// Signals one waiter on this condition variable to wake up.
26 pub fn notify_one(&self) {
27 unsafe { self.inner.notify_one() };
30 /// Awakens all current waiters on this condition variable.
32 pub fn notify_all(&self) {
33 unsafe { self.inner.notify_all() };
36 /// Waits for a signal on the specified mutex.
38 /// Behavior is undefined if the mutex is not locked by the current thread.
40 /// May panic if used with more than one mutex.
42 pub unsafe fn wait(&self, mutex: &MovableMutex) {
43 self.check.verify(mutex);
44 self.inner.wait(mutex.raw())
47 /// Waits for a signal on the specified mutex with a timeout duration
48 /// specified by `dur` (a relative time into the future).
50 /// Behavior is undefined if the mutex is not locked by the current thread.
52 /// May panic if used with more than one mutex.
54 pub unsafe fn wait_timeout(&self, mutex: &MovableMutex, dur: Duration) -> bool {
55 self.check.verify(mutex);
56 self.inner.wait_timeout(mutex.raw(), dur)
60 impl Drop for Condvar {
62 unsafe { self.inner.destroy() };