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

✨ Allow calling user provided hooks in system daemon #734

Draft
wants to merge 1 commit into
base: develop-pros-4
Choose a base branch
from

Conversation

ion098
Copy link
Contributor

@ion098 ion098 commented Dec 24, 2024

Summary:

Allows calling user provided functions inside of the PROS system daemon.

Motivation:

Allows user code to process sensor data right after vexBackgroundProcessing is called.

Test Plan:

  • check it compiles
  • check that hooks are added and called properly

@Rocky14683
Copy link
Member

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@djava
Copy link
Contributor

djava commented Dec 25, 2024

Can we please get a more detailed motivation for this?

@@ -45,13 +49,27 @@ task_fn_t task_fns[4] = {_opcontrol_task, _autonomous_task, _disabled_task, _com

extern void ser_output_flush(void);

void add_daemon_hook(generic_fn_t hook) {
// TODO: make thread safe
if (unlikely(hook_list == NULL)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not that it matters because this whole function is like, the coldest of paths, but is this even unlikely? It happens at least once per program that uses this function, and it seems at least semi-likely that most programs that use this would only have one.

Not that it matters, but you did add the unlikely so it seems worth questioning.

Comment on lines 66 to 75
static inline void do_background_operations() {
port_mutex_take_all();
ser_output_flush();
rtos_suspend_all();
vexBackgroundProcessing();
rtos_resume_all();
linked_list_foreach(hook_list, &call_hook, NULL);
vdml_background_processing();
port_mutex_give_all();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sold on this order - I frankly don't know what vdml_background_processing is, but it sounds like something that should happen first.

Also, any user code should definitely not be called while all port mutexes are locked, because thats obviously going to deadlock, as every device API function requires locking a port mutex.

I think the linked_list_foreach should be at the end, after port_mutex_give_all().

Comment on lines +55 to +57
typedef void (*generic_fn_t)(void);

void add_daemon_hook(generic_fn_t hook);
Copy link
Contributor

Choose a reason for hiding this comment

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

Doxygen, obviously.

Also, I'm unsold on the names of both generic_fn_t and add_daemon_hook.

Copy link
Contributor

@djava djava Dec 25, 2024

Choose a reason for hiding this comment

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

Probably data_collection_hook_fn_t and add_data_processing_hook()?

@SizzinSeal
Copy link
Contributor

SizzinSeal commented Dec 26, 2024

There is a much better way to achieve the desired behavior which does not make it possible to accidentally crash the PROS system daemon (credit to @meisZWFLZ for this idea).

The PROS system daemon will notify tasks in a global linked list after mutexes have been released. If these tasks have a high enough priority, they will be scheduled immediately after the PROS system daemon ticks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants