11use std:: sync:: atomic:: AtomicU64 ;
2- use std:: time:: { Duration , Instant , SystemTime } ;
2+ use std:: time:: { Duration , Instant as StdInstant , SystemTime } ;
33
44use rustc_data_structures:: sync:: Ordering ;
55
@@ -10,12 +10,18 @@ use crate::*;
1010/// are passing for each basic block.
1111const NANOSECOND_PER_BASIC_BLOCK : u64 = 10 ;
1212
13+ #[ derive( Debug ) ]
14+ pub enum Instant {
15+ Host ( StdInstant ) ,
16+ Virtual { nanoseconds : u64 } ,
17+ }
18+
1319/// A monotone clock used for `Instant` simulation.
1420#[ derive( Debug ) ]
1521pub enum Clock {
1622 Host {
1723 /// The "time anchor" for this machine's monotone clock.
18- time_anchor : Instant ,
24+ time_anchor : StdInstant ,
1925 } ,
2026 Virtual {
2127 /// The "current virtual time".
@@ -27,7 +33,7 @@ impl Clock {
2733 /// Create a new clock based on the availability of communication with the host.
2834 pub fn new ( communicate : bool ) -> Self {
2935 if communicate {
30- Self :: Host { time_anchor : Instant :: now ( ) }
36+ Self :: Host { time_anchor : StdInstant :: now ( ) }
3137 } else {
3238 Self :: Virtual { nanoseconds : 0 . into ( ) }
3339 }
@@ -36,7 +42,7 @@ impl Clock {
3642 /// Get the current time relative to this clock.
3743 pub fn get ( & self ) -> Duration {
3844 match self {
39- Self :: Host { time_anchor } => Instant :: now ( ) . saturating_duration_since ( * time_anchor) ,
45+ Self :: Host { time_anchor } => StdInstant :: now ( ) . saturating_duration_since ( * time_anchor) ,
4046 Self :: Virtual { nanoseconds } =>
4147 Duration :: from_nanos ( nanoseconds. load ( Ordering :: Relaxed ) ) ,
4248 }
@@ -68,30 +74,48 @@ impl Clock {
6874 /// Compute `now + duration` relative to this clock.
6975 pub fn get_time_relative ( & self , duration : Duration ) -> Option < Time > {
7076 match self {
71- Self :: Host { .. } => Instant :: now ( ) . checked_add ( duration) . map ( Time :: Monotonic ) ,
77+ Self :: Host { .. } =>
78+ StdInstant :: now ( )
79+ . checked_add ( duration)
80+ . map ( |instant| Time :: Monotonic ( Instant :: Host ( instant) ) ) ,
7281 Self :: Virtual { nanoseconds } =>
7382 nanoseconds
7483 . load ( Ordering :: Relaxed )
7584 . checked_add ( duration. as_nanos ( ) . try_into ( ) . unwrap ( ) )
76- . map ( |nanoseconds| Time :: Virtual { nanoseconds } ) ,
85+ . map ( |nanoseconds| Time :: Monotonic ( Instant :: Virtual { nanoseconds } ) ) ,
7786 }
7887 }
7988
8089 /// Compute `start + duration` relative to this clock where `start` is the instant of time when
8190 /// this clock was created.
8291 pub fn get_time_absolute ( & self , duration : Duration ) -> Option < Time > {
8392 match self {
84- Self :: Host { time_anchor } => time_anchor. checked_add ( duration) . map ( Time :: Monotonic ) ,
93+ Self :: Host { time_anchor } =>
94+ time_anchor
95+ . checked_add ( duration)
96+ . map ( |instant| Time :: Monotonic ( Instant :: Host ( instant) ) ) ,
8597 Self :: Virtual { .. } =>
86- Some ( Time :: Virtual { nanoseconds : duration. as_nanos ( ) . try_into ( ) . unwrap ( ) } ) ,
98+ Some ( Time :: Monotonic ( Instant :: Virtual {
99+ nanoseconds : duration. as_nanos ( ) . try_into ( ) . unwrap ( ) ,
100+ } ) ) ,
87101 }
88102 }
89103
90- /// Assert that this clock is a virtual one and get the current time in nanoseconds.
91- pub ( crate ) fn assert_virtual ( & self ) -> & AtomicU64 {
92- match self {
93- Clock :: Host { .. } => panic ! ( ) ,
94- Clock :: Virtual { nanoseconds } => nanoseconds,
104+ /// How long do we have to wait from now until the specified time?
105+ pub fn get_wait_time ( & self , time : & Time ) -> Duration {
106+ match time {
107+ Time :: Monotonic ( instant) =>
108+ match ( instant, self ) {
109+ ( Instant :: Host ( instant) , Clock :: Host { .. } ) =>
110+ instant. saturating_duration_since ( StdInstant :: now ( ) ) ,
111+ (
112+ Instant :: Virtual { nanoseconds } ,
113+ Clock :: Virtual { nanoseconds : current_ns } ,
114+ ) => Duration :: from_nanos ( nanoseconds - current_ns. load ( Ordering :: Relaxed ) ) ,
115+ _ => panic ! ( ) ,
116+ } ,
117+ Time :: RealTime ( time) =>
118+ time. duration_since ( SystemTime :: now ( ) ) . unwrap_or ( Duration :: new ( 0 , 0 ) ) ,
95119 }
96120 }
97121}
0 commit comments