Put timespec convert into common fn

This commit is contained in:
Wildan M
2026-01-07 00:07:50 +07:00
parent ba4d789f6e
commit 9789af36f0
4 changed files with 33 additions and 52 deletions
+5 -16
View File
@@ -5,7 +5,7 @@ use crate::{
header::{
errno::{EINVAL, ENOMEM, ETIMEDOUT},
pthread::*,
time::{CLOCK_MONOTONIC, CLOCK_REALTIME, clock_gettime, timespec},
time::{CLOCK_MONOTONIC, CLOCK_REALTIME, timespec, timespec_realtime_to_monotonic},
},
platform::types::clockid_t,
};
@@ -69,24 +69,13 @@ impl Cond {
timeout: &timespec,
clock_id: clockid_t,
) -> Result<(), Errno> {
// adjusted timeout similar in semaphore.rs
let relative = match clock_id {
// FUTEX expect monotonic clock
CLOCK_MONOTONIC => timeout.clone(),
CLOCK_REALTIME => {
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(timeout.clone(), realtime) else {
return Err(Errno(ETIMEDOUT));
};
let Some(relative) = timespec::add(monotonic, delta) else {
return Err(Errno(ENOMEM));
};
relative
}
CLOCK_REALTIME => match timespec_realtime_to_monotonic(timeout.clone()) {
Ok(relative) => relative,
Err(err) => return Err(err),
},
_ => return Err(Errno(EINVAL)),
};
+5 -20
View File
@@ -2,7 +2,7 @@
//TODO: improve implementation
use crate::{
header::time::{CLOCK_MONOTONIC, CLOCK_REALTIME, clock_gettime, timespec},
header::time::{CLOCK_MONOTONIC, CLOCK_REALTIME, timespec, timespec_realtime_to_monotonic},
platform::types::{c_uint, clockid_t},
};
@@ -63,27 +63,12 @@ impl Semaphore {
let relative = match clock_id {
// FUTEX expect monotonic clock
CLOCK_MONOTONIC => timeout.clone(),
CLOCK_REALTIME => {
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(timeout.clone(), realtime) else {
//Timeout happened
return Err(());
};
let Some(relative) = timespec::add(monotonic, delta) else {
return Err(());
};
relative
}
CLOCK_REALTIME => match timespec_realtime_to_monotonic(timeout.clone()) {
Ok(relative) => relative,
Err(_) => return Err(()),
},
_ => return Err(()),
};
crate::sync::futex_wait(&self.count, value, Some(&relative));
} else {
// Use futex to wait for the next change, without a timeout