Skip to content

Commit

Permalink
Auto merge of rust-lang#3135 - RalfJung:nonatomic-clock, r=RalfJung
Browse files Browse the repository at this point in the history
avoid AtomicU64 when a Cell is enough
  • Loading branch information
bors committed Oct 22, 2023
2 parents f6be93f + b53c34f commit f35c36a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/tools/miri/src/clock.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -59,7 +59,7 @@ enum ClockKind {
},
Virtual {
/// The "current virtual time".
nanoseconds: AtomicU64,
nanoseconds: Cell<u64>,
},
}

Expand All @@ -82,7 +82,7 @@ impl Clock {
// 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);
}
}
}
Expand All @@ -93,7 +93,8 @@ impl Clock {
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);
}
}
}
Expand All @@ -110,9 +111,7 @@ impl Clock {
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() } },
}
}
}
1 change: 1 addition & 0 deletions src/tools/miri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(rustc_private)]
#![feature(cell_update)]
#![feature(float_gamma)]
#![feature(map_try_insert)]
#![feature(never_type)]
Expand Down

0 comments on commit f35c36a

Please sign in to comment.