Skip to content

Commit bc631c6

Browse files
committed
Add erlang:monotonic_time/0
Signed-off-by: Peter M <petermm@gmail.com>
1 parent df09ed8 commit bc631c6

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/libAtomVM/nifs.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ static term nif_erlang_float_to_list(Context *ctx, int argc, term argv[]);
127127
static term nif_erlang_list_to_binary_1(Context *ctx, int argc, term argv[]);
128128
static term nif_erlang_list_to_integer(Context *ctx, int argc, term argv[]);
129129
static term nif_erlang_list_to_float_1(Context *ctx, int argc, term argv[]);
130+
static term nif_erlang_monotonic_time_0(Context *ctx, int argc, term argv[]);
130131
static term nif_erlang_monotonic_time_1(Context *ctx, int argc, term argv[]);
131132
static term nif_erlang_iolist_size_1(Context *ctx, int argc, term argv[]);
132133
static term nif_erlang_iolist_to_binary_1(Context *ctx, int argc, term argv[]);
@@ -499,7 +500,13 @@ static const struct Nif concat_nif =
499500
.nif_ptr = nif_erlang_concat_2
500501
};
501502

502-
static const struct Nif monotonic_time_nif =
503+
static const struct Nif monotonic_time_0_nif =
504+
{
505+
.base.type = NIFFunctionType,
506+
.nif_ptr = nif_erlang_monotonic_time_0
507+
};
508+
509+
static const struct Nif monotonic_time_1_nif =
503510
{
504511
.base.type = NIFFunctionType,
505512
.nif_ptr = nif_erlang_monotonic_time_1
@@ -1626,6 +1633,18 @@ term nif_erlang_make_ref_0(Context *ctx, int argc, term argv[])
16261633
return term_from_ref_ticks(ref_ticks, &ctx->heap);
16271634
}
16281635

1636+
term nif_erlang_monotonic_time_0(Context *ctx, int argc, term argv[])
1637+
{
1638+
UNUSED(argc);
1639+
UNUSED(argv);
1640+
1641+
struct timespec ts;
1642+
sys_monotonic_time(&ts);
1643+
1644+
// Return nanoseconds as the native unit
1645+
return make_maybe_boxed_int64(ctx, ((int64_t) ts.tv_sec) * 1000000000UL + ts.tv_nsec);
1646+
}
1647+
16291648
term nif_erlang_monotonic_time_1(Context *ctx, int argc, term argv[])
16301649
{
16311650
UNUSED(ctx);

src/libAtomVM/nifs.gperf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ erlang:system_flag/2, &system_flag_nif
100100
erlang:whereis/1, &whereis_nif
101101
erlang:++/2, &concat_nif
102102
erlang:--/2, &erlang_lists_subtract_nif
103-
erlang:monotonic_time/1, &monotonic_time_nif
103+
erlang:monotonic_time/0, &monotonic_time_0_nif
104+
erlang:monotonic_time/1, &monotonic_time_1_nif
104105
erlang:system_time/1, &system_time_nif
105106
erlang:tuple_to_list/1, &tuple_to_list_nif
106107
erlang:universaltime/0, &universaltime_nif

tests/erlang_tests/test_monotonic_time.erl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,24 @@
2323
-export([start/0]).
2424

2525
start() ->
26+
% Test monotonic_time/1 with millisecond
2627
T1 = erlang:monotonic_time(millisecond),
2728
receive
2829
after 1 -> ok
2930
end,
3031
T2 = erlang:monotonic_time(millisecond),
31-
test_diff(T2 - T1).
32-
33-
test_diff(X) when is_integer(X) andalso X >= 0 ->
34-
1;
35-
test_diff(X) when X < 0 ->
36-
0.
32+
Diff1 = T2 - T1,
33+
34+
% Test monotonic_time/0 (native nanoseconds)
35+
T3 = erlang:monotonic_time(),
36+
receive
37+
after 1 -> ok
38+
end,
39+
T4 = erlang:monotonic_time(),
40+
Diff2 = T4 - T3,
41+
42+
% Both tests must pass: millisecond diff >= 0, nanosecond diff >= 1000
43+
case {is_integer(Diff1) andalso Diff1 >= 0, is_integer(Diff2) andalso Diff2 >= 1000} of
44+
{true, true} -> 1;
45+
_ -> 0
46+
end.

0 commit comments

Comments
 (0)