]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/condvar.rs
SGX target: implement synchronization primitives and threading
[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     #[unstable(feature = "sys_internals", issue = "0")] // FIXME: min_const_fn
29     pub const fn new() -> Condvar { Condvar(imp::Condvar::new()) }
30
31     /// Prepares the condition variable for use.
32     ///
33     /// This should be called once the condition variable is at a stable memory
34     /// address.
35     #[inline]
36     pub unsafe fn init(&mut self) { self.0.init() }
37
38     /// Signals one waiter on this condition variable to wake up.
39     #[inline]
40     pub unsafe fn notify_one(&self) { self.0.notify_one() }
41
42     /// Awakens all current waiters on this condition variable.
43     #[inline]
44     pub unsafe fn notify_all(&self) { self.0.notify_all() }
45
46     /// Waits for a signal on the specified mutex.
47     ///
48     /// Behavior is undefined if the mutex is not locked by the current thread.
49     /// Behavior is also undefined if more than one mutex is used concurrently
50     /// on this condition variable.
51     #[inline]
52     pub unsafe fn wait(&self, mutex: &Mutex) { self.0.wait(mutex::raw(mutex)) }
53
54     /// Waits for a signal on the specified mutex with a timeout duration
55     /// specified by `dur` (a relative time into the future).
56     ///
57     /// Behavior is undefined if the mutex is not locked by the current thread.
58     /// Behavior is also undefined if more than one mutex is used concurrently
59     /// on this condition variable.
60     #[inline]
61     pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
62         self.0.wait_timeout(mutex::raw(mutex), dur)
63     }
64
65     /// Deallocates all resources associated with this condition variable.
66     ///
67     /// Behavior is undefined if there are current or will be future users of
68     /// this condition variable.
69     #[inline]
70     pub unsafe fn destroy(&self) { self.0.destroy() }
71 }