@@ -6,6 +6,8 @@ use rustc_data_structures::sync::Ordering;
6
6
use crate :: concurrency:: thread:: Time ;
7
7
use crate :: * ;
8
8
9
+ const NANOSECOND_PER_BASIC_BLOCK : u64 = 10 ;
10
+
9
11
#[ derive( Debug ) ]
10
12
pub enum Clock {
11
13
Host {
@@ -37,9 +39,11 @@ impl Clock {
37
39
38
40
pub fn tick ( & self ) {
39
41
match self {
40
- Self :: Host { .. } => ( ) ,
42
+ Self :: Host { .. } => {
43
+ // Time will pass without us doing anything.
44
+ }
41
45
Self :: Virtual { nanoseconds } => {
42
- nanoseconds. fetch_add ( 1 , Ordering :: Relaxed ) ;
46
+ nanoseconds. fetch_add ( NANOSECOND_PER_BASIC_BLOCK , Ordering :: Relaxed ) ;
43
47
}
44
48
}
45
49
}
@@ -48,11 +52,13 @@ impl Clock {
48
52
match self {
49
53
Self :: Host { .. } => std:: thread:: sleep ( duration) ,
50
54
Self :: Virtual { nanoseconds } => {
55
+ // Just pretend that we have slept for some time.
51
56
nanoseconds. fetch_add ( duration. as_nanos ( ) . try_into ( ) . unwrap ( ) , Ordering :: Relaxed ) ;
52
57
}
53
58
}
54
59
}
55
60
61
+
56
62
pub fn checked_add_since_now ( & self , duration : Duration ) -> Option < Time > {
57
63
match self {
58
64
Self :: Host { .. } => Instant :: now ( ) . checked_add ( duration) . map ( Time :: Monotonic ) ,
@@ -291,10 +297,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
291
297
// If adding the duration overflows, let's just sleep for an hour. Waking up early is always acceptable.
292
298
let timeout_time =
293
299
this. machine . clock . checked_add_since_now ( duration) . unwrap_or_else ( || {
294
- this. machine
295
- . clock
296
- . checked_add_since_now ( Duration :: from_secs ( 3600 ) )
297
- . unwrap ( )
300
+ this. machine . clock . checked_add_since_now ( Duration :: from_secs ( 3600 ) ) . unwrap ( )
298
301
} ) ;
299
302
300
303
let active_thread = this. get_active_thread ( ) ;
@@ -321,8 +324,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
321
324
let timeout_ms = this. read_scalar ( timeout) ?. to_u32 ( ) ?;
322
325
323
326
let duration = Duration :: from_millis ( timeout_ms. into ( ) ) ;
324
- let timeout_time =
325
- this. machine . clock . checked_add_since_now ( duration) . unwrap ( ) ;
327
+ let timeout_time = this. machine . clock . checked_add_since_now ( duration) . unwrap ( ) ;
326
328
327
329
let active_thread = this. get_active_thread ( ) ;
328
330
this. block_thread ( active_thread) ;
0 commit comments