Skip to content

Commit

Permalink
:erlang.monotonic_time/0
Browse files Browse the repository at this point in the history
  • Loading branch information
KronicDeth committed Apr 26, 2019
1 parent ab4ae36 commit 493d595
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 14 deletions.
6 changes: 5 additions & 1 deletion lumen_runtime/src/otp/erlang.rs
Expand Up @@ -23,7 +23,7 @@ use crate::registry::{self, Registered};
use crate::send::{self, send, Sent};
use crate::stacktrace;
use crate::term::{Tag, Tag::*, Term};
use crate::time;
use crate::time::{self, monotonic, Unit::*};
use crate::tuple::{Tuple, ZeroBasedIndex};

#[cfg(test)]
Expand Down Expand Up @@ -1051,6 +1051,10 @@ pub fn min_2(term1: Term, term2: Term) -> Term {
term1.min(term2)
}

pub fn monotonic_time_0(process: &Process) -> Term {
monotonic::time(Native).into_process(process)
}

/// `*/2` infix operator
pub fn multiply_2(multiplier: Term, multiplicand: Term, process: &Process) -> Result {
number_infix_operator!(multiplier, multiplicand, process, checked_mul, *)
Expand Down
1 change: 1 addition & 0 deletions lumen_runtime/src/otp/erlang/tests.rs
Expand Up @@ -82,6 +82,7 @@ mod map_get_2;
mod map_size_1;
mod max_2;
mod min_2;
mod monotonic_time_0;
mod multiply_2;
mod negate_1;
mod node_0;
Expand Down
2 changes: 1 addition & 1 deletion lumen_runtime/src/otp/erlang/tests/max_2.rs
@@ -1,4 +1,4 @@
use super::FirstSecond::*;
use super::FirstSecond::{First, Second};
use super::*;

mod with_atom_first;
Expand Down
2 changes: 1 addition & 1 deletion lumen_runtime/src/otp/erlang/tests/min_2.rs
@@ -1,4 +1,4 @@
use super::FirstSecond::*;
use super::FirstSecond::{First, Second};
use super::*;

mod with_atom_first;
Expand Down
17 changes: 17 additions & 0 deletions lumen_runtime/src/otp/erlang/tests/monotonic_time_0.rs
@@ -0,0 +1,17 @@
use super::*;

use std::thread;
use std::time::Duration;

#[test]
fn increases_after_2_native_time_units() {
with_process(|process| {
let first = erlang::monotonic_time_0(process);

thread::sleep(Duration::from_millis(2));

let second = erlang::monotonic_time_0(process);

assert_eq!(erlang::is_less_than_2(first, second), true.into())
});
}
12 changes: 1 addition & 11 deletions lumen_runtime/src/system/time.rs
@@ -1,10 +1,5 @@
#![allow(dead_code)]
use lazy_static::lazy_static;
use std::time::{Duration, Instant, SystemTime};

lazy_static! {
static ref SYSTEM_START_TIME: Instant = { Instant::now() };
}
use std::time::{Duration, SystemTime};

pub struct ErlangTimestamp {
pub megaseconds: u32,
Expand Down Expand Up @@ -32,8 +27,3 @@ pub fn system_time() -> Duration {
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Unable to get system time!")
}

#[inline]
pub fn monotonic_time() -> Duration {
Instant::now().duration_since(*SYSTEM_START_TIME)
}
2 changes: 2 additions & 0 deletions lumen_runtime/src/time.rs
Expand Up @@ -7,6 +7,8 @@ use crate::exception::Exception;
use crate::integer::big;
use crate::term::{Tag::*, Term};

pub mod monotonic;

pub fn convert(time: BigInt, from_unit: Unit, to_unit: Unit) -> BigInt {
if from_unit == to_unit {
time
Expand Down
22 changes: 22 additions & 0 deletions lumen_runtime/src/time/monotonic.rs
@@ -0,0 +1,22 @@
use std::time::Instant;

use num_bigint::BigInt;

use crate::time::convert;
use crate::time::Unit::{self, *};

pub fn time(unit: Unit) -> BigInt {
let duration = START.elapsed();

match unit {
Second => duration.as_secs().into(),
Millisecond => duration.as_millis().into(),
Microsecond => duration.as_micros().into(),
Nanosecond => duration.as_nanos().into(),
_ => convert(duration.as_nanos().into(), Nanosecond, unit),
}
}

lazy_static! {
static ref START: Instant = Instant::now();
}

0 comments on commit 493d595

Please sign in to comment.