]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys_common/condvar.rs
:arrow_up: rust-analyzer
[rust.git] / library / std / src / sys_common / condvar.rs
1 use crate::sys::locks as imp;
2 use crate::sys_common::mutex::MovableMutex;
3 use crate::time::Duration;
4
5 mod check;
6
7 type CondvarCheck = <imp::MovableMutex as check::CondvarCheck>::Check;
8
9 /// An OS-based condition variable.
10 pub struct Condvar {
11     inner: imp::MovableCondvar,
12     check: CondvarCheck,
13 }
14
15 impl Condvar {
16     /// Creates a new condition variable for use.
17     #[inline]
18     pub const fn new() -> Self {
19         Self { inner: imp::MovableCondvar::new(), check: CondvarCheck::new() }
20     }
21
22     /// Signals one waiter on this condition variable to wake up.
23     #[inline]
24     pub fn notify_one(&self) {
25         unsafe { self.inner.notify_one() };
26     }
27
28     /// Awakens all current waiters on this condition variable.
29     #[inline]
30     pub fn notify_all(&self) {
31         unsafe { self.inner.notify_all() };
32     }
33
34     /// Waits for a signal on the specified mutex.
35     ///
36     /// Behavior is undefined if the mutex is not locked by the current thread.
37     ///
38     /// May panic if used with more than one mutex.
39     #[inline]
40     pub unsafe fn wait(&self, mutex: &MovableMutex) {
41         self.check.verify(mutex);
42         self.inner.wait(mutex.raw())
43     }
44
45     /// Waits for a signal on the specified mutex with a timeout duration
46     /// specified by `dur` (a relative time into the future).
47     ///
48     /// Behavior is undefined if the mutex is not locked by the current thread.
49     ///
50     /// May panic if used with more than one mutex.
51     #[inline]
52     pub unsafe fn wait_timeout(&self, mutex: &MovableMutex, dur: Duration) -> bool {
53         self.check.verify(mutex);
54         self.inner.wait_timeout(mutex.raw(), dur)
55     }
56 }