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.
16 use sys
::mutex
::{self, Mutex}
;
21 pub struct Condvar { inner: UnsafeCell<ffi::pthread_cond_t> }
23 unsafe impl Send
for Condvar {}
24 unsafe impl Sync
for 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) }
34 pub unsafe fn notify_one(&self) {
35 let r
= ffi
::pthread_cond_signal(self.inner
.get());
36 debug_assert_eq
!(r
, 0);
40 pub unsafe fn notify_all(&self) {
41 let r
= ffi
::pthread_cond_broadcast(self.inner
.get());
42 debug_assert_eq
!(r
, 0);
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);
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);
63 let nsec
= dur
.extra_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
.secs() as libc
::time_t
;
69 let timeout
= sys_now
.tv_sec
.checked_add(extra
).and_then(|s
| {
70 s
.checked_add(seconds
)
72 libc
::timespec { tv_sec: s, tv_nsec: nsec }
73 }).unwrap_or_else(|| {
75 tv_sec
: <libc
::time_t
>::max_value(),
76 tv_nsec
: 1_000_000_000 - 1,
81 let r
= ffi
::pthread_cond_timedwait(self.inner
.get(), mutex
::raw(mutex
),
83 debug_assert
!(r
== libc
::ETIMEDOUT
|| r
== 0);
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
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);
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
);