Skip to content

sys/event: add event_deferred_callback_post() helper #21219

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

Merged
merged 2 commits into from
Feb 28, 2025

Conversation

benpicco
Copy link
Contributor

Contribution description

Often there is a need to execute an event once after a delay.
This adds a convenience helper function for it.

Testing procedure

A test for the new function has been added to tests/sys/event_periodic_callback

Issues/PRs references

cleaner alternative to #20861

@github-actions github-actions bot added Area: tests Area: tests and testing framework Area: sys Area: System labels Feb 15, 2025
@benpicco benpicco force-pushed the event/periodic_callback branch from 0ab580f to 7f3e398 Compare February 15, 2025 22:39
@benpicco benpicco changed the title Event/periodic callback sys/event: add event_deferred_callback_start() helper Feb 15, 2025
@benpicco benpicco force-pushed the event/periodic_callback branch from 7f3e398 to 4bd0361 Compare February 15, 2025 22:42
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 15, 2025
@riot-ci
Copy link

riot-ci commented Feb 15, 2025

Murdock results

✔️ PASSED

b20f8cb tests/event_periodic_callback: test for event_deferred_callback_post()

Success Failures Total Runtime
10270 0 10271 08m:19s

Artifacts

@mguetschow mguetschow self-requested a review February 17, 2025 08:45
@benpicco benpicco force-pushed the event/periodic_callback branch from 4bd0361 to 2ab559a Compare February 17, 2025 12:03
@benpicco benpicco changed the title sys/event: add event_deferred_callback_start() helper sys/event: add event_deferred_callback_post() helper Feb 17, 2025
@benpicco benpicco force-pushed the event/periodic_callback branch from 2ab559a to b20f8cb Compare February 17, 2025 12:04
* @brief Internal helper function for ztimer callback
* @param[in] arg event structure
*/
static inline void _event_deferred_post(void *arg)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does inline do anything here?

Copy link
Member

Choose a reason for hiding this comment

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

To my experience, modern compilers tread inline as white space and just do what makes most sense (e.g. not inlining when emitting a static function into the compilation unit is more compact, or inlinling even without inline if that results in better code).

I personally still tend to add those, as this can be a hint to someone reading the code.

My personal vision is that we just enable LTO by default though, and move all the static inline functions to non-static functions in C files. Not having any C code in the headers makes compatibility with C++ and rust a lot easier and LTO does a better job than static inlines anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Still, but since this is a local callback function it cannot be inlined anyway, right? So having inline as hint might rather confuse people.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it cannot be inlined when a function ptr is set. Sorry, I missed that detail.

Copy link
Contributor

@kfessel kfessel Feb 28, 2025

Choose a reason for hiding this comment

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

gcc does not treat inline as white space instead "inline" removes the default/implicit extern so without adding another linkage maker the inline marked implementation is only available to be inlined (called in this compilation unit)

inline fn() { } << this can not be pointed to ( as it is no longer linkable) it is also not able to access static variables 
inline static fn() {}  << this can be inlined or staticly linked 
inline extern fn() {}  << this can be inlined or externaly linked
inline fn() {}  << use this implementation is used if inlining 

// another compilation unit may provide 

extern fn() {} << use this implementation is used for extern or to be pointed to 

https://godbolt.org/z/vb7YfKj3v

see compiler warning about no access to static and liker error about not linkable

what this is saying is inline is wrong as this function is not intended be directly called and it is not a good hint but one that leads the reader in the wrong direction

@benpicco benpicco requested a review from maribu February 20, 2025 11:05
* @param[in] callback callback to set up
* @param[in] arg callback argument to set up
*/
static inline void event_deferred_callback_post(event_deferred_callback_t *event,
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather have this in a .c file, as I think not inlining this will generate smaller binaries.

Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

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

Nice one. Not having to free-code this every time makes apps a lot easier to read :)

@benpicco benpicco added this pull request to the merge queue Feb 28, 2025
@benpicco benpicco removed this pull request from the merge queue due to a manual request Feb 28, 2025
@benpicco benpicco added this pull request to the merge queue Feb 28, 2025
Merged via the queue into RIOT-OS:master with commit 1e29887 Feb 28, 2025
31 checks passed
@benpicco benpicco deleted the event/periodic_callback branch February 28, 2025 14:28
@mguetschow mguetschow added this to the Release 2025.04 milestone Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: sys Area: System Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants