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