Skip to content

Conversation

@sticnarf
Copy link
Contributor

@sticnarf sticnarf commented Apr 2, 2021

A "status quo" story about my first experience about writing an async executor.

Copy link
Member

@tmandry tmandry left a comment

Choose a reason for hiding this comment

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

Nicely written! I was able to follow along quite well and each step along the way made sense. I added a few comments that might help improve the story somewhat.

}
```

"How to create a waker?" Barbara asked herself. Quickly, she found the `Wake` trait. After reading the documentation, she realized the task in the scheduler should be stored in an `Arc`. And to implement `Wake`, the `Task` should also contain the sender of the scheduler. She changed the code to something like this:
Copy link
Member

Choose a reason for hiding this comment

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

At this point I'm thinking about building an I/O reactor, so it might help to explain (here or above) that this is for compute tasks, not async I/O ones.

Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be helpful I think to say why the task should be in an arc. Presumably the idea is that the Waker will need to hold a reference to the task so that it can be scheduled; I guess it will need to also hold a reference to the queue or something like that?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I guess you said that last part ('the sender of the scheduler")

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, and I guess the Arc is partly because of the waker API...


Luckily, a colleague of Barbara noticed something wrong. The `wake` method could be called multiple times so multiple copies of the task could exist in the scheduler. The scheduler might not work correctly because of it and a more severe problem was that multiple threads might get copies of the same task from the scheduler and cause a race in polling the future.

Barbara soon got a idea to solve it. She added a state field to the `Task`. By carefully maintaining the state of the task, she could guarantee there are no duplicate tasks in the scheduler and no race can happen when polling the future:
Copy link
Member

Choose a reason for hiding this comment

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

Just as a suggestion, maybe this paragraph can say something about how she spends a lot of effort in writing the code below (at least, it would take me a lot of effort). Then the reader is already focused on the pain of having to write this to satisfy such a simple requirement.


*Here are some standard FAQ to get you started. Feel free to add more!*

* **What are the morals of the story?**
Copy link
Contributor

Choose a reason for hiding this comment

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

The style of the FAQ sections was changed in #118 / #119, using sub headings (###) instead of nested bullet lists.

}
```

"How to create a waker?" Barbara asked herself. Quickly, she found the `Wake` trait. After reading the documentation, she realized the task in the scheduler should be stored in an `Arc`. And to implement `Wake`, the `Task` should also contain the sender of the scheduler. She changed the code to something like this:
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be helpful I think to say why the task should be in an arc. Presumably the idea is that the Waker will need to hold a reference to the task so that it can be scheduled; I guess it will need to also hold a reference to the queue or something like that?

}
```

"How to create a waker?" Barbara asked herself. Quickly, she found the `Wake` trait. After reading the documentation, she realized the task in the scheduler should be stored in an `Arc`. And to implement `Wake`, the `Task` should also contain the sender of the scheduler. She changed the code to something like this:
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I guess you said that last part ('the sender of the scheduler")

}
```

"How to create a waker?" Barbara asked herself. Quickly, she found the `Wake` trait. After reading the documentation, she realized the task in the scheduler should be stored in an `Arc`. And to implement `Wake`, the `Task` should also contain the sender of the scheduler. She changed the code to something like this:
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, and I guess the Arc is partly because of the waker API...

@nikomatsakis
Copy link
Contributor

This story is really good. I left various comments, but I don't think it needs much.

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

(Marking as Changes Requested)

@nikomatsakis nikomatsakis added the status-quo-story-ideas "Status quo" user story ideas label Apr 6, 2021
Copy link
Contributor Author

@sticnarf sticnarf left a comment

Choose a reason for hiding this comment

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

Apply some suggestions.

@sticnarf
Copy link
Contributor Author

sticnarf commented Apr 6, 2021

It'd be helpful I think to say why the task should be in an arc. Presumably the idea is that the Waker will need to hold a reference to the task so that it can be scheduled; I guess it will need to also hold a reference to the queue or something like that?

Oh, and I guess the Arc is partly because of the waker API...

Yes, the API is the biggest hint. And it can take some while to think out why it needs an Arc. I changed a little of the story text to convey it better.

Copy link
Member

@rylev rylev left a comment

Choose a reason for hiding this comment

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

Awesome! I left a few small suggestions, but it is already looking great.

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

one more nit, per @rylev

@nikomatsakis nikomatsakis merged commit 9be3993 into rust-lang:master Apr 6, 2021
@nikomatsakis
Copy link
Contributor

I'm going to go ahead and merge this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status-quo-story-ideas "Status quo" user story ideas

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants