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 {}
26 pub const CONDVAR_INIT
: Condvar
= Condvar
{
27 inner
: UnsafeCell { value: ffi::PTHREAD_COND_INITIALIZER }
,
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) }
39 pub unsafe fn notify_one(&self) {
40 let r
= ffi
::pthread_cond_signal(self.inner
.get());
41 debug_assert_eq
!(r
, 0);
45 pub unsafe fn notify_all(&self) {
46 let r
= ffi
::pthread_cond_broadcast(self.inner
.get());
47 debug_assert_eq
!(r
, 0);
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);
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);
68 let seconds
= dur
.secs() as libc
::time_t
;
69 let timeout
= match sys_now
.tv_sec
.checked_add(seconds
) {
73 tv_nsec
: dur
.extra_nanos() as libc
::c_long
,
78 tv_sec
: <libc
::time_t
>::max_value(),
79 tv_nsec
: 1_000_000_000 - 1,
85 let r
= ffi
::pthread_cond_timedwait(self.inner
.get(), mutex
::raw(mutex
),
87 debug_assert
!(r
== libc
::ETIMEDOUT
|| r
== 0);
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
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);
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
);