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.
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.
12 use sys_common::mutex::{mod, Mutex};
13 use sys::condvar as imp;
15 /// An OS-based condition variable.
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
21 pub struct Condvar(imp::Condvar);
23 /// Static initializer for condition variables.
24 pub const CONDVAR_INIT: Condvar = Condvar(imp::CONDVAR_INIT);
27 /// Creates a new condition variable for use.
29 /// Behavior is undefined if the condition variable is moved after it is
30 /// first used with any of the functions below.
32 pub unsafe fn new() -> Condvar { Condvar(imp::Condvar::new()) }
34 /// Signal one waiter on this condition variable to wake up.
36 pub unsafe fn notify_one(&self) { self.0.notify_one() }
38 /// Awaken all current waiters on this condition variable.
40 pub unsafe fn notify_all(&self) { self.0.notify_all() }
42 /// Wait for a signal on the specified mutex.
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.
48 pub unsafe fn wait(&self, mutex: &Mutex) { self.0.wait(mutex::raw(mutex)) }
50 /// Wait for a signal on the specified mutex with a timeout duration
51 /// specified by `dur` (a relative time into the future).
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.
57 pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
58 self.0.wait_timeout(mutex::raw(mutex), dur)
61 /// Deallocate all resources associated with this condition variable.
63 /// Behavior is undefined if there are current or will be future users of
64 /// this condition variable.
66 pub unsafe fn destroy(&self) { self.0.destroy() }