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

Introduce ports (local and external) #1365

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion libs/estdlib/src/erlang.erl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@
term_to_binary/1,
timestamp/0,
universaltime/0,
localtime/0
localtime/0,
setnode/2
]).

-export_type([
Expand Down Expand Up @@ -1254,3 +1255,8 @@ universaltime() ->
-spec localtime() -> calendar:datetime().
localtime() ->
erlang:nif_error(undefined).

%% @hidden
-spec setnode(atom(), pos_integer()) -> true.
setnode(_NodeName, _Creation) ->
erlang:nif_error(undefined).
14 changes: 14 additions & 0 deletions src/libAtomVM/bif.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ term bif_erlang_self_0(Context *ctx)
return term_from_local_process_id(ctx->process_id);
}

term bif_erlang_node_0(Context *ctx)
{
return ctx->global->node_name;
}

term bif_erlang_byte_size_1(Context *ctx, uint32_t fail_label, int live, term arg1)
{
UNUSED(live);
Expand Down Expand Up @@ -181,6 +186,15 @@ term bif_erlang_is_pid_1(Context *ctx, uint32_t fail_label, term arg1)
return term_is_pid(arg1) ? TRUE_ATOM : FALSE_ATOM;
}

// Generated by OTP-21 compiler
term bif_erlang_is_port_1(Context *ctx, uint32_t fail_label, term arg1)
{
UNUSED(ctx);
UNUSED(fail_label);

return term_is_port(arg1) ? TRUE_ATOM : FALSE_ATOM;
}

term bif_erlang_is_reference_1(Context *ctx, uint32_t fail_label, term arg1)
{
UNUSED(ctx);
Expand Down
2 changes: 2 additions & 0 deletions src/libAtomVM/bif.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern "C" {
const struct ExportedFunction *bif_registry_get_handler(AtomString module, AtomString function, int arity);

term bif_erlang_self_0(Context *ctx);
term bif_erlang_node_0(Context *ctx);
term bif_erlang_byte_size_1(Context *ctx, uint32_t fail_label, int live, term arg1);
term bif_erlang_bit_size_1(Context *ctx, uint32_t fail_label, int live, term arg1);
term bif_erlang_length_1(Context *ctx, uint32_t fail_label, int live, term arg1);
Expand All @@ -55,6 +56,7 @@ term bif_erlang_is_integer_1(Context *ctx, uint32_t fail_label, term arg1);
term bif_erlang_is_list_1(Context *ctx, uint32_t fail_label, term arg1);
term bif_erlang_is_number_1(Context *ctx, uint32_t fail_label, term arg1);
term bif_erlang_is_pid_1(Context *ctx, uint32_t fail_label, term arg1);
term bif_erlang_is_port_1(Context *ctx, uint32_t fail_label, term arg1);
term bif_erlang_is_reference_1(Context *ctx, uint32_t fail_label, term arg1);
term bif_erlang_is_tuple_1(Context *ctx, uint32_t fail_label, term arg1);
term bif_erlang_is_map_1(Context *ctx, uint32_t fail_label, term arg1);
Expand Down
2 changes: 2 additions & 0 deletions src/libAtomVM/bifs.gperf
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct BifNameAndPtr
};
%%
erlang:self/0, {.bif.base.type = BIFFunctionType, .bif.bif0_ptr = bif_erlang_self_0}
erlang:node/0, {.bif.base.type = BIFFunctionType, .bif.bif0_ptr = bif_erlang_node_0}
erlang:length/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_length_1}
erlang:byte_size/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_byte_size_1}
erlang:bit_size/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_bit_size_1}
Expand All @@ -50,6 +51,7 @@ erlang:is_integer/1, {.bif.base.type = BIFFunctionType, .bif.bif1_ptr = bif_erla
erlang:is_list/1, {.bif.base.type = BIFFunctionType, .bif.bif1_ptr = bif_erlang_is_list_1}
erlang:is_number/1, {.bif.base.type = BIFFunctionType, .bif.bif1_ptr = bif_erlang_is_number_1}
erlang:is_pid/1, {.bif.base.type = BIFFunctionType, .bif.bif1_ptr = bif_erlang_is_pid_1}
erlang:is_port/1, {.bif.base.type = BIFFunctionType, .bif.bif1_ptr = bif_erlang_is_port_1}
erlang:is_reference/1, {.bif.base.type = BIFFunctionType, .bif.bif1_ptr = bif_erlang_is_reference_1}
erlang:is_tuple/1, {.bif.base.type = BIFFunctionType, .bif.bif1_ptr = bif_erlang_is_tuple_1}
erlang:is_map/1, {.bif.base.type = BIFFunctionType, .bif.bif1_ptr = bif_erlang_is_map_1}
Expand Down
19 changes: 17 additions & 2 deletions src/libAtomVM/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#define BYTES_PER_TERM (TERM_BITS / 8)

static struct ResourceMonitor *context_monitors_handle_terminate(Context *ctx);
static void context_distribution_handle_terminate(Context *ctx);
static void destroy_extended_registers(Context *ctx, unsigned int live);

Context *context_new(GlobalContext *glb)
Expand Down Expand Up @@ -132,6 +133,11 @@ void context_destroy(Context *ctx)
// Ensure process is not registered
globalcontext_maybe_unregister_process_id(ctx->global, ctx->process_id);

// Handle distribution termination
if (UNLIKELY(ctx->flags & Distribution)) {
context_distribution_handle_terminate(ctx);
}

// When monitor message is sent, process is no longer in the table
// and is no longer registered either.
struct ResourceMonitor *resource_monitor = context_monitors_handle_terminate(ctx);
Expand Down Expand Up @@ -225,7 +231,7 @@ void context_process_flush_monitor_signal(Context *ctx, uint64_t ref_ticks, bool
if (term_is_tuple(msg)
&& term_get_tuple_arity(msg) == 5
&& term_get_tuple_element(msg, 0) == DOWN_ATOM
&& term_is_reference(term_get_tuple_element(msg, 1))
&& term_is_local_reference(term_get_tuple_element(msg, 1))
&& term_to_ref_ticks(term_get_tuple_element(msg, 1)) == ref_ticks) {
mailbox_remove_message(&ctx->mailbox, &ctx->heap);
// If option info is combined with option flush, false is returned if a flush was needed, otherwise true.
Expand Down Expand Up @@ -434,10 +440,11 @@ static struct ResourceMonitor *context_monitors_handle_terminate(Context *ctx)
term_put_tuple_element(info_tuple, 1, ref);
if (ctx->native_handler != NULL) {
term_put_tuple_element(info_tuple, 2, PORT_ATOM);
term_put_tuple_element(info_tuple, 3, term_port_from_local_process_id(ctx->process_id));
} else {
term_put_tuple_element(info_tuple, 2, PROCESS_ATOM);
term_put_tuple_element(info_tuple, 3, term_from_local_process_id(ctx->process_id));
}
term_put_tuple_element(info_tuple, 3, term_from_local_process_id(ctx->process_id));
term_put_tuple_element(info_tuple, 4, ctx->exit_reason);

mailbox_send(target, info_tuple);
Expand All @@ -448,6 +455,14 @@ static struct ResourceMonitor *context_monitors_handle_terminate(Context *ctx)
return result;
}

static void context_distribution_handle_terminate(Context *ctx)
{
// For now, the only process with Distribution flag set is net_kernel.
GlobalContext *glb = ctx->global;
glb->node_name = NONODE_AT_NOHOST_ATOM;
glb->creation = 0;
}

int context_link(Context *ctx, term link_pid)
{
struct ListHead *item;
Expand Down
1 change: 1 addition & 0 deletions src/libAtomVM/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ enum ContextFlags
Ready = 8,
Killed = 16,
Trap = 32,
Distribution = 64,
};

enum HeapGrowthStrategy
Expand Down
6 changes: 6 additions & 0 deletions src/libAtomVM/defaultatoms.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ static const char *const unicode_atom = "\x7" "unicode";

static const char *const global_atom = "\x6" "global";

static const char *const nonode_at_nohost_atom = "\xD" "nonode@nohost";
static const char *const net_kernel_atom = "\xA" "net_kernel";

void defaultatoms_init(GlobalContext *glb)
{
int ok = 1;
Expand Down Expand Up @@ -308,6 +311,9 @@ void defaultatoms_init(GlobalContext *glb)

ok &= globalcontext_insert_atom(glb, global_atom) == GLOBAL_ATOM_INDEX;

ok &= globalcontext_insert_atom(glb, nonode_at_nohost_atom) == NONODE_AT_NOHOST_ATOM_INDEX;
ok &= globalcontext_insert_atom(glb, net_kernel_atom) == NET_KERNEL_ATOM_INDEX;

if (!ok) {
AVM_ABORT();
}
Expand Down
8 changes: 7 additions & 1 deletion src/libAtomVM/defaultatoms.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ extern "C" {

#define GLOBAL_ATOM_INDEX 111

#define PLATFORM_ATOMS_BASE_INDEX 112
#define NONODE_AT_NOHOST_ATOM_INDEX 112
#define NET_KERNEL_ATOM_INDEX 113

#define PLATFORM_ATOMS_BASE_INDEX 114

#define FALSE_ATOM TERM_FROM_ATOM_INDEX(FALSE_ATOM_INDEX)
#define TRUE_ATOM TERM_FROM_ATOM_INDEX(TRUE_ATOM_INDEX)
Expand Down Expand Up @@ -317,6 +320,9 @@ extern "C" {

#define GLOBAL_ATOM TERM_FROM_ATOM_INDEX(GLOBAL_ATOM_INDEX)

#define NONODE_AT_NOHOST_ATOM TERM_FROM_ATOM_INDEX(NONODE_AT_NOHOST_ATOM_INDEX)
#define NET_KERNEL_ATOM TERM_FROM_ATOM_INDEX(NET_KERNEL_ATOM_INDEX)

void defaultatoms_init(GlobalContext *glb);

void platform_defaultatoms_init(GlobalContext *glb);
Expand Down
4 changes: 2 additions & 2 deletions src/libAtomVM/ets_hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ static uint32_t hash_term_incr(term t, int32_t h, GlobalContext *global)
return hash_integer(t, h, global);
} else if (term_is_float(t)) {
return hash_float(t, h, global);
} else if (term_is_pid(t)) {
} else if (term_is_local_pid(t)) {
return hash_pid(t, h, global);
} else if (term_is_reference(t)) {
} else if (term_is_local_reference(t)) {
return hash_reference(t, h, global);
} else if (term_is_binary(t)) {
return hash_binary(t, h, global);
Expand Down
Loading
Loading