Merge branch 'sem-timedwait' into 'master'

Add sem_timedwait and sem_clockwait

See merge request redox-os/relibc!696
This commit is contained in:
Jeremy Soller
2025-08-28 06:24:27 -06:00
3 changed files with 28 additions and 5 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
sys_includes = []
sys_includes = ["time.h"]
include_guard = "_RELIBC_SEMAPHORE_H"
language = "C"
style = "Type"
+25 -2
View File
@@ -2,7 +2,10 @@
//!
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/semaphore.h.html>.
use crate::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 +81,27 @@ 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
}
/// 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), CLOCK_REALTIME);
0
}
+2 -2
View File
@@ -51,7 +51,7 @@ impl Semaphore {
}
}
pub fn wait(&self, timeout_opt: Option<&timespec>) -> Result<(), ()> {
pub fn wait(&self, timeout_opt: Option<&timespec>, 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)
{