From 80ea3bc6a104c8fa2460d9fa7ab32b4328a26ac9 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Thu, 28 Aug 2025 12:58:54 +0700 Subject: [PATCH] Add sem_clockwait and fix timedwait clock_id --- src/header/semaphore/mod.rs | 14 +++++++++++--- src/sync/semaphore.rs | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/header/semaphore/mod.rs b/src/header/semaphore/mod.rs index b5137d54a7..db43eccd46 100644 --- a/src/header/semaphore/mod.rs +++ b/src/header/semaphore/mod.rs @@ -2,7 +2,7 @@ //! //! See . -use crate::{header::time::timespec, platform::types::*}; +use crate::{header::time::{timespec, CLOCK_MONOTONIC, CLOCK_REALTIME}, platform::types::*}; /// See . // TODO: Statically verify size and align @@ -78,7 +78,15 @@ pub unsafe extern "C" fn sem_unlink(name: *const c_char) -> c_int { /// See . #[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 . +#[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 . #[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 } diff --git a/src/sync/semaphore.rs b/src/sync/semaphore.rs index 34a1283b05..eee6ba90b6 100644 --- a/src/sync/semaphore.rs +++ b/src/sync/semaphore.rs @@ -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) {