1
1
use std:: sync:: atomic:: AtomicU64 ;
2
- use std:: time:: { Duration , Instant , SystemTime } ;
2
+ use std:: time:: { Duration , Instant as StdInstant , SystemTime } ;
3
3
4
4
use rustc_data_structures:: sync:: Ordering ;
5
5
@@ -10,12 +10,18 @@ use crate::*;
10
10
/// are passing for each basic block.
11
11
const NANOSECOND_PER_BASIC_BLOCK : u64 = 10 ;
12
12
13
+ #[ derive( Debug ) ]
14
+ pub enum Instant {
15
+ Host ( StdInstant ) ,
16
+ Virtual { nanoseconds : u64 } ,
17
+ }
18
+
13
19
/// A monotone clock used for `Instant` simulation.
14
20
#[ derive( Debug ) ]
15
21
pub enum Clock {
16
22
Host {
17
23
/// The "time anchor" for this machine's monotone clock.
18
- time_anchor : Instant ,
24
+ time_anchor : StdInstant ,
19
25
} ,
20
26
Virtual {
21
27
/// The "current virtual time".
@@ -27,7 +33,7 @@ impl Clock {
27
33
/// Create a new clock based on the availability of communication with the host.
28
34
pub fn new ( communicate : bool ) -> Self {
29
35
if communicate {
30
- Self :: Host { time_anchor : Instant :: now ( ) }
36
+ Self :: Host { time_anchor : StdInstant :: now ( ) }
31
37
} else {
32
38
Self :: Virtual { nanoseconds : 0 . into ( ) }
33
39
}
@@ -36,7 +42,7 @@ impl Clock {
36
42
/// Get the current time relative to this clock.
37
43
pub fn get ( & self ) -> Duration {
38
44
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) ,
40
46
Self :: Virtual { nanoseconds } =>
41
47
Duration :: from_nanos ( nanoseconds. load ( Ordering :: Relaxed ) ) ,
42
48
}
@@ -68,30 +74,48 @@ impl Clock {
68
74
/// Compute `now + duration` relative to this clock.
69
75
pub fn get_time_relative ( & self , duration : Duration ) -> Option < Time > {
70
76
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) ) ) ,
72
81
Self :: Virtual { nanoseconds } =>
73
82
nanoseconds
74
83
. load ( Ordering :: Relaxed )
75
84
. checked_add ( duration. as_nanos ( ) . try_into ( ) . unwrap ( ) )
76
- . map ( |nanoseconds| Time :: Virtual { nanoseconds } ) ,
85
+ . map ( |nanoseconds| Time :: Monotonic ( Instant :: Virtual { nanoseconds } ) ) ,
77
86
}
78
87
}
79
88
80
89
/// Compute `start + duration` relative to this clock where `start` is the instant of time when
81
90
/// this clock was created.
82
91
pub fn get_time_absolute ( & self , duration : Duration ) -> Option < Time > {
83
92
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) ) ) ,
85
97
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
+ } ) ) ,
87
101
}
88
102
}
89
103
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 ) ) ,
95
119
}
96
120
}
97
121
}
0 commit comments