1 use crate::sys
::locks
as imp
;
2 use crate::sys_common
::mutex
::MovableMutex
;
3 use crate::time
::Duration
;
7 type CondvarCheck
= <imp
::MovableMutex
as check
::CondvarCheck
>::Check
;
9 /// An OS-based condition variable.
11 inner
: imp
::MovableCondvar
,
16 /// Creates a new condition variable for use.
18 #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
19 pub const fn new() -> Self {
20 Self { inner: imp::MovableCondvar::new(), check: CondvarCheck::new() }
23 /// Signals one waiter on this condition variable to wake up.
25 pub fn notify_one(&self) {
26 unsafe { self.inner.notify_one() }
;
29 /// Awakens all current waiters on this condition variable.
31 pub fn notify_all(&self) {
32 unsafe { self.inner.notify_all() }
;
35 /// Waits for a signal on the specified mutex.
37 /// Behavior is undefined if the mutex is not locked by the current thread.
39 /// May panic if used with more than one mutex.
41 pub unsafe fn wait(&self, mutex
: &MovableMutex
) {
42 self.check
.verify(mutex
);
43 self.inner
.wait(mutex
.raw())
46 /// Waits for a signal on the specified mutex with a timeout duration
47 /// specified by `dur` (a relative time into the future).
49 /// Behavior is undefined if the mutex is not locked by the current thread.
51 /// May panic if used with more than one mutex.
53 pub unsafe fn wait_timeout(&self, mutex
: &MovableMutex
, dur
: Duration
) -> bool
{
54 self.check
.verify(mutex
);
55 self.inner
.wait_timeout(mutex
.raw(), dur
)