-
Notifications
You must be signed in to change notification settings - Fork 135
Process aliases #2027
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
Open
mat-hek
wants to merge
2
commits into
atomvm:main
Choose a base branch
from
mat-hek:mf/upstream-aliases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Process aliases #2027
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,6 +260,7 @@ void context_destroy(Context *ctx) | |
| case CONTEXT_MONITOR_MONITORED_LOCAL: | ||
| case CONTEXT_MONITOR_MONITORING_LOCAL: | ||
| case CONTEXT_MONITOR_MONITORING_LOCAL_REGISTEREDNAME: | ||
| case CONTEXT_MONITOR_ALIAS: | ||
| UNREACHABLE(); | ||
| } | ||
| } | ||
|
|
@@ -774,15 +775,15 @@ static struct Monitor *context_monitors_handle_terminate(Context *ctx) | |
| Context *target = globalcontext_get_process_nolock(glb, local_process_id); | ||
| // Target cannot be NULL as we processed Demonitor signals | ||
| assert(target != NULL); | ||
| int required_terms = REF_SIZE + TUPLE_SIZE(5); | ||
| int required_terms = TERM_BOXED_PROCESS_REF_SIZE + TUPLE_SIZE(5); | ||
| if (UNLIKELY(memory_ensure_free(ctx, required_terms) != MEMORY_GC_OK)) { | ||
| // TODO: handle out of memory here | ||
| fprintf(stderr, "Cannot handle out of memory.\n"); | ||
| globalcontext_get_process_unlock(glb, target); | ||
| AVM_ABORT(); | ||
| } | ||
| // Prepare the message on ctx's heap which will be freed afterwards. | ||
| term ref = term_from_ref_ticks(monitored_monitor->ref_ticks, &ctx->heap); | ||
| term ref = term_make_process_reference(target->process_id, monitored_monitor->ref_ticks, &ctx->heap); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not what OTP does and there probably is a good reason. |
||
|
|
||
| term port_or_process = term_pid_or_port_from_context(ctx); | ||
| term port_or_process_atom | ||
|
|
@@ -799,6 +800,9 @@ static struct Monitor *context_monitors_handle_terminate(Context *ctx) | |
| free(monitor); | ||
| break; | ||
| } | ||
| case CONTEXT_MONITOR_ALIAS: { | ||
| free(monitor); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
|
|
@@ -869,6 +873,19 @@ struct Monitor *monitor_registeredname_monitor_new(int32_t monitor_process_id, t | |
| return &monitor->monitor; | ||
| } | ||
|
|
||
| struct Monitor *monitor_alias_new(uint64_t ref_ticks, enum ContextMonitorAliasType alias_type) | ||
| { | ||
| struct MonitorAlias *monitor = malloc(sizeof(struct MonitorAlias)); | ||
| if (IS_NULL_PTR(monitor)) { | ||
| return NULL; | ||
| } | ||
| monitor->monitor.monitor_type = CONTEXT_MONITOR_ALIAS; | ||
| monitor->ref_ticks = ref_ticks; | ||
| monitor->alias_type = alias_type; | ||
|
|
||
| return &monitor->monitor; | ||
| } | ||
|
|
||
| struct Monitor *monitor_resource_monitor_new(void *resource, uint64_t ref_ticks) | ||
| { | ||
| struct ResourceContextMonitor *monitor = malloc(sizeof(struct ResourceContextMonitor)); | ||
|
|
@@ -920,6 +937,16 @@ bool context_add_monitor(Context *ctx, struct Monitor *new_monitor) | |
| } | ||
| break; | ||
| } | ||
| case CONTEXT_MONITOR_ALIAS: { | ||
| struct MonitorAlias *new_alias_monitor = CONTAINER_OF(new_monitor, struct MonitorAlias, monitor); | ||
| struct MonitorAlias *existing_alias_monitor = CONTAINER_OF(existing, struct MonitorAlias, monitor); | ||
|
|
||
| if (UNLIKELY(existing_alias_monitor->alias_type == new_alias_monitor->alias_type && existing_alias_monitor->ref_ticks == new_alias_monitor->ref_ticks)) { | ||
| free(new_monitor); | ||
| return false; | ||
| } | ||
| break; | ||
| } | ||
| case CONTEXT_MONITOR_RESOURCE: { | ||
| struct ResourceContextMonitor *new_resource_monitor = CONTAINER_OF(new_monitor, struct ResourceContextMonitor, monitor); | ||
| struct ResourceContextMonitor *existing_resource_monitor = CONTAINER_OF(existing, struct ResourceContextMonitor, monitor); | ||
|
|
@@ -1053,6 +1080,11 @@ void context_unlink_ack(Context *ctx, term link_pid, uint64_t unlink_id) | |
|
|
||
| void context_demonitor(Context *ctx, uint64_t ref_ticks) | ||
| { | ||
| struct MonitorAlias *alias = context_find_alias(ctx, ref_ticks); | ||
| if (alias != NULL && alias->alias_type != ContextMonitorAliasExplicitUnalias) { | ||
| context_unalias(alias); | ||
| } | ||
|
|
||
| struct ListHead *item; | ||
| LIST_FOR_EACH (item, &ctx->monitors_head) { | ||
| struct Monitor *monitor = GET_LIST_ENTRY(item, struct Monitor, monitor_list_head); | ||
|
|
@@ -1086,11 +1118,36 @@ void context_demonitor(Context *ctx, uint64_t ref_ticks) | |
| } | ||
| case CONTEXT_MONITOR_LINK_LOCAL: | ||
| case CONTEXT_MONITOR_LINK_REMOTE: | ||
| case CONTEXT_MONITOR_ALIAS: | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct MonitorAlias *context_find_alias(Context *ctx, uint64_t ref_ticks) | ||
| { | ||
| struct ListHead *item; | ||
| LIST_FOR_EACH (item, &ctx->monitors_head) { | ||
| struct Monitor *monitor = GET_LIST_ENTRY(item, struct Monitor, monitor_list_head); | ||
| if (monitor->monitor_type == CONTEXT_MONITOR_ALIAS) { | ||
| struct MonitorAlias *alias_monitor = CONTAINER_OF(monitor, struct MonitorAlias, monitor); | ||
| if (alias_monitor->ref_ticks == ref_ticks) { | ||
| return alias_monitor; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| void context_unalias(struct MonitorAlias *alias) | ||
| { | ||
| TERM_DEBUG_ASSERT(alias != NULL); | ||
| struct Monitor *monitor = &alias->monitor; | ||
| list_remove(&monitor->monitor_list_head); | ||
| free(monitor); | ||
| } | ||
|
|
||
| term context_get_monitor_pid(Context *ctx, uint64_t ref_ticks, bool *is_monitoring) | ||
| { | ||
| struct ListHead *item; | ||
|
|
@@ -1117,6 +1174,7 @@ term context_get_monitor_pid(Context *ctx, uint64_t ref_ticks, bool *is_monitori | |
| case CONTEXT_MONITOR_LINK_LOCAL: | ||
| case CONTEXT_MONITOR_LINK_REMOTE: | ||
| case CONTEXT_MONITOR_RESOURCE: | ||
| case CONTEXT_MONITOR_ALIAS: | ||
| break; | ||
| } | ||
| } | ||
|
|
@@ -1246,6 +1304,12 @@ COLD_FUNC void context_dump(Context *ctx) | |
| fprintf(stderr, "\n"); | ||
| break; | ||
| } | ||
| case CONTEXT_MONITOR_ALIAS: { | ||
| struct MonitorLocalMonitor *monitored_monitor = CONTAINER_OF(monitor, struct MonitorLocalMonitor, monitor); | ||
| fprintf(stderr, "has alias ref=%lu", (long unsigned) monitored_monitor->ref_ticks); | ||
| fprintf(stderr, "\n"); | ||
| break; | ||
| } | ||
| case CONTEXT_MONITOR_MONITORED_LOCAL: { | ||
| struct MonitorLocalMonitor *monitored_monitor = CONTAINER_OF(monitor, struct MonitorLocalMonitor, monitor); | ||
| fprintf(stderr, "monitored by "); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not enforced by style guide but usually we don't quote atoms when it's not required.