]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/condvar.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.git] / src / libstd / sys / unix / 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 prelude::v1::*;
12
13 use cell::UnsafeCell;
14 use libc;
15 use ptr;
16 use sys::mutex::{self, Mutex};
17 use sys::time;
18 use sys::sync as ffi;
19 use time::Duration;
20
21 pub struct Condvar { inner: UnsafeCell<ffi::pthread_cond_t> }
22
23 unsafe impl Send for Condvar {}
24 unsafe impl Sync for Condvar {}
25
26 pub const CONDVAR_INIT: Condvar = Condvar {
27 inner: UnsafeCell { value: ffi::PTHREAD_COND_INITIALIZER },
28 };
29
30 impl Condvar {
31 #[inline]
32 pub unsafe fn new() -> Condvar {
33 // Might be moved and address is changing it is better to avoid
34 // initialization of potentially opaque OS data before it landed
35 Condvar { inner: UnsafeCell::new(ffi::PTHREAD_COND_INITIALIZER) }
36 }
37
38 #[inline]
39 pub unsafe fn notify_one(&self) {
40 let r = ffi::pthread_cond_signal(self.inner.get());
41 debug_assert_eq!(r, 0);
42 }
43
44 #[inline]
45 pub unsafe fn notify_all(&self) {
46 let r = ffi::pthread_cond_broadcast(self.inner.get());
47 debug_assert_eq!(r, 0);
48 }
49
50 #[inline]
51 pub unsafe fn wait(&self, mutex: &Mutex) {
52 let r = ffi::pthread_cond_wait(self.inner.get(), mutex::raw(mutex));
53 debug_assert_eq!(r, 0);
54 }
55
56 // This implementation is modeled after libcxx's condition_variable
57 // https://github.com/llvm-mirror/libcxx/blob/release_35/src/condition_variable.cpp#L46
58 // https://github.com/llvm-mirror/libcxx/blob/release_35/include/__mutex_base#L367
59 pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
60 // First, figure out what time it currently is, in both system and
61 // stable time. pthread_cond_timedwait uses system time, but we want to
62 // report timeout based on stable time.
63 let mut sys_now = libc::timeval { tv_sec: 0, tv_usec: 0 };
64 let stable_now = time::SteadyTime::now();
65 let r = ffi::gettimeofday(&mut sys_now, ptr::null_mut());
66 debug_assert_eq!(r, 0);
67
68 let seconds = dur.secs() as libc::time_t;
69 let timeout = match sys_now.tv_sec.checked_add(seconds) {
70 Some(sec) => {
71 libc::timespec {
72 tv_sec: sec,
73 tv_nsec: dur.extra_nanos() as libc::c_long,
74 }
75 }
76 None => {
77 libc::timespec {
78 tv_sec: <libc::time_t>::max_value(),
79 tv_nsec: 1_000_000_000 - 1,
80 }
81 }
82 };
83
84 // And wait!
85 let r = ffi::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex),
86 &timeout);
87 debug_assert!(r == libc::ETIMEDOUT || r == 0);
88
89 // ETIMEDOUT is not a totally reliable method of determining timeout due
90 // to clock shifts, so do the check ourselves
91 &time::SteadyTime::now() - &stable_now < dur
92 }
93
94 #[inline]
95 #[cfg(not(target_os = "dragonfly"))]
96 pub unsafe fn destroy(&self) {
97 let r = ffi::pthread_cond_destroy(self.inner.get());
98 debug_assert_eq!(r, 0);
99 }
100
101 #[inline]
102 #[cfg(target_os = "dragonfly")]
103 pub unsafe fn destroy(&self) {
104 let r = ffi::pthread_cond_destroy(self.inner.get());
105 // On DragonFly pthread_cond_destroy() returns EINVAL if called on
106 // a condvar that was just initialized with
107 // ffi::PTHREAD_COND_INITIALIZER. Once it is used or
108 // pthread_cond_init() is called, this behaviour no longer occurs.
109 debug_assert!(r == 0 || r == libc::EINVAL);
110 }
111 }