Skip to content

Commit 22752e5

Browse files
committed
document Time::Virtual
1 parent 8b7960d commit 22752e5

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/concurrency/thread.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,12 @@ impl<'mir, 'tcx> Thread<'mir, 'tcx> {
188188
#[derive(Debug)]
189189
pub enum Time {
190190
Monotonic(Instant),
191-
Virtual(u64, Arc<AtomicU64>),
191+
Virtual {
192+
/// The instant for this moment.
193+
instant: u64,
194+
/// A reference to the time anchor used to read the current global virtual time.
195+
time_anchor: Arc<AtomicU64>,
196+
},
192197
RealTime(SystemTime),
193198
}
194199

@@ -197,8 +202,8 @@ impl Time {
197202
fn get_wait_time(&self) -> Duration {
198203
match self {
199204
Time::Monotonic(instant) => instant.saturating_duration_since(Instant::now()),
200-
Time::Virtual(nanoseconds, now) =>
201-
Duration::from_nanos(nanoseconds - now.load(Ordering::Relaxed)),
205+
Time::Virtual { instant, time_anchor } =>
206+
Duration::from_nanos(instant - time_anchor.load(Ordering::Relaxed)),
202207
Time::RealTime(time) =>
203208
time.duration_since(SystemTime::now()).unwrap_or(Duration::new(0, 0)),
204209
}

src/shims/time.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,22 @@ impl TimeAnchor {
4646
pub fn checked_add_since_now(&self, duration: Duration) -> Option<Time> {
4747
match self {
4848
Self::Host(_) => Instant::now().checked_add(duration).map(Time::Monotonic),
49-
Self::Virtual(nanoseconds) =>
50-
nanoseconds
49+
Self::Virtual(time_anchor) =>
50+
time_anchor
5151
.load(Ordering::Relaxed)
5252
.checked_add(duration.as_nanos().try_into().unwrap())
53-
.map(|duration| Time::Virtual(duration, nanoseconds.clone())),
53+
.map(|instant| Time::Virtual { instant, time_anchor: time_anchor.clone() }),
5454
}
5555
}
5656

5757
pub fn checked_add_since_start(&self, duration: Duration) -> Option<Time> {
5858
match self {
5959
Self::Host(instant) => instant.checked_add(duration).map(Time::Monotonic),
60-
Self::Virtual(now) =>
61-
Some(Time::Virtual(duration.as_nanos().try_into().unwrap(), now.clone())),
60+
Self::Virtual(time_anchor) =>
61+
Some(Time::Virtual {
62+
instant: duration.as_nanos().try_into().unwrap(),
63+
time_anchor: time_anchor.clone(),
64+
}),
6265
}
6366
}
6467
}

0 commit comments

Comments
 (0)