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