Convert u128 to nanosecond

This commit is contained in:
tiif 2024-06-07 22:59:36 +08:00
parent 844450ae3a
commit 0bca4e1a22
2 changed files with 8 additions and 12 deletions

View File

@ -39,15 +39,11 @@ impl Instant {
InstantKind::Virtual { nanoseconds },
InstantKind::Virtual { nanoseconds: earlier },
) => {
// Trade some nanosecond precision to prevent as much overflow as possible.
let duration = match u64::try_from(
// Manually convert from nanosecond to millisecond.
// If it exceeded u64::MAX millisecond, we will just use u64::MAX millisecond,
// Duration can't take in more than u64::MAX millisecond.
nanoseconds.saturating_sub(earlier).saturating_div(1_000_000),
) {
Ok(millisecond) => Duration::from_millis(millisecond),
_ => Duration::from_millis(u64::MAX),
// If it exceeded u64::MAX nanosecond, we will just keep u64::MAX nanosecond,
// Duration can't take in more than u64::MAX.
let duration = match u64::try_from(nanoseconds.saturating_sub(earlier)) {
Ok(nanosecond) => Duration::from_nanos(nanosecond),
Err(_err) => Duration::from_nanos(u64::MAX),
};
Duration::new(duration.as_secs(), duration.subsec_nanos())
}
@ -104,7 +100,7 @@ impl Clock {
ClockKind::Host { .. } => std::thread::sleep(duration),
ClockKind::Virtual { nanoseconds } => {
// Just pretend that we have slept for some time.
let nanos: u128 = duration.as_nanos().try_into().unwrap();
let nanos: u128 = duration.as_nanos();
nanoseconds.update(|x| x + nanos);
}
}

View File

@ -280,7 +280,7 @@ fn concurrent_wait_wake() {
assert!(woken > 0 && woken < rounds);
}
// Reproduce of https://github.com/rust-lang/miri/issues/3647.
// Reproduce https://github.com/rust-lang/miri/issues/3647. This should not ICE.
fn large_timeout() {
let futex: i32 = 123;
@ -296,7 +296,6 @@ fn large_timeout() {
}
fn main() {
large_timeout();
wake_nobody();
wake_dangling();
wait_wrong_val();
@ -305,4 +304,5 @@ fn main() {
wait_wake();
wait_wake_bitset();
concurrent_wait_wake();
large_timeout();
}