Skip to content

Make kernel timeout record (struct _timeout) a public API #91146

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bjarki-andreasen
Copy link
Collaborator

@bjarki-andreasen bjarki-andreasen commented Jun 5, 2025

This PR promotes the internal kernel timeout record struct _timeout to a public struct and API, exposing it with a friendly name struct k_timeout_record and documenting it thoroughly. The kernel timeout record is really efficient and useful for subsystems which need a "timer" but can benefit from the lower overhead compared to the k_timer. The name timeout_record was inspired by @nashif :)

This PR is backwards compatible, it keeps the internal struct untouched, it just adds a wrapper around it, with a tiny test in kernel/common just to make sure the wrapper functions as expected.

The kernel timeout record, currenly a private feature, is very
useful for subsystems which require a very low overhead
timeout with callback. Currently subsystems are forced to use
the k_timer for any and all timeouts, which is not efficient,
or do some tricks to include and use the kernel timeout record
anyway, like #include <../kernel/include/timeout_q.h> like its
done for RTIO.

This commit exposes the kernel timeout in a zero overhead and
backwards compatible manner, with added (and needed)
documentation to help users use the kernel timeout record
appropriately. It will hopefully encourage subsystems to use
the kernel timeout record in place of k_timer where
appropriate.

In the future, we may even deprecate struct _timeout in favor
of k_timeout_record.

Signed-off-by: Bjarki Arge Andreasen <[email protected]>
Add test of timeout record to the common kernel tests included if
CONFIG_SYS_CLOCK_EXISTS.

Signed-off-by: Bjarki Arge Andreasen <[email protected]>
Use newly exposed k_timeout_record instead of "hacking" a bit to
use the internal _timeout.

Signed-off-by: Bjarki Arge Andreasen <[email protected]>
Copy link

sonarqubecloud bot commented Jun 5, 2025

Copy link
Collaborator

@andyross andyross left a comment

Choose a reason for hiding this comment

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

No complaints, really. I agree this is more generically useful than a k_timer (though k_timer is really a pretty lightweight wrapper) and has a simple API to specify.

But at the same time this is the internal thing, and putting a public face on it is going to freeze the kernel if it wants to do something different in the future (e.g. there is a "min-heap" PR in flight somewhere else where I suggested that it would make a good backend for the timeout list) it might tie our hands if it requires an API change.

Maybe we could mark this "experimental" for the first few releases out of conservatism?

Copy link
Member

@ubieda ubieda left a comment

Choose a reason for hiding this comment

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

I also support this PR and I wished it was available when RTIO delay was implemented. Thanks, @bjarki-andreasen!

@nashif
Copy link
Member

nashif commented Jun 5, 2025

The name timeout_record was inspired by @nashif :)

not really, it is already there

/* kernel timeout record */

So, what happens to _timeout? Do we need to keep it? We now will end up with three timeout structs, isnt this going o be confusing?

@bjarki-andreasen
Copy link
Collaborator Author

The name timeout_record was inspired by @nashif :)

not really, it is already there

/* kernel timeout record */

So, what happens to _timeout? Do we need to keep it? We now will end up with three timeout structs, isnt this going o be confusing?

I mention in the commit we could decide to deprecate _timeout in favor of the one in this PR, but as noted by @andyross maybe we should give the "new API" some time before comitting to it :)

@nashif
Copy link
Member

nashif commented Jun 7, 2025

I mention in the commit we could decide to deprecate _timeout in favor of the one in this PR, but as noted by @andyross maybe we should give the "new API" some time before comitting to it :)

I say we need to deprecate and just have one way for doing this and remove the duplicate structs and private APIs/structures.
We are in agreement this API lands as experimental just like any other API, so changes might follow, however, I do not expect us to be backing out, so let's do this proper please.
I like the direction, however we need this done thoroughly or we will be stuck with multiple timeout structe/API for a long time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Architecture Review Discussion in the Architecture WG required area: Kernel area: RTIO
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

7 participants