Skip to content

Commit c119c44

Browse files
committed
add NANOSECONDS_PER_BASIC_BLOCK and comments
1 parent 13ac242 commit c119c44

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/shims/time.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use rustc_data_structures::sync::Ordering;
66
use crate::concurrency::thread::Time;
77
use crate::*;
88

9+
const NANOSECOND_PER_BASIC_BLOCK: u64 = 10;
10+
911
#[derive(Debug)]
1012
pub enum Clock {
1113
Host {
@@ -37,9 +39,11 @@ impl Clock {
3739

3840
pub fn tick(&self) {
3941
match self {
40-
Self::Host { .. } => (),
42+
Self::Host { .. } => {
43+
// Time will pass without us doing anything.
44+
}
4145
Self::Virtual { nanoseconds } => {
42-
nanoseconds.fetch_add(1, Ordering::Relaxed);
46+
nanoseconds.fetch_add(NANOSECOND_PER_BASIC_BLOCK, Ordering::Relaxed);
4347
}
4448
}
4549
}
@@ -48,11 +52,13 @@ impl Clock {
4852
match self {
4953
Self::Host { .. } => std::thread::sleep(duration),
5054
Self::Virtual { nanoseconds } => {
55+
// Just pretend that we have slept for some time.
5156
nanoseconds.fetch_add(duration.as_nanos().try_into().unwrap(), Ordering::Relaxed);
5257
}
5358
}
5459
}
5560

61+
5662
pub fn checked_add_since_now(&self, duration: Duration) -> Option<Time> {
5763
match self {
5864
Self::Host { .. } => Instant::now().checked_add(duration).map(Time::Monotonic),
@@ -291,10 +297,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
291297
// If adding the duration overflows, let's just sleep for an hour. Waking up early is always acceptable.
292298
let timeout_time =
293299
this.machine.clock.checked_add_since_now(duration).unwrap_or_else(|| {
294-
this.machine
295-
.clock
296-
.checked_add_since_now(Duration::from_secs(3600))
297-
.unwrap()
300+
this.machine.clock.checked_add_since_now(Duration::from_secs(3600)).unwrap()
298301
});
299302

300303
let active_thread = this.get_active_thread();
@@ -321,8 +324,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
321324
let timeout_ms = this.read_scalar(timeout)?.to_u32()?;
322325

323326
let duration = Duration::from_millis(timeout_ms.into());
324-
let timeout_time =
325-
this.machine.clock.checked_add_since_now(duration).unwrap();
327+
let timeout_time = this.machine.clock.checked_add_since_now(duration).unwrap();
326328

327329
let active_thread = this.get_active_thread();
328330
this.block_thread(active_thread);

0 commit comments

Comments
 (0)