Skip to content

Redox: Use futex timeout to implement CondVar::wait_timeout #45893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions src/libstd/sys/redox/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
// except according to those terms.

use cell::UnsafeCell;
use intrinsics::{atomic_cxchg, atomic_xadd, atomic_xchg};
use intrinsics::{atomic_cxchg, atomic_load, atomic_xadd, atomic_xchg};
use ptr;
use time::Duration;

use sys::mutex::{mutex_unlock, Mutex};
use sys::syscall::{futex, FUTEX_WAIT, FUTEX_WAKE, FUTEX_REQUEUE};
use sys::syscall::{futex, TimeSpec, FUTEX_WAIT, FUTEX_WAKE, FUTEX_REQUEUE};

pub struct Condvar {
lock: UnsafeCell<*mut i32>,
Expand Down Expand Up @@ -63,33 +63,50 @@ impl Condvar {
}

#[inline]
pub fn wait(&self, mutex: &Mutex) {
unsafe {
let lock = self.lock.get();
let seq = self.seq.get();

if *lock != mutex.lock.get() {
if *lock != ptr::null_mut() {
panic!("Condvar used with more than one Mutex");
}
unsafe fn wait_inner(&self, mutex: &Mutex, timeout_ptr: *const TimeSpec) -> bool {
let lock = self.lock.get();
let seq = self.seq.get();

atomic_cxchg(lock as *mut usize, 0, mutex.lock.get() as usize);
if *lock != mutex.lock.get() {
if *lock != ptr::null_mut() {
panic!("Condvar used with more than one Mutex");
}

mutex_unlock(*lock);
atomic_cxchg(lock as *mut usize, 0, mutex.lock.get() as usize);
}

let _ = futex(seq, FUTEX_WAIT, *seq, 0, ptr::null_mut());
mutex_unlock(*lock);

while atomic_xchg(*lock, 2) != 0 {
let _ = futex(*lock, FUTEX_WAIT, 2, 0, ptr::null_mut());
}
let seq_before = atomic_load(seq);

let _ = futex(seq, FUTEX_WAIT, seq_before, timeout_ptr as usize, ptr::null_mut());

let seq_after = atomic_load(seq);

while atomic_xchg(*lock, 2) != 0 {
let _ = futex(*lock, FUTEX_WAIT, 2, 0, ptr::null_mut());
}

seq_before != seq_after
}

#[inline]
pub fn wait(&self, mutex: &Mutex) {
unsafe {
assert!(self.wait_inner(mutex, ptr::null()));
}
}

#[inline]
pub fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
::sys_common::util::dumb_print(format_args!("condvar wait_timeout\n"));
unimplemented!();
pub fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
unsafe {
let timeout = TimeSpec {
tv_sec: dur.as_secs() as i64,
tv_nsec: dur.subsec_nanos() as i32
};

self.wait_inner(mutex, &timeout as *const TimeSpec)
}
}

#[inline]
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/sys/redox/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,7 @@ pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
}

pub fn link(_src: &Path, _dst: &Path) -> io::Result<()> {
::sys_common::util::dumb_print(format_args!("Link\n"));
unimplemented!();
Err(Error::from_raw_os_error(syscall::ENOSYS))
}

pub fn stat(p: &Path) -> io::Result<FileAttr> {
Expand Down