1
- use std:: sync :: atomic :: { AtomicU64 , Ordering } ;
1
+ use std:: cell :: Cell ;
2
2
use std:: time:: { Duration , Instant as StdInstant } ;
3
3
4
4
/// When using a virtual clock, this defines how many nanoseconds we pretend are passing for each
@@ -59,7 +59,7 @@ enum ClockKind {
59
59
} ,
60
60
Virtual {
61
61
/// The "current virtual time".
62
- nanoseconds : AtomicU64 ,
62
+ nanoseconds : Cell < u64 > ,
63
63
} ,
64
64
}
65
65
@@ -82,7 +82,7 @@ impl Clock {
82
82
// Time will pass without us doing anything.
83
83
}
84
84
ClockKind :: Virtual { nanoseconds } => {
85
- nanoseconds. fetch_add ( NANOSECONDS_PER_BASIC_BLOCK , Ordering :: SeqCst ) ;
85
+ nanoseconds. update ( |x| x + NANOSECONDS_PER_BASIC_BLOCK ) ;
86
86
}
87
87
}
88
88
}
@@ -93,7 +93,8 @@ impl Clock {
93
93
ClockKind :: Host { .. } => std:: thread:: sleep ( duration) ,
94
94
ClockKind :: Virtual { nanoseconds } => {
95
95
// 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) ;
97
98
}
98
99
}
99
100
}
@@ -110,9 +111,7 @@ impl Clock {
110
111
match & self . kind {
111
112
ClockKind :: Host { .. } => Instant { kind : InstantKind :: Host ( StdInstant :: now ( ) ) } ,
112
113
ClockKind :: Virtual { nanoseconds } =>
113
- Instant {
114
- kind : InstantKind :: Virtual { nanoseconds : nanoseconds. load ( Ordering :: SeqCst ) } ,
115
- } ,
114
+ Instant { kind : InstantKind :: Virtual { nanoseconds : nanoseconds. get ( ) } } ,
116
115
}
117
116
}
118
117
}
0 commit comments