aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys/unix/condvar.rs
diff options
context:
space:
mode:
authorValentin <[email protected]>2018-06-15 18:57:24 +0200
committerFenrirWolf <[email protected]>2018-06-15 10:57:24 -0600
commitf2a90174bb36b9ad528e863ab34c02ebce002b02 (patch)
tree959e8d67883d3a89e179b3549b1f30d28e51a87c /ctr-std/src/sys/unix/condvar.rs
parentMerge pull request #68 from linouxis9/master (diff)
downloadarchived-ctru-rs-f2a90174bb36b9ad528e863ab34c02ebce002b02.tar.xz
archived-ctru-rs-f2a90174bb36b9ad528e863ab34c02ebce002b02.zip
Update for latest nightly 2018-06-09 (#70)
* Update for latest nightly 2018-06-09 * We now have a proper horizon os and sys modules in libstd
Diffstat (limited to 'ctr-std/src/sys/unix/condvar.rs')
-rw-r--r--ctr-std/src/sys/unix/condvar.rs240
1 files changed, 144 insertions, 96 deletions
diff --git a/ctr-std/src/sys/unix/condvar.rs b/ctr-std/src/sys/unix/condvar.rs
index bfff16b..4f878d8 100644
--- a/ctr-std/src/sys/unix/condvar.rs
+++ b/ctr-std/src/sys/unix/condvar.rs
@@ -1,4 +1,4 @@
-// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
@@ -8,132 +8,180 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-// *Implementation adapted from `/sys/redox/condvar.rs`
-
use cell::UnsafeCell;
-use intrinsics::atomic_cxchg;
-use ptr;
-use time::Duration;
-
+use libc;
use sys::mutex::{self, Mutex};
+use time::Duration;
-pub struct Condvar {
- lock: UnsafeCell<*mut ::libctru::LightLock>,
-}
+pub struct Condvar { inner: UnsafeCell<libc::pthread_cond_t> }
unsafe impl Send for Condvar {}
unsafe impl Sync for Condvar {}
+const TIMESPEC_MAX: libc::timespec = libc::timespec {
+ tv_sec: <libc::time_t>::max_value(),
+ tv_nsec: 1_000_000_000 - 1,
+};
+
+fn saturating_cast_to_time_t(value: u64) -> libc::time_t {
+ if value > <libc::time_t>::max_value() as u64 {
+ <libc::time_t>::max_value()
+ } else {
+ value as libc::time_t
+ }
+}
+
impl Condvar {
pub const fn new() -> Condvar {
- Condvar {
- lock: UnsafeCell::new(ptr::null_mut()),
- }
+ // Might be moved and address is changing it is better to avoid
+ // initialization of potentially opaque OS data before it landed
+ Condvar { inner: UnsafeCell::new(libc::PTHREAD_COND_INITIALIZER) }
}
- #[inline]
- pub unsafe fn init(&self) {
- *self.lock.get() = ptr::null_mut();
+ #[cfg(any(target_os = "macos",
+ target_os = "ios",
+ target_os = "l4re",
+ target_os = "android"))]
+ pub unsafe fn init(&mut self) {}
+
+ #[cfg(not(any(target_os = "macos",
+ target_os = "ios",
+ target_os = "l4re",
+ target_os = "android")))]
+ pub unsafe fn init(&mut self) {
+ use mem;
+ let mut attr: libc::pthread_condattr_t = mem::uninitialized();
+ let r = libc::pthread_condattr_init(&mut attr);
+ assert_eq!(r, 0);
+ let r = libc::pthread_condattr_setclock(&mut attr, libc::CLOCK_MONOTONIC);
+ assert_eq!(r, 0);
+ let r = libc::pthread_cond_init(self.inner.get(), &attr);
+ assert_eq!(r, 0);
+ let r = libc::pthread_condattr_destroy(&mut attr);
+ assert_eq!(r, 0);
}
#[inline]
- pub fn notify_one(&self) {
- unsafe {
- let arbiter = ::libctru::__sync_get_arbiter();
-
- ::libctru::svcArbitrateAddress(arbiter,
- *self.lock.get() as u32,
- ::libctru::ARBITRATION_SIGNAL,
- 1,
- 0);
- }
+ pub unsafe fn notify_one(&self) {
+ let r = libc::pthread_cond_signal(self.inner.get());
+ debug_assert_eq!(r, 0);
}
#[inline]
- pub fn notify_all(&self) {
- unsafe {
- let lock = self.lock.get();
-
- if *lock == ptr::null_mut() {
- return;
- }
-
- let arbiter = ::libctru::__sync_get_arbiter();
-
- ::libctru::svcArbitrateAddress(arbiter,
- *self.lock.get() as u32,
- ::libctru::ARBITRATION_SIGNAL,
- -1,
- 0);
- }
+ pub unsafe fn notify_all(&self) {
+ let r = libc::pthread_cond_broadcast(self.inner.get());
+ debug_assert_eq!(r, 0);
}
#[inline]
- pub fn wait(&self, mutex: &Mutex) {
- unsafe {
- let lock = self.lock.get();
-
- if *lock != mutex::raw(mutex) {
- if *lock != ptr::null_mut() {
- panic!("Condvar used with more than one Mutex");
- }
-
- atomic_cxchg(lock as *mut usize, 0, mutex::raw(mutex) as usize);
- }
+ pub unsafe fn wait(&self, mutex: &Mutex) {
+ let r = libc::pthread_cond_wait(self.inner.get(), mutex::raw(mutex));
+ debug_assert_eq!(r, 0);
+ }
- mutex.unlock();
+ // This implementation is used on systems that support pthread_condattr_setclock
+ // where we configure condition variable to use monotonic clock (instead of
+ // default system clock). This approach avoids all problems that result
+ // from changes made to the system time.
+ #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
+ pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
+ use mem;
+
+ let mut now: libc::timespec = mem::zeroed();
+ let r = libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now);
+ assert_eq!(r, 0);
+
+ // Nanosecond calculations can't overflow because both values are below 1e9.
+ let nsec = dur.subsec_nanos() + now.tv_nsec as u32;
+
+ let sec = saturating_cast_to_time_t(dur.as_secs())
+ .checked_add((nsec / 1_000_000_000) as libc::time_t)
+ .and_then(|s| s.checked_add(now.tv_sec));
+ let nsec = nsec % 1_000_000_000;
+
+ let timeout = sec.map(|s| {
+ libc::timespec { tv_sec: s, tv_nsec: nsec as _}
+ }).unwrap_or(TIMESPEC_MAX);
+
+ let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex),
+ &timeout);
+ assert!(r == libc::ETIMEDOUT || r == 0);
+ r == 0
+ }
- let arbiter = ::libctru::__sync_get_arbiter();
- ::libctru::svcArbitrateAddress(arbiter,
- *self.lock.get() as u32,
- ::libctru::ARBITRATION_WAIT_IF_LESS_THAN,
- 2,
- 0);
+ // This implementation is modeled after libcxx's condition_variable
+ // https://github.com/llvm-mirror/libcxx/blob/release_35/src/condition_variable.cpp#L46
+ // https://github.com/llvm-mirror/libcxx/blob/release_35/include/__mutex_base#L367
+ #[cfg(any(target_os = "macos", target_os = "ios", target_os = "android"))]
+ pub unsafe fn wait_timeout(&self, mutex: &Mutex, mut dur: Duration) -> bool {
+ use ptr;
+ use time::Instant;
- mutex.lock();
+ // 1000 years
+ let max_dur = Duration::from_secs(1000 * 365 * 86400);
+
+ if dur > max_dur {
+ // OSX implementation of `pthread_cond_timedwait` is buggy
+ // with super long durations. When duration is greater than
+ // 0x100_0000_0000_0000 seconds, `pthread_cond_timedwait`
+ // in macOS Sierra return error 316.
+ //
+ // This program demonstrates the issue:
+ // https://gist.github.com/stepancheg/198db4623a20aad2ad7cddb8fda4a63c
+ //
+ // To work around this issue, and possible bugs of other OSes, timeout
+ // is clamped to 1000 years, which is allowable per the API of `wait_timeout`
+ // because of spurious wakeups.
+
+ dur = max_dur;
}
+
+ // First, figure out what time it currently is, in both system and
+ // stable time. pthread_cond_timedwait uses system time, but we want to
+ // report timeout based on stable time.
+ let mut sys_now = libc::timeval { tv_sec: 0, tv_usec: 0 };
+ let stable_now = Instant::now();
+ let r = libc::gettimeofday(&mut sys_now, ptr::null_mut());
+ debug_assert_eq!(r, 0);
+
+ let nsec = dur.subsec_nanos() as libc::c_long +
+ (sys_now.tv_usec * 1000) as libc::c_long;
+ let extra = (nsec / 1_000_000_000) as libc::time_t;
+ let nsec = nsec % 1_000_000_000;
+ let seconds = saturating_cast_to_time_t(dur.as_secs());
+
+ let timeout = sys_now.tv_sec.checked_add(extra).and_then(|s| {
+ s.checked_add(seconds)
+ }).map(|s| {
+ libc::timespec { tv_sec: s, tv_nsec: nsec }
+ }).unwrap_or(TIMESPEC_MAX);
+
+ // And wait!
+ let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex),
+ &timeout);
+ debug_assert!(r == libc::ETIMEDOUT || r == 0);
+
+ // ETIMEDOUT is not a totally reliable method of determining timeout due
+ // to clock shifts, so do the check ourselves
+ stable_now.elapsed() < dur
}
#[inline]
- pub fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
- use time::Instant;
-
- unsafe {
- let lock = self.lock.get();
-
- if *lock != mutex::raw(mutex) {
- if *lock != ptr::null_mut() {
- panic!("Condvar used with more than one Mutex");
- }
-
- atomic_cxchg(lock as *mut usize, 0, mutex::raw(mutex) as usize);
- }
-
- let now = Instant::now();
-
- let nanos = dur.as_secs()
- .saturating_mul(1_000_000_000)
- .saturating_add(dur.subsec_nanos() as u64);
-
- mutex.unlock();
-
- let arbiter = ::libctru::__sync_get_arbiter();
-
- ::libctru::svcArbitrateAddress(arbiter,
- *self.lock.get() as u32,
- ::libctru::ARBITRATION_WAIT_IF_LESS_THAN_TIMEOUT,
- 2,
- nanos as i64);
-
- mutex.lock();
-
- now.elapsed() < dur
- }
+ #[cfg(not(target_os = "dragonfly"))]
+ pub unsafe fn destroy(&self) {
+ let r = libc::pthread_cond_destroy(self.inner.get());
+ debug_assert_eq!(r, 0);
}
#[inline]
+ #[cfg(target_os = "dragonfly")]
pub unsafe fn destroy(&self) {
- *self.lock.get() = ptr::null_mut();
+ let r = libc::pthread_cond_destroy(self.inner.get());
+ // On DragonFly pthread_cond_destroy() returns EINVAL if called on
+ // a condvar that was just initialized with
+ // libc::PTHREAD_COND_INITIALIZER. Once it is used or
+ // pthread_cond_init() is called, this behaviour no longer occurs.
+ debug_assert!(r == 0 || r == libc::EINVAL);
}
}