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

maitake: Schedule::current_task method #277

Closed
hawkw opened this issue Jul 30, 2022 · 0 comments · Fixed by #282
Closed

maitake: Schedule::current_task method #277

hawkw opened this issue Jul 30, 2022 · 0 comments · Fixed by #282
Labels
crate/maitake Related to the `maitake` crate kind/enhancement New feature or request

Comments

@hawkw
Copy link
Owner

hawkw commented Jul 30, 2022

It would be nice to have a function for getting the current task. This can be used by leaf futures if they want to self-wake, access information about what task they're being polled from, such as for debugging (see #265 and #267) , or forcibly cancel the current task (see #264).

maitake itself can't implement an "ambiently available" task::current() function, because it doesn't know about "locality"1. But, we can add a method to the Scheduler types for accessing the task that scheduler is currently polling. Then, the OS can use this to implement a task::current() by finding the current core's scheduler (or picking the global one if single-core).

I imagine this would have a signature like

pub fn current_task(&self) -> Option<TaskRef>

We would probably have to store the task ptr in an AtomicPointer in the scheduler, and load that and that into a TaskRef if someone wants it.

Footnotes

  1. Are we single-core, and there's a single global scheduler? Or are we multi-core, and each core has its own scheduler? And if so, how do I access the scheduler for the current core? Those are all OS-level concerns that maitake can't handle.

@hawkw hawkw added kind/enhancement New feature or request crate/maitake Related to the `maitake` crate labels Jul 30, 2022
hawkw added a commit that referenced this issue Jul 30, 2022
This adds an initial implementation of a `Schedule::current_task` method
for cloning the currently-polled task. This implementation is somewhat
inefficient, as it has to perform an additional ref count clone/drop
cycle on every poll. This is unfortunately necessary because the current
task variable is not unset until the `TaskRef::poll` method returns, and
that method can drop the task, so we have to keep the task alive as long
as it's accessible through the scheduler (or else we risk handing out
`TaskRef`s pointed at deallocated tasks).

We can probably improve this by removing the unnecessary ref clone/drop
cycle, if we refactored how polling works a bit. If the scheduler unset
the current task variable _before_ a task is dropped, we wouldn't need
to clone the task before storing it in the current task variable...but,
this implementation works for now.

Closes #277
hawkw added a commit that referenced this issue Jul 31, 2022
This adds an initial implementation of a `Schedule::current_task` method
for cloning the currently-polled task. This implementation is somewhat
inefficient, as it has to perform an additional ref count clone/drop
cycle on every poll. This is unfortunately necessary because the current
task variable is not unset until the `TaskRef::poll` method returns, and
that method can drop the task, so we have to keep the task alive as long
as it's accessible through the scheduler (or else we risk handing out
`TaskRef`s pointed at deallocated tasks).

We can probably improve this by removing the unnecessary ref clone/drop
cycle, if we refactored how polling works a bit. If the scheduler unset
the current task variable _before_ a task is dropped, we wouldn't need
to clone the task before storing it in the current task variable...but,
this implementation works for now.

Closes #277
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/maitake Related to the `maitake` crate kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant