1- use std:: sync :: atomic :: { AtomicU64 , Ordering } ;
1+ use std:: cell :: Cell ;
22use std:: time:: { Duration , Instant as StdInstant } ;
33
44/// When using a virtual clock, this defines how many nanoseconds we pretend are passing for each
@@ -59,7 +59,7 @@ enum ClockKind {
5959 } ,
6060 Virtual {
6161 /// The "current virtual time".
62- nanoseconds : AtomicU64 ,
62+ nanoseconds : Cell < u64 > ,
6363 } ,
6464}
6565
@@ -82,7 +82,7 @@ impl Clock {
8282 // Time will pass without us doing anything.
8383 }
8484 ClockKind :: Virtual { nanoseconds } => {
85- nanoseconds. fetch_add ( NANOSECONDS_PER_BASIC_BLOCK , Ordering :: SeqCst ) ;
85+ nanoseconds. update ( |x| x + NANOSECONDS_PER_BASIC_BLOCK ) ;
8686 }
8787 }
8888 }
@@ -93,7 +93,8 @@ impl Clock {
9393 ClockKind :: Host { .. } => std:: thread:: sleep ( duration) ,
9494 ClockKind :: Virtual { nanoseconds } => {
9595 // 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) ;
9798 }
9899 }
99100 }
@@ -110,9 +111,7 @@ impl Clock {
110111 match & self . kind {
111112 ClockKind :: Host { .. } => Instant { kind : InstantKind :: Host ( StdInstant :: now ( ) ) } ,
112113 ClockKind :: Virtual { nanoseconds } =>
113- Instant {
114- kind : InstantKind :: Virtual { nanoseconds : nanoseconds. load ( Ordering :: SeqCst ) } ,
115- } ,
114+ Instant { kind : InstantKind :: Virtual { nanoseconds : nanoseconds. get ( ) } } ,
116115 }
117116 }
118117}
0 commit comments