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

Add standalone waker constructor for executor and expose it #1015

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Ddystopia
Copy link

@Ddystopia Ddystopia commented Jan 27, 2025

Currently we can spawn a task with task::spawn(), but there is no way to get a waker. This PR adds const function task::waker(), that returns a waker that will wake that task.

My use case for this is smoltcp, they expose a lot of functions like that one: Socket::register_waker

@Ddystopia Ddystopia marked this pull request as draft January 27, 2025 09:57
@Ddystopia Ddystopia marked this pull request as ready for review January 27, 2025 10:04
@Ddystopia Ddystopia changed the title feat(executor): add standalone waker constructor Add standalone waker constructor for executor and expose it Jan 31, 2025
@korken89
Copy link
Collaborator

korken89 commented Feb 5, 2025

Hi! Thanks for the PR.

Can you motivate the need for this a bit more? As I see it, and e.g. what embassy-net does internally (https://github.com/embassy-rs/embassy/blob/main/embassy-net/src/lib.rs#L797) is the following to not need to have a way to get a waker:

async fn do_something() {
    poll_fn(|cx| {
        smoltcp.register_waker(cx.waker());

        if smoltcp.api_that_later_will_wake().is_done_or_something() {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }).await
}

@Ddystopia
Copy link
Author

Hi! Thanks for the PR.

Can you motivate the need for this a bit more? As I see it, and e.g. what embassy-net does internally (https://github.com/embassy-rs/embassy/blob/main/embassy-net/src/lib.rs#L797) is the following to not need to have a way to get a waker:

async fn do_something() {
    poll_fn(|cx| {
        smoltcp.register_waker(cx.waker());

        if smoltcp.api_that_later_will_wake().is_done_or_something() {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }).await
}

Sometimes I need to wake other task than the one setting the waker, or with some other logic. So I would need to create the waker with poll_fn and store it somewhere, then retrieve with other methods. And it can't be done in const contexts. With that change code is very straightforward by directly using the waker for a task.

So yes, it is possible, for non-const use cases, to use poll_fn to extract the waker, but when use case is more complicated then a basic one, user of the api would suffer, while it is really trivial for rtic to add that getter, as presented in pr.

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.

2 participants