Skip to content

Commit

Permalink
Merge #159
Browse files Browse the repository at this point in the history
159: Make 'ticks' be a monotonic microsecond timer. r=vext01 a=ltratt

As specified in SOM-st/SOM#46.

Co-authored-by: Laurence Tratt <[email protected]>
  • Loading branch information
bors[bot] and ltratt authored Jun 17, 2020
2 parents 69c3da0 + c2fffb5 commit 77627a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
10 changes: 9 additions & 1 deletion lang_tests/ticks.som
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
"
VM:
status: success
stdout: Integer
stdout:
Integer
"

ticks = (
run = (
| i start |
system ticks class println.
start := system ticks.
i := 1000.
[i ~= 0] whileTrue: [
i := i - 1.
(start <= system ticks) ifFalse: [ system exit: 1 ].
].
)
)
23 changes: 12 additions & 11 deletions src/lib/vm/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
path::{Path, PathBuf},
process,
str::FromStr,
time::{SystemTime, UNIX_EPOCH},
time::Instant,
};

use lrpar::Span;
Expand Down Expand Up @@ -92,6 +92,7 @@ pub struct VM {
symbols: Vec<Val>,
reverse_symbols: HashMap<String, usize>,
frames: Vec<Frame>,
time_at_start: Instant,
}

impl VM {
Expand Down Expand Up @@ -140,6 +141,7 @@ impl VM {
symbols: Vec::new(),
reverse_symbols: HashMap::new(),
frames: Vec::new(),
time_at_start: Instant::now(),
};

// These two phases must be executed in the correct order.
Expand Down Expand Up @@ -1034,16 +1036,14 @@ impl VM {
SendReturn::Val
}
Primitive::Ticks => {
match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(d) => {
let t = d.as_micros();
const_assert!(size_of::<usize>() <= size_of::<u128>());
if t > usize::max_value() as u128 {
todo!();
} else {
let v = Val::from_usize(self, t as usize);
self.stack.push(v);
}
const_assert!(size_of::<usize>() <= size_of::<u128>());
let t = Instant::now()
.duration_since(self.time_at_start)
.as_micros();
match usize::try_from(t) {
Ok(t) => {
let v = Val::from_usize(self, t as usize);
self.stack.push(v);
}
Err(_) => todo!(),
}
Expand Down Expand Up @@ -1462,6 +1462,7 @@ impl VM {
symbols: Vec::new(),
reverse_symbols: HashMap::new(),
frames: Vec::new(),
time_at_start: Instant::now(),
}
}
}
Expand Down

0 comments on commit 77627a4

Please sign in to comment.