]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/common/condvar.rs
Imported Upstream version 1.0.0~beta.3
[rustc.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 /// Static initializer for condition variables.
24 pub const CONDVAR_INIT: Condvar = Condvar(imp::CONDVAR_INIT);
25
26 impl Condvar {
27 /// Creates a new condition variable for use.
28 ///
29 /// Behavior is undefined if the condition variable is moved after it is
30 /// first used with any of the functions below.
31 #[inline]
32 pub unsafe fn new() -> Condvar { Condvar(imp::Condvar::new()) }
33
34 /// Signals one waiter on this condition variable to wake up.
35 #[inline]
36 pub unsafe fn notify_one(&self) { self.0.notify_one() }
37
38 /// Awakens all current waiters on this condition variable.
39 #[inline]
40 pub unsafe fn notify_all(&self) { self.0.notify_all() }
41
42 /// Waits for a signal on the specified mutex.
43 ///
44 /// Behavior is undefined if the mutex is not locked by the current thread.
45 /// Behavior is also undefined if more than one mutex is used concurrently
46 /// on this condition variable.
47 #[inline]
48 pub unsafe fn wait(&self, mutex: &Mutex) { self.0.wait(mutex::raw(mutex)) }
49
50 /// Waits for a signal on the specified mutex with a timeout duration
51 /// specified by `dur` (a relative time into the future).
52 ///
53 /// Behavior is undefined if the mutex is not locked by the current thread.
54 /// Behavior is also undefined if more than one mutex is used concurrently
55 /// on this condition variable.
56 #[inline]
57 pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
58 self.0.wait_timeout(mutex::raw(mutex), dur)
59 }
60
61 /// Deallocates all resources associated with this condition variable.
62 ///
63 /// Behavior is undefined if there are current or will be future users of
64 /// this condition variable.
65 #[inline]
66 pub unsafe fn destroy(&self) { self.0.destroy() }
67 }