@@ -8,10 +8,11 @@ use crate::*;
8
8
9
9
const NANOSECOND_PER_BASIC_BLOCK : u64 = 10 ;
10
10
11
+ /// A monotone clock used for `Instant` simulation.
11
12
#[ derive( Debug ) ]
12
13
pub enum Clock {
13
14
Host {
14
- /// The "time anchor" for this machine's monotone clock (for `Instant` simulation) .
15
+ /// The "time anchor" for this machine's monotone clock.
15
16
time_anchor : Instant ,
16
17
} ,
17
18
Virtual {
@@ -21,6 +22,7 @@ pub enum Clock {
21
22
}
22
23
23
24
impl Clock {
25
+ /// Create a new clock based on the availability of communication with the host.
24
26
pub fn new ( communicate : bool ) -> Self {
25
27
if communicate {
26
28
Self :: Host { time_anchor : Instant :: now ( ) }
@@ -29,6 +31,7 @@ impl Clock {
29
31
}
30
32
}
31
33
34
+ /// Get the current time relative to this clock.
32
35
pub fn get ( & self ) -> Duration {
33
36
match self {
34
37
Self :: Host { time_anchor } => Instant :: now ( ) . saturating_duration_since ( * time_anchor) ,
@@ -37,6 +40,7 @@ impl Clock {
37
40
}
38
41
}
39
42
43
+ /// Let the time pass for a small interval.
40
44
pub fn tick ( & self ) {
41
45
match self {
42
46
Self :: Host { .. } => {
@@ -48,6 +52,7 @@ impl Clock {
48
52
}
49
53
}
50
54
55
+ /// Sleep for the desired duration.
51
56
pub fn sleep ( & self , duration : Duration ) {
52
57
match self {
53
58
Self :: Host { .. } => std:: thread:: sleep ( duration) ,
@@ -58,7 +63,7 @@ impl Clock {
58
63
}
59
64
}
60
65
61
-
66
+ /// Compute `now + duration` relative to this clock.
62
67
pub fn checked_add_since_now ( & self , duration : Duration ) -> Option < Time > {
63
68
match self {
64
69
Self :: Host { .. } => Instant :: now ( ) . checked_add ( duration) . map ( Time :: Monotonic ) ,
@@ -70,6 +75,8 @@ impl Clock {
70
75
}
71
76
}
72
77
78
+ /// Compute `start + duration` relative to this clock where `start` is the instant of time when
79
+ /// this clock was created.
73
80
pub fn checked_add_since_start ( & self , duration : Duration ) -> Option < Time > {
74
81
match self {
75
82
Self :: Host { time_anchor } => time_anchor. checked_add ( duration) . map ( Time :: Monotonic ) ,
@@ -78,6 +85,7 @@ impl Clock {
78
85
}
79
86
}
80
87
88
+ /// Assert that this clock is a virtual one and get the current time in nanoseconds.
81
89
pub ( crate ) fn assert_virtual ( & self ) -> & AtomicU64 {
82
90
match self {
83
91
Clock :: Host { .. } => panic ! ( ) ,
0 commit comments