Skip to content

Commit ab09ce5

Browse files
committed
NIFs: avoid locks when updating group_leader for itself
Refactor previous commit (de23f00) to avoid locks when not needed. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent de23f00 commit ab09ce5

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/libAtomVM/nifs.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4083,19 +4083,8 @@ static term nif_erlang_group_leader(Context *ctx, int argc, term argv[])
40834083
VALIDATE_VALUE(leader, term_is_pid);
40844084

40854085
int local_process_id = term_to_local_process_id(pid);
4086-
Context *target = globalcontext_get_process_lock(ctx->global, local_process_id);
4087-
if (IS_NULL_PTR(target)) {
4088-
RAISE_ERROR(BADARG_ATOM);
4089-
}
4090-
4091-
if (target != ctx) {
4092-
// always use signals when changing group leader of another context
4093-
// this will avoid any heap access during GC and in general avoids a number
4094-
// of annoying situations
4095-
// this will be async
4096-
mailbox_send_term_signal(target, SetGroupLeaderSignal, leader);
4097-
} else {
4098-
// this will be sync
4086+
if (ctx->process_id == local_process_id) {
4087+
// use a synchronous approach when updating group leader for this process
40994088
if (term_is_local_pid(leader)) {
41004089
ctx->group_leader = leader;
41014090
} else {
@@ -4107,9 +4096,20 @@ static term nif_erlang_group_leader(Context *ctx, int argc, term argv[])
41074096
}
41084097
ctx->group_leader = memory_copy_term_tree(&ctx->heap, leader);
41094098
}
4110-
}
4099+
} else {
4100+
Context *target = globalcontext_get_process_lock(ctx->global, local_process_id);
4101+
if (IS_NULL_PTR(target)) {
4102+
RAISE_ERROR(BADARG_ATOM);
4103+
}
41114104

4112-
globalcontext_get_process_unlock(ctx->global, target);
4105+
// always use signals when changing group leader of another context
4106+
// this will avoid any heap access during GC and in general avoids a number
4107+
// of annoying situations
4108+
// this will be async
4109+
mailbox_send_term_signal(target, SetGroupLeaderSignal, leader);
4110+
4111+
globalcontext_get_process_unlock(ctx->global, target);
4112+
}
41134113
return TRUE_ATOM;
41144114
}
41154115
}

0 commit comments

Comments
 (0)