From ba4d789f6ea81de67709b4bbdb7b16ee67ff2779 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Tue, 6 Jan 2026 23:33:00 +0700 Subject: [PATCH] Calculate pthread_mutex_timedlock clock --- src/header/pthread/mutex.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/header/pthread/mutex.rs b/src/header/pthread/mutex.rs index 92cacffb41..33311aeaf5 100644 --- a/src/header/pthread/mutex.rs +++ b/src/header/pthread/mutex.rs @@ -1,6 +1,9 @@ use super::*; -use crate::error::Errno; +use crate::{ + error::Errno, + header::errno::{EINVAL, ENOMEM, ETIMEDOUT}, +}; // PTHREAD_MUTEX_INITIALIZER is defined in bits_pthread/cbindgen.toml @@ -72,9 +75,25 @@ pub unsafe extern "C" fn pthread_mutex_setprioceiling( #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_mutex_timedlock( mutex: *mut pthread_mutex_t, - timespec: *const timespec, + abstime: *const timespec, ) -> c_int { - e((&*mutex.cast::()).lock_with_timeout(&*timespec)) + // convert REALTIME_CLOCK to MONOTONIC_CLOCK + + let relative = { + let mut realtime = timespec::default(); + unsafe { clock_gettime(CLOCK_REALTIME, &mut realtime) }; + let mut monotonic = timespec::default(); + unsafe { clock_gettime(CLOCK_MONOTONIC, &mut monotonic) }; + let Some(delta) = timespec::subtract(*abstime.clone(), realtime) else { + return e(Err(Errno(ETIMEDOUT))); + }; + let Some(relative) = timespec::add(monotonic, delta) else { + return e(Err(Errno(ENOMEM))); + }; + relative + }; + + e((&*mutex.cast::()).lock_with_timeout(&relative)) } #[unsafe(no_mangle)] pub unsafe extern "C" fn pthread_mutex_trylock(mutex: *mut pthread_mutex_t) -> c_int {