Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add atomvm:get_creation/0 #1418

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for external pids and encoded pids in external terms
- Added support for external refs and encoded refs in external terms
- Introduce ports to represent native processes and added support for external ports and encoded ports in external terms
- Added `atomvm:get_creation/0`, equivalent to `erts_internal:get_creation/0`

## [0.6.6] - Unreleased

Expand Down
8 changes: 7 additions & 1 deletion libs/eavmlib/src/atomvm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
posix_clock_settime/2,
posix_opendir/1,
posix_closedir/1,
posix_readdir/1
posix_readdir/1,
get_creation/0
]).

-export_type([
Expand Down Expand Up @@ -335,3 +336,8 @@ posix_closedir(_Dir) ->
{ok, {dirent, Inode :: integer(), Name :: binary()}} | eof | {error, posix_error()}.
posix_readdir(_Dir) ->
erlang:nif_error(undefined).

%% @hidden
-spec get_creation() -> non_neg_integer().
get_creation() ->
erlang:nif_error(undefined).
2 changes: 1 addition & 1 deletion libs/estdlib/src/erlang.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,6 @@ localtime() ->
erlang:nif_error(undefined).

%% @hidden
-spec setnode(atom(), pos_integer()) -> true.
-spec setnode(atom(), non_neg_integer()) -> true.
setnode(_NodeName, _Creation) ->
erlang:nif_error(undefined).
27 changes: 23 additions & 4 deletions src/libAtomVM/nifs.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ static term nif_erlang_function_exported(Context *ctx, int argc, term argv[]);
static term nif_erlang_garbage_collect(Context *ctx, int argc, term argv[]);
static term nif_erlang_group_leader(Context *ctx, int argc, term argv[]);
static term nif_erlang_get_module_info(Context *ctx, int argc, term argv[]);
static term nif_erlang_setnode(Context *ctx, int argc, term argv[]);
static term nif_erlang_setnode_2(Context *ctx, int argc, term argv[]);
static term nif_erlang_memory(Context *ctx, int argc, term argv[]);
static term nif_erlang_monitor(Context *ctx, int argc, term argv[]);
static term nif_erlang_demonitor(Context *ctx, int argc, term argv[]);
Expand All @@ -177,6 +177,7 @@ static term nif_atomvm_add_avm_pack_file(Context *ctx, int argc, term argv[]);
static term nif_atomvm_close_avm_pack(Context *ctx, int argc, term argv[]);
static term nif_atomvm_get_start_beam(Context *ctx, int argc, term argv[]);
static term nif_atomvm_read_priv(Context *ctx, int argc, term argv[]);
static term nif_atomvm_get_creation(Context *ctx, int argc, term argv[]);
static term nif_console_print(Context *ctx, int argc, term argv[]);
static term nif_base64_encode(Context *ctx, int argc, term argv[]);
static term nif_base64_decode(Context *ctx, int argc, term argv[]);
Expand Down Expand Up @@ -669,10 +670,10 @@ static const struct Nif get_module_info_nif =
.nif_ptr = nif_erlang_get_module_info
};

static const struct Nif setnode_nif =
static const struct Nif setnode_2_nif =
{
.base.type = NIFFunctionType,
.nif_ptr = nif_erlang_setnode
.nif_ptr = nif_erlang_setnode_2
};

static const struct Nif raise_nif =
Expand Down Expand Up @@ -736,6 +737,11 @@ static const struct Nif atomvm_read_priv_nif =
.base.type = NIFFunctionType,
.nif_ptr = nif_atomvm_read_priv
};
static const struct Nif atomvm_get_creation_nif =
{
.base.type = NIFFunctionType,
.nif_ptr = nif_atomvm_get_creation
};
static const struct Nif console_print_nif =
{
.base.type = NIFFunctionType,
Expand Down Expand Up @@ -3975,7 +3981,7 @@ static term nif_erlang_get_module_info(Context *ctx, int argc, term argv[])
return result;
}

static term nif_erlang_setnode(Context *ctx, int argc, term argv[])
static term nif_erlang_setnode_2(Context *ctx, int argc, term argv[])
{
UNUSED(argc);

Expand Down Expand Up @@ -4343,6 +4349,19 @@ static term nif_atomvm_read_priv(Context *ctx, int argc, term argv[])
return result;
}

// AtomVM extension, equivalent to erts_internal:get_creation/0
static term nif_atomvm_get_creation(Context *ctx, int argc, term argv[])
{
UNUSED(argc);
UNUSED(argv);

if (UNLIKELY(memory_ensure_free_opt(ctx, term_boxed_integer_size(ctx->global->creation), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}

return term_make_maybe_boxed_int64(ctx->global->creation, &ctx->heap);
}

static term nif_console_print(Context *ctx, int argc, term argv[])
{
UNUSED(argc);
Expand Down
3 changes: 2 additions & 1 deletion src/libAtomVM/nifs.gperf
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ erlang:group_leader/0, &group_leader_nif
erlang:group_leader/2, &group_leader_nif
erlang:get_module_info/1, &get_module_info_nif
erlang:get_module_info/2, &get_module_info_nif
erlang:setnode/2, &setnode_nif
erlang:setnode/2, &setnode_2_nif
erts_debug:flat_size/1, &flat_size_nif
ets:new/2, &ets_new_nif
ets:insert/2, &ets_insert_nif
Expand All @@ -154,6 +154,7 @@ atomvm:posix_clock_settime/2, IF_HAVE_CLOCK_SETTIME_OR_SETTIMEOFDAY(&atomvm_posi
atomvm:posix_opendir/1, IF_HAVE_OPENDIR_READDIR_CLOSEDIR(&atomvm_posix_opendir_nif)
atomvm:posix_closedir/1, IF_HAVE_OPENDIR_READDIR_CLOSEDIR(&atomvm_posix_closedir_nif)
atomvm:posix_readdir/1, IF_HAVE_OPENDIR_READDIR_CLOSEDIR(&atomvm_posix_readdir_nif)
atomvm:get_creation/0, &atomvm_get_creation_nif
code:load_abs/1, &code_load_abs_nif
code:load_binary/3, &code_load_binary_nif
code:all_available/0, &code_all_available_nif
Expand Down
9 changes: 9 additions & 0 deletions tests/erlang_tests/test_node.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ test_node_distribution() ->
),
register(net_kernel, NetKernelPid),
true = erlang:setnode(test@test_node, 42),
42 = get_creation(),
test@test_node = node(),
NetKernelPid ! quit,
ok =
Expand Down Expand Up @@ -74,6 +75,14 @@ has_setnode_creation() ->
OTPRelease >= "23"
end.

get_creation() ->
case erlang:system_info(machine) of
"ATOM" ->
atomvm:get_creation();
"BEAM" ->
erts_internal:get_creation()
end.

sleep(Ms) ->
receive
after Ms -> ok
Expand Down
Loading