Use less cloning for timespec

This commit is contained in:
Wildan M
2026-05-03 03:21:06 +07:00
parent 7b0354ca3d
commit d16dfade59
8 changed files with 13 additions and 15 deletions
+2 -2
View File
@@ -16,7 +16,7 @@ impl timespec {
/// similar logic with timeradd
#[allow(clippy::should_implement_trait)] // not to confuse std::ops::Add
pub fn add(base: timespec, interval: timespec) -> Option<timespec> {
pub fn add(base: &timespec, interval: &timespec) -> Option<timespec> {
let delta_sec = base.tv_sec.checked_add(interval.tv_sec)?;
let delta_nsec = base.tv_nsec.checked_add(interval.tv_nsec)?;
@@ -30,7 +30,7 @@ impl timespec {
})
}
/// similar logic with timersub
pub fn subtract(later: timespec, earlier: timespec) -> Option<timespec> {
pub fn subtract(later: &timespec, earlier: &timespec) -> Option<timespec> {
let delta_sec = later.tv_sec.checked_sub(earlier.tv_sec)?;
let delta_nsec = later.tv_nsec.checked_sub(earlier.tv_nsec)?;
+1 -1
View File
@@ -76,7 +76,7 @@ pub unsafe extern "C" fn pthread_mutex_timedlock(
mutex: *mut pthread_mutex_t,
abstime: &timespec,
) -> c_int {
let relative = match timespec_realtime_to_monotonic(abstime.clone()) {
let relative = match timespec_realtime_to_monotonic(abstime) {
Ok(relative) => relative,
Err(err) => return e(Err(err)),
};
+3 -3
View File
@@ -882,15 +882,15 @@ const fn blank_tm() -> tm {
}
}
pub(crate) fn timespec_realtime_to_monotonic(abstime: timespec) -> Result<timespec, Errno> {
pub(crate) fn timespec_realtime_to_monotonic(abstime: &timespec) -> Result<timespec, Errno> {
let mut realtime = timespec::default();
unsafe { clock_gettime(CLOCK_REALTIME, &raw mut realtime) };
let mut monotonic = timespec::default();
unsafe { clock_gettime(CLOCK_MONOTONIC, &raw mut monotonic) };
let Some(delta) = timespec::subtract(abstime, realtime) else {
let Some(delta) = timespec::subtract(abstime, &realtime) else {
return Err(Errno(ETIMEDOUT));
};
let Some(relative) = timespec::add(monotonic, delta) else {
let Some(relative) = timespec::add(&monotonic, &delta) else {
return Err(Errno(ENOMEM));
};
Ok(relative)
+3 -3
View File
@@ -1354,7 +1354,7 @@ impl Pal for Sys {
Self::clock_gettime(timer_st.clockid, Out::from_mut(&mut now))?;
if timer_st.evp.sigev_notify == SIGEV_NONE {
if timespec::subtract(timer_st.next_wake_time.it_value.clone(), now.clone()).is_none() {
if timespec::subtract(&timer_st.next_wake_time.it_value, &now).is_none() {
// error here means the timer is disarmed
let _ = timer_update_wake_time(timer_st);
}
@@ -1366,7 +1366,7 @@ impl Pal for Sys {
} else {
itimerspec {
it_interval: timer_st.next_wake_time.it_interval.clone(),
it_value: timespec::subtract(timer_st.next_wake_time.it_value.clone(), now)
it_value: timespec::subtract(&timer_st.next_wake_time.it_value, &now)
.unwrap_or_default(),
}
});
@@ -1392,7 +1392,7 @@ impl Pal for Sys {
if flags & TIMER_ABSTIME == 0 {
let mut now = timespec::default();
Self::clock_gettime(timer_st.clockid, Out::from_mut(&mut now))?;
val.it_value = timespec::add(now, val.it_value).ok_or(Errno(EINVAL))?;
val.it_value = timespec::add(&now, &val.it_value).ok_or(Errno(EINVAL))?;
}
val
};
+1 -1
View File
@@ -86,7 +86,7 @@ pub(crate) fn timer_update_wake_time(timer_st: &mut timer_internal_t) -> Result<
} else {
let mut now = timespec::default();
Sys::clock_gettime(timer_st.clockid, Out::from_mut(&mut now))?;
let next_time = match timespec::add(now, timer_st.next_wake_time.it_interval.clone()) {
let next_time = match timespec::add(&now, &timer_st.next_wake_time.it_interval) {
Some(a) => a,
None => timespec::default(),
};
+1 -1
View File
@@ -73,7 +73,7 @@ impl Cond {
let relative = match clock_id {
// FUTEX expect monotonic clock
CLOCK_MONOTONIC => timeout.clone(),
CLOCK_REALTIME => timespec_realtime_to_monotonic(timeout.clone())?,
CLOCK_REALTIME => timespec_realtime_to_monotonic(timeout)?,
_ => return Err(Errno(EINVAL)),
};
+1 -3
View File
@@ -38,9 +38,7 @@ impl InnerRwLock {
let relative = match deadline {
// FUTEX expect monotonic clock
Some((abstime, CLOCK_MONOTONIC)) => Some(abstime.clone()),
Some((abstime, CLOCK_REALTIME)) => {
Some(timespec_realtime_to_monotonic(abstime.clone())?)
}
Some((abstime, CLOCK_REALTIME)) => Some(timespec_realtime_to_monotonic(abstime)?),
None => None,
_ => {
return Err(Errno(EINVAL));
+1 -1
View File
@@ -66,7 +66,7 @@ impl Semaphore {
let relative = match clock_id {
// FUTEX expect monotonic clock
CLOCK_MONOTONIC => timeout.clone(),
CLOCK_REALTIME => match timespec_realtime_to_monotonic(timeout.clone()) {
CLOCK_REALTIME => match timespec_realtime_to_monotonic(timeout) {
Ok(relative) => relative,
Err(_) => return Err(()),
},