Add sem_clockwait and fix timedwait clock_id
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
//!
|
||||
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/semaphore.h.html>.
|
||||
|
||||
use crate::{header::time::timespec, platform::types::*};
|
||||
use crate::{header::time::{timespec, CLOCK_MONOTONIC, CLOCK_REALTIME}, platform::types::*};
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/semaphore.h.html>.
|
||||
// TODO: Statically verify size and align
|
||||
@@ -78,7 +78,15 @@ pub unsafe extern "C" fn sem_unlink(name: *const c_char) -> c_int {
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_trywait.html>.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sem_wait(sem: *mut sem_t) -> c_int {
|
||||
get(sem).wait(None);
|
||||
get(sem).wait(None, CLOCK_MONOTONIC);
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_clockwait.html>.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sem_clockwait(sem: *mut sem_t, clock_id: clockid_t, abstime: *const timespec) -> c_int {
|
||||
get(sem).wait(Some(&*abstime), clock_id);
|
||||
|
||||
0
|
||||
}
|
||||
@@ -86,7 +94,7 @@ pub unsafe extern "C" fn sem_wait(sem: *mut sem_t) -> c_int {
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sem_timedwait.html>.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sem_timedwait(sem: *mut sem_t, abstime: *const timespec) -> c_int {
|
||||
get(sem).wait(Some(&*abstime));
|
||||
get(sem).wait(Some(&*abstime), CLOCK_REALTIME);
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ impl Semaphore {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wait(&self, timeout_opt: Option<×pec>) -> Result<(), ()> {
|
||||
pub fn wait(&self, timeout_opt: Option<×pec>, clock_id: clockid_t) -> Result<(), ()> {
|
||||
loop {
|
||||
let value = self.try_wait();
|
||||
|
||||
@@ -61,7 +61,7 @@ impl Semaphore {
|
||||
|
||||
if let Some(timeout) = timeout_opt {
|
||||
let mut time = timespec::default();
|
||||
unsafe { clock_gettime(CLOCK_MONOTONIC, &mut time) };
|
||||
unsafe { clock_gettime(clock_id, &mut time) };
|
||||
if (time.tv_sec > timeout.tv_sec)
|
||||
|| (time.tv_sec == timeout.tv_sec && time.tv_nsec >= timeout.tv_nsec)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user