]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys_common/condvar.rs
Auto merge of #96551 - ferrocene:pa-ignore-paths-when-abbreviating, r=Mark-Simulacrum
[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     pub fn new() -> Self {
18         Self { inner: imp::MovableCondvar::new(), check: CondvarCheck::new() }
19     }
20
21     /// Signals one waiter on this condition variable to wake up.
22     #[inline]
23     pub fn notify_one(&self) {
24         unsafe { self.inner.notify_one() };
25     }
26
27     /// Awakens all current waiters on this condition variable.
28     #[inline]
29     pub fn notify_all(&self) {
30         unsafe { self.inner.notify_all() };
31     }
32
33     /// Waits for a signal on the specified mutex.
34     ///
35     /// Behavior is undefined if the mutex is not locked by the current thread.
36     ///
37     /// May panic if used with more than one mutex.
38     #[inline]
39     pub unsafe fn wait(&self, mutex: &MovableMutex) {
40         self.check.verify(mutex);
41         self.inner.wait(mutex.raw())
42     }
43
44     /// Waits for a signal on the specified mutex with a timeout duration
45     /// specified by `dur` (a relative time into the future).
46     ///
47     /// Behavior is undefined if the mutex is not locked by the current thread.
48     ///
49     /// May panic if used with more than one mutex.
50     #[inline]
51     pub unsafe fn wait_timeout(&self, mutex: &MovableMutex, dur: Duration) -> bool {
52         self.check.verify(mutex);
53         self.inner.wait_timeout(mutex.raw(), dur)
54     }
55 }