Auto merge of #3135 - RalfJung:nonatomic-clock, r=RalfJung

avoid AtomicU64 when a Cell is enough
This commit is contained in:
bors 2023-10-22 07:20:06 +00:00
commit f35c36af19
2 changed files with 7 additions and 7 deletions

View File

@ -1,4 +1,4 @@
use std::sync::atomic::{AtomicU64, Ordering};
use std::cell::Cell;
use std::time::{Duration, Instant as StdInstant};
/// When using a virtual clock, this defines how many nanoseconds we pretend are passing for each
@ -59,7 +59,7 @@ enum ClockKind {
},
Virtual {
/// The "current virtual time".
nanoseconds: AtomicU64,
nanoseconds: Cell<u64>,
},
}
@ -82,7 +82,7 @@ pub fn tick(&self) {
// Time will pass without us doing anything.
}
ClockKind::Virtual { nanoseconds } => {
nanoseconds.fetch_add(NANOSECONDS_PER_BASIC_BLOCK, Ordering::SeqCst);
nanoseconds.update(|x| x + NANOSECONDS_PER_BASIC_BLOCK);
}
}
}
@ -93,7 +93,8 @@ pub fn sleep(&self, duration: Duration) {
ClockKind::Host { .. } => std::thread::sleep(duration),
ClockKind::Virtual { nanoseconds } => {
// Just pretend that we have slept for some time.
nanoseconds.fetch_add(duration.as_nanos().try_into().unwrap(), Ordering::SeqCst);
let nanos: u64 = duration.as_nanos().try_into().unwrap();
nanoseconds.update(|x| x + nanos);
}
}
}
@ -110,9 +111,7 @@ pub fn now(&self) -> Instant {
match &self.kind {
ClockKind::Host { .. } => Instant { kind: InstantKind::Host(StdInstant::now()) },
ClockKind::Virtual { nanoseconds } =>
Instant {
kind: InstantKind::Virtual { nanoseconds: nanoseconds.load(Ordering::SeqCst) },
},
Instant { kind: InstantKind::Virtual { nanoseconds: nanoseconds.get() } },
}
}
}

View File

@ -1,4 +1,5 @@
#![feature(rustc_private)]
#![feature(cell_update)]
#![feature(float_gamma)]
#![feature(map_try_insert)]
#![feature(never_type)]