Add timeout to futex_wait calls

This commit is contained in:
Jeremy Soller
2022-04-04 20:23:29 -06:00
parent 6d394ba4f4
commit 8576b99759
6 changed files with 25 additions and 13 deletions
+2 -2
View File
@@ -207,8 +207,8 @@ impl Pal for Sys {
e(unsafe { syscall!(FTRUNCATE, fildes, length) }) as c_int
}
fn futex(addr: *mut c_int, op: c_int, val: c_int) -> c_int {
unsafe { syscall!(FUTEX, addr, op, val, 0, 0, 0) as c_int }
fn futex(addr: *mut c_int, op: c_int, val: c_int, val2: usize) -> c_int {
unsafe { syscall!(FUTEX, addr, op, val, val2, 0, 0) as c_int }
}
fn futimens(fd: c_int, times: *const timespec) -> c_int {
+1 -1
View File
@@ -69,7 +69,7 @@ pub trait Pal {
fn ftruncate(fildes: c_int, length: off_t) -> c_int;
fn futex(addr: *mut c_int, op: c_int, val: c_int) -> c_int;
fn futex(addr: *mut c_int, op: c_int, val: c_int, val2: usize) -> c_int;
fn futimens(fd: c_int, times: *const timespec) -> c_int;
+10 -3
View File
@@ -346,8 +346,15 @@ pub unsafe extern "C" fn pte_osSemaphorePend(
handle: pte_osSemaphoreHandle,
pTimeout: *mut c_uint,
) -> pte_osResult {
//TODO: pTimeout
(*handle).wait();
let timeout_opt = if ! pTimeout.is_null() {
let timeout = *pTimeout as i64;
let tv_sec = timeout / 1000;
let tv_nsec = (timeout % 1000) * 1000000;
Some(timespec { tv_sec, tv_nsec })
} else {
None
};
(*handle).wait(timeout_opt.as_ref());
PTE_OS_OK
}
@@ -356,7 +363,7 @@ pub unsafe extern "C" fn pte_osSemaphoreCancellablePend(
handle: pte_osSemaphoreHandle,
pTimeout: *mut c_uint,
) -> pte_osResult {
//TODO
//TODO: thread cancel
pte_osSemaphorePend(handle, pTimeout)
}
+2 -2
View File
@@ -446,13 +446,13 @@ impl Pal for Sys {
e(syscall::ftruncate(fd as usize, len as usize)) as c_int
}
fn futex(addr: *mut c_int, op: c_int, val: c_int) -> c_int {
fn futex(addr: *mut c_int, op: c_int, val: c_int, val2: usize) -> c_int {
match unsafe {
syscall::futex(
addr as *mut i32,
op as usize,
val as i32,
0,
val2,
ptr::null_mut(),
)
} {
+7 -3
View File
@@ -8,6 +8,7 @@ pub use self::{
semaphore::Semaphore,
};
use crate::header::time::timespec;
use crate::platform::{types::*, Pal, Sys};
use core::{
cell::UnsafeCell,
@@ -37,22 +38,25 @@ impl AtomicLock {
}
}
pub fn notify_one(&self) {
Sys::futex(unsafe { &mut *self.atomic.get() }.get_mut(), FUTEX_WAKE, 1);
Sys::futex(unsafe { &mut *self.atomic.get() }.get_mut(), FUTEX_WAKE, 1, 0);
}
pub fn notify_all(&self) {
Sys::futex(
unsafe { &mut *self.atomic.get() }.get_mut(),
FUTEX_WAKE,
c_int::max_value(),
0
);
}
pub fn wait_if(&self, value: c_int) {
pub fn wait_if(&self, value: c_int, timeout_opt: Option<&timespec>) {
Sys::futex(
unsafe { &mut *self.atomic.get() }.get_mut(),
FUTEX_WAIT,
value,
timeout_opt.map_or(0, |timeout| timeout as *const timespec as usize)
);
}
/// A general way to efficiently wait for what might be a long time, using two closures:
///
/// - `attempt` = Attempt to modify the atomic value to any
@@ -103,7 +107,7 @@ impl AtomicLock {
// wait informed us that we might be done waiting
mark_long(self) != AttemptStatus::Desired
{
self.wait_if(long);
self.wait_if(long, None);
}
previous = attempt(self);
+3 -2
View File
@@ -2,6 +2,7 @@
//TODO: improve implementation
use super::AtomicLock;
use crate::header::time::timespec;
use crate::platform::{types::*, Pal, Sys};
use core::sync::atomic::Ordering;
@@ -21,7 +22,7 @@ impl Semaphore {
self.lock.notify_one();
}
pub fn wait(&self) {
pub fn wait(&self, timeout_opt: Option<&timespec>) {
let mut value = 1;
loop {
@@ -38,7 +39,7 @@ impl Semaphore {
}
if value == 0 {
self.lock.wait_if(0);
self.lock.wait_if(0, timeout_opt);
value = 1;
}
}