From 973ac667e34be7eebd94231a235b4177d9a9cdf4 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Sat, 30 May 2026 00:22:21 +0700 Subject: [PATCH] Use syscall time helper functions --- src/context/timeout.rs | 2 +- src/scheme/time.rs | 5 +---- src/syscall/futex.rs | 5 +---- src/syscall/time.rs | 17 ++++------------- 4 files changed, 7 insertions(+), 22 deletions(-) diff --git a/src/context/timeout.rs b/src/context/timeout.rs index 5191a35792..1387f42e7f 100644 --- a/src/context/timeout.rs +++ b/src/context/timeout.rs @@ -44,7 +44,7 @@ pub fn register( scheme_id, event_id, clock, - time: (time.tv_sec as u128 * time::NANOS_PER_SEC) + (time.tv_nsec as u128), + time: time.to_nanos(), }); } diff --git a/src/scheme/time.rs b/src/scheme/time.rs index 4ec3aed320..2a76a6d843 100644 --- a/src/scheme/time.rs +++ b/src/scheme/time.rs @@ -157,10 +157,7 @@ impl KernelScheme for TimeScheme { (CLOCK_MONOTONIC, TimeSchemeKind::ClockGetres) => time::monotonic_resolution(), _ => return Err(Error::new(EINVAL)), }; - let time = TimeSpec { - tv_sec: (arch_time / time::NANOS_PER_SEC) as i64, - tv_nsec: (arch_time % time::NANOS_PER_SEC) as i32, - }; + let time = TimeSpec::from_nanos(arch_time); current_chunk.copy_exactly(&time)?; bytes_read += size_of::(); diff --git a/src/syscall/futex.rs b/src/syscall/futex.rs index 9cc6859d2a..fe8a4bf0bd 100644 --- a/src/syscall/futex.rs +++ b/src/syscall/futex.rs @@ -19,7 +19,6 @@ use crate::{ }, memory::{Page, PhysicalAddress, VirtualAddress}, sync::{CleanLockToken, Mutex, L1}, - time, }; use crate::syscall::{ @@ -154,9 +153,7 @@ pub fn futex( { let mut context = context_lock.write(token.token()); - context.wake = timeout_opt.map(|TimeSpec { tv_sec, tv_nsec }| { - tv_sec as u128 * time::NANOS_PER_SEC + tv_nsec as u128 - }); + context.wake = timeout_opt.map(|time| time.to_nanos()); if let Some((tctl, pctl, _)) = context.sigcontrol() && tctl.currently_pending_unblocked(pctl) != 0 { diff --git a/src/syscall/time.rs b/src/syscall/time.rs index e9f4c2e3ed..e5b64fa0a5 100644 --- a/src/syscall/time.rs +++ b/src/syscall/time.rs @@ -18,10 +18,7 @@ pub fn clock_gettime(clock: usize, buf: UserSliceWo, token: &mut CleanLockToken) _ => return Err(Error::new(EINVAL)), }; - buf.copy_exactly(&TimeSpec { - tv_sec: (arch_time / time::NANOS_PER_SEC) as i64, - tv_nsec: (arch_time % time::NANOS_PER_SEC) as i32, - }) + buf.copy_exactly(&TimeSpec::from_nanos(arch_time)) } /// Nanosleep will sleep by switching the current context @@ -37,7 +34,7 @@ pub fn nanosleep( } let start = time::monotonic(token); - let end = start + (req.tv_sec as u128 * time::NANOS_PER_SEC) + (req.tv_nsec as u128); + let end = start + req.to_nanos(); let current_context = context::current(); { @@ -64,15 +61,9 @@ pub fn nanosleep( rem_buf.copy_exactly(&if current < end { let diff = end - current; - TimeSpec { - tv_sec: (diff / time::NANOS_PER_SEC) as i64, - tv_nsec: (diff % time::NANOS_PER_SEC) as i32, - } + TimeSpec::from_nanos(diff) } else { - TimeSpec { - tv_sec: 0, - tv_nsec: 0, - } + TimeSpec::default() })?; }