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..2565beea6d 100644 --- a/src/header/semaphore/mod.rs +++ b/src/header/semaphore/mod.rs @@ -2,7 +2,10 @@ //! //! See . -use crate::platform::types::*; +use crate::{ + header::time::{timespec, CLOCK_MONOTONIC, CLOCK_REALTIME}, + platform::types::*, +}; /// See . // TODO: Statically verify size and align @@ -78,7 +81,27 @@ 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 +} + +/// See . +#[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 } 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) {