]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/condvar.rs
Rollup merge of #68441 - Centril:pprust-as_deref, r=Mark-Simulacrum
[rust.git] / src / libstd / sys_common / condvar.rs
1 use crate::sys::condvar as imp;
2 use crate::sys_common::mutex::{self, Mutex};
3 use crate::time::Duration;
4
5 /// An OS-based condition variable.
6 ///
7 /// This structure is the lowest layer possible on top of the OS-provided
8 /// condition variables. It is consequently entirely unsafe to use. It is
9 /// recommended to use the safer types at the top level of this crate instead of
10 /// this type.
11 pub struct Condvar(imp::Condvar);
12
13 impl Condvar {
14     /// Creates a new condition variable for use.
15     ///
16     /// Behavior is undefined if the condition variable is moved after it is
17     /// first used with any of the functions below.
18     pub const fn new() -> Condvar {
19         Condvar(imp::Condvar::new())
20     }
21
22     /// Prepares the condition variable for use.
23     ///
24     /// This should be called once the condition variable is at a stable memory
25     /// address.
26     #[inline]
27     pub unsafe fn init(&mut self) {
28         self.0.init()
29     }
30
31     /// Signals one waiter on this condition variable to wake up.
32     #[inline]
33     pub unsafe fn notify_one(&self) {
34         self.0.notify_one()
35     }
36
37     /// Awakens all current waiters on this condition variable.
38     #[inline]
39     pub unsafe fn notify_all(&self) {
40         self.0.notify_all()
41     }
42
43     /// Waits for a signal on the specified mutex.
44     ///
45     /// Behavior is undefined if the mutex is not locked by the current thread.
46     /// Behavior is also undefined if more than one mutex is used concurrently
47     /// on this condition variable.
48     #[inline]
49     pub unsafe fn wait(&self, mutex: &Mutex) {
50         self.0.wait(mutex::raw(mutex))
51     }
52
53     /// Waits for a signal on the specified mutex with a timeout duration
54     /// specified by `dur` (a relative time into the future).
55     ///
56     /// Behavior is undefined if the mutex is not locked by the current thread.
57     /// Behavior is also undefined if more than one mutex is used concurrently
58     /// on this condition variable.
59     #[inline]
60     pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
61         self.0.wait_timeout(mutex::raw(mutex), dur)
62     }
63
64     /// Deallocates all resources associated with this condition variable.
65     ///
66     /// Behavior is undefined if there are current or will be future users of
67     /// this condition variable.
68     #[inline]
69     pub unsafe fn destroy(&self) {
70         self.0.destroy()
71     }
72 }