-
Notifications
You must be signed in to change notification settings - Fork 40
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 metadata to tasks #33
Conversation
At first glance, I feel that generic metadata is a bit overkill/complicated, but I have no strong opinion on this. |
@smol-rs/admins Any thoughts on this? |
I don't think it's overkill but seems to go beyond the scope. #31 is about debugging and makes perfect sense. If users need to associate other metadata, they can create custom types that contain the task along with any other data they need and pass it around. Unless I am missing something? A lot more useful thing to have would be task-local data, which async-std and tokio provide an API for. |
Technically, you're right about metadata. The metadata could be stored in both the scheduler function and alongside the task handle. You could do something like this for the priority queue example I wrote:
...and then Overall I would be opposed to task-local variables. We could emulate them by storing a |
It doesn't necessarily have to be part of this crate and if it's part of this crate, it can be feature-gated. |
Thanks for explaining. I guess the only questions are that which you asked in the description:
Seems fine to me since it's not an API break.
Indeed this would make the metadata api a lot more appealing. |
I've implemented a new strategy. Instead of the future, the Please let me know if there is a more idiomatic way of going about this safely. |
Looks mostly good to me, do we ensure that the |
I think that's assured by this line in
Since the borrowed metadata is a borrowed variable, returning it would mean outliving the For the safe cases, the |
@smol-rs/admins Are there any blockers to this being approved for merge? It seems like this was blocked for further discussion. |
I think the docs around Second, "If |
I think this is a good idea! My bandwidth is limited at the moment, do you mind writing a PR? |
I could, sure, but I actually don't know whether it's fine for EDIT: Ok, after reading some more, yeah, guaranteed the Future is dropped before the ref count is decremented on the raw task, and the metadata reference is valid for the lifetime of the raw task. So capturing references to metadata is safe. |
In the course of resolving #31, I realized that users may want to associate other things with tasks, like string names, priorities, debugger information, and otherwise. This PR adds a new generic field to both
Task
andRunnable
,M
. By default,M
is the unit type,()
, in order to keep consistency with the previous operation of this crate. This metadata is stored in the allocation and can be accessed via themetadata()
function on bothTask
andRunnable
.In order to create new tasks with metadata, I've introduced a new
Builder
structure. This builder expands on the previousspawn_*
family of functions by providing ametadata()
function that creates the task using a generic piece of metadata.Builder
may be useful later on for solving #24 once allocator APIs are stable.This is a minor version bump;
Task
s use()
as their defaultM
parameter to ensure that the logic is kept consistent.Discussion questions:
M
is essentially stored in anArc
, I've addedM: Send + Sync
bounds to theimpl Send/Sync
for both theTask
and theRunnable.
Is this right?Closes #31