Skip to content

Commit f35c36a

Browse files
committed
Auto merge of rust-lang#3135 - RalfJung:nonatomic-clock, r=RalfJung
avoid AtomicU64 when a Cell is enough
2 parents f6be93f + b53c34f commit f35c36a

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/tools/miri/src/clock.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::atomic::{AtomicU64, Ordering};
1+
use std::cell::Cell;
22
use std::time::{Duration, Instant as StdInstant};
33

44
/// When using a virtual clock, this defines how many nanoseconds we pretend are passing for each
@@ -59,7 +59,7 @@ enum ClockKind {
5959
},
6060
Virtual {
6161
/// The "current virtual time".
62-
nanoseconds: AtomicU64,
62+
nanoseconds: Cell<u64>,
6363
},
6464
}
6565

@@ -82,7 +82,7 @@ impl Clock {
8282
// Time will pass without us doing anything.
8383
}
8484
ClockKind::Virtual { nanoseconds } => {
85-
nanoseconds.fetch_add(NANOSECONDS_PER_BASIC_BLOCK, Ordering::SeqCst);
85+
nanoseconds.update(|x| x + NANOSECONDS_PER_BASIC_BLOCK);
8686
}
8787
}
8888
}
@@ -93,7 +93,8 @@ impl Clock {
9393
ClockKind::Host { .. } => std::thread::sleep(duration),
9494
ClockKind::Virtual { nanoseconds } => {
9595
// Just pretend that we have slept for some time.
96-
nanoseconds.fetch_add(duration.as_nanos().try_into().unwrap(), Ordering::SeqCst);
96+
let nanos: u64 = duration.as_nanos().try_into().unwrap();
97+
nanoseconds.update(|x| x + nanos);
9798
}
9899
}
99100
}
@@ -110,9 +111,7 @@ impl Clock {
110111
match &self.kind {
111112
ClockKind::Host { .. } => Instant { kind: InstantKind::Host(StdInstant::now()) },
112113
ClockKind::Virtual { nanoseconds } =>
113-
Instant {
114-
kind: InstantKind::Virtual { nanoseconds: nanoseconds.load(Ordering::SeqCst) },
115-
},
114+
Instant { kind: InstantKind::Virtual { nanoseconds: nanoseconds.get() } },
116115
}
117116
}
118117
}

src/tools/miri/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(rustc_private)]
2+
#![feature(cell_update)]
23
#![feature(float_gamma)]
34
#![feature(map_try_insert)]
45
#![feature(never_type)]

0 commit comments

Comments
 (0)