From 7ab71a0ad591cd72d7bee512b58a53e08e510ade Mon Sep 17 00:00:00 2001 From: Wildan M Date: Thu, 28 Aug 2025 08:36:16 +0700 Subject: [PATCH 1/3] Add sem_timedwait --- src/header/semaphore/cbindgen.toml | 2 +- src/header/semaphore/mod.rs | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/header/semaphore/cbindgen.toml b/src/header/semaphore/cbindgen.toml index 213064b7ac..1eb7ba4cbf 100644 --- a/src/header/semaphore/cbindgen.toml +++ b/src/header/semaphore/cbindgen.toml @@ -1,4 +1,4 @@ -sys_includes = [] +sys_includes = ["time.h"] include_guard = "_RELIBC_SEMAPHORE_H" language = "C" style = "Type" diff --git a/src/header/semaphore/mod.rs b/src/header/semaphore/mod.rs index 34e4d1fcfb..b5137d54a7 100644 --- a/src/header/semaphore/mod.rs +++ b/src/header/semaphore/mod.rs @@ -2,7 +2,7 @@ //! //! See . -use crate::platform::types::*; +use crate::{header::time::timespec, platform::types::*}; /// See . // TODO: Statically verify size and align @@ -83,6 +83,14 @@ pub unsafe extern "C" fn sem_wait(sem: *mut sem_t) -> c_int { 0 } +/// See . +#[no_mangle] +pub unsafe extern "C" fn sem_timedwait(sem: *mut sem_t, abstime: *const timespec) -> c_int { + get(sem).wait(Some(&*abstime)); + + 0 +} + unsafe fn get<'any>(sem: *mut sem_t) -> &'any RlctSempahore { &*sem.cast() } From 80ea3bc6a104c8fa2460d9fa7ab32b4328a26ac9 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Thu, 28 Aug 2025 12:58:54 +0700 Subject: [PATCH 2/3] 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) { From 4c6c5da8f259721cad9e10235ed859f409029fa8 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Thu, 28 Aug 2025 16:13:16 +0700 Subject: [PATCH 3/3] fix fmt --- src/header/semaphore/mod.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/header/semaphore/mod.rs b/src/header/semaphore/mod.rs index db43eccd46..2565beea6d 100644 --- a/src/header/semaphore/mod.rs +++ b/src/header/semaphore/mod.rs @@ -2,7 +2,10 @@ //! //! See . -use crate::{header::time::{timespec, CLOCK_MONOTONIC, CLOCK_REALTIME}, platform::types::*}; +use crate::{ + header::time::{timespec, CLOCK_MONOTONIC, CLOCK_REALTIME}, + platform::types::*, +}; /// See . // TODO: Statically verify size and align @@ -85,7 +88,11 @@ pub unsafe extern "C" fn sem_wait(sem: *mut sem_t) -> c_int { /// See . #[no_mangle] -pub unsafe extern "C" fn sem_clockwait(sem: *mut sem_t, clock_id: clockid_t, abstime: *const timespec) -> c_int { +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