]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/condvar.rs
b6f29dd5fc3d3184f6a528fcb36f70b64b3f1051
[rust.git] / src / libstd / sys_common / condvar.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use time::Duration;
12 use sys_common::mutex::{self, Mutex};
13 use sys::condvar as imp;
14
15 /// An OS-based condition variable.
16 ///
17 /// This structure is the lowest layer possible on top of the OS-provided
18 /// condition variables. It is consequently entirely unsafe to use. It is
19 /// recommended to use the safer types at the top level of this crate instead of
20 /// this type.
21 pub struct Condvar(imp::Condvar);
22
23 impl Condvar {
24     /// Creates a new condition variable for use.
25     ///
26     /// Behavior is undefined if the condition variable is moved after it is
27     /// first used with any of the functions below.
28     pub const fn new() -> Condvar { Condvar(imp::Condvar::new()) }
29
30     /// Prepares the condition variable for use.
31     ///
32     /// This should be called once the condition variable is at a stable memory
33     /// address.
34     #[inline]
35     pub unsafe fn init(&mut self) { self.0.init() }
36
37     /// Signals one waiter on this condition variable to wake up.
38     #[inline]
39     pub unsafe fn notify_one(&self) { self.0.notify_one() }
40
41     /// Awakens all current waiters on this condition variable.
42     #[inline]
43     pub unsafe fn notify_all(&self) { self.0.notify_all() }
44
45     /// Waits for a signal on the specified mutex.
46     ///
47     /// Behavior is undefined if the mutex is not locked by the current thread.
48     /// Behavior is also undefined if more than one mutex is used concurrently
49     /// on this condition variable.
50     #[inline]
51     pub unsafe fn wait(&self, mutex: &Mutex) { self.0.wait(mutex::raw(mutex)) }
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) { self.0.destroy() }
70 }