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

External messaging. #1807

Merged
merged 13 commits into from May 13, 2024
Merged

External messaging. #1807

merged 13 commits into from May 13, 2024

Conversation

floitsch
Copy link
Member

No description provided.

@cla-bot cla-bot bot added the cla-signed The contributors have signed the CLA label Sep 13, 2023
@floitsch floitsch marked this pull request as draft September 13, 2023 16:47
// directory of this repository.

/*
* C interface for Toit's messaging API.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should consider just having a single toit.h include file that has all our APIs in there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I called it ctoit.h.
I think it makes sense to split it out again if we ever have more functions (and have a ctoit.h that combines all others). At the moment that's not worth it.
done.


#pragma once

#include <stdint.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need these?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


void toit_register_external_message_handler(void* user_context,
int requested_pid,
void (*create_handler)(void* user_context, HandlerContext* handler_context));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd introduce a type for the create handler function. It feels cleaner.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

typedef struct HandlerContext HandlerContext;

void toit_register_external_message_handler(void* user_context,
int requested_pid,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, I think it would be better with a symbolic name here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do that in a follow-up PR.

void (*release)(void* user_context));

bool toit_send_message(HandlerContext* handler_context, int target_pid, int type, void* data, int length, bool free_on_failure);
void toit_release_handler(HandlerContext* handler_context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void toit_release_handler(HandlerContext* handler_context);
void toit_release_handler(HandlerContext* handler_context);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

struct HandlerContext;
typedef struct HandlerContext HandlerContext;

void toit_register_external_message_handler(void* user_context,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add/remove instead of register/release?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

src/messaging.cc Outdated

void on_message(int pid, int type, void* data, int length) override {
if (on_message_ == null) return;
printf("Calling on-message %p\n", user_context_);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the debug printing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

}
list->handler.create_handler(list->handler.user_context, reinterpret_cast<HandlerContext*>(handler));
}
// Free the list.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you keep symbolic names in the list, you should probably not free it here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I allocated a mapping object.

src/run.cc Outdated
@@ -65,6 +66,7 @@ int run_program(SnapshotBundle boot_bundle, SnapshotBundle application_bundle, c
vm.load_platform_event_sources();
ProgramImage boot_image = read_image_from_bundle(boot_bundle);
int group_id = vm.scheduler()->next_group_id();
create_and_start_external_message_handlers(&vm);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it important that you do this after you've gotten the group_id? If not, I'd try to do this before, maybe just after load_platform_event_sources().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@@ -113,6 +114,7 @@ static void start() {
{ VM vm;
vm.load_platform_event_sources();
int group_id = vm.scheduler()->next_group_id();
create_and_start_external_message_handlers(&vm);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again. Do you need the group id here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Member

@kasperl kasperl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no Toit code in this PR?

@floitsch
Copy link
Member Author

Is there no Toit code in this PR?

There is no Toit code here. The example in the envelopes repository has some: toitlang/envelopes#22

@floitsch floitsch marked this pull request as ready for review May 13, 2024 08:19
@floitsch floitsch enabled auto-merge (squash) May 13, 2024 08:20
@floitsch floitsch merged commit 1005c0a into master May 13, 2024
23 of 24 checks passed
@floitsch floitsch deleted the floitsch/external-messaging branch May 13, 2024 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The contributors have signed the CLA
Development

Successfully merging this pull request may close these issues.

None yet

2 participants