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::{self, 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);
24 /// Creates a new condition variable for use.
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()) }
30 /// Prepares the condition variable for use.
32 /// This should be called once the condition variable is at a stable memory
35 pub unsafe fn init(&mut self) { self.0.init() }
37 /// Signals one waiter on this condition variable to wake up.
39 pub unsafe fn notify_one(&self) { self.0.notify_one() }
41 /// Awakens all current waiters on this condition variable.
43 pub unsafe fn notify_all(&self) { self.0.notify_all() }
45 /// Waits for a signal on the specified mutex.
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.
51 pub unsafe fn wait(&self, mutex: &Mutex) { self.0.wait(mutex::raw(mutex)) }
53 /// Waits for a signal on the specified mutex with a timeout duration
54 /// specified by `dur` (a relative time into the future).
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.
60 pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
61 self.0.wait_timeout(mutex::raw(mutex), dur)
64 /// Deallocates all resources associated with this condition variable.
66 /// Behavior is undefined if there are current or will be future users of
67 /// this condition variable.
69 pub unsafe fn destroy(&self) { self.0.destroy() }