minor fixes to the debug printout of the main example

This commit is contained in:
Carl Fredrik Samson
2020-04-11 00:23:39 +02:00
parent 8934c46679
commit 02bb33c6b6
22 changed files with 1881 additions and 1616 deletions

View File

@@ -576,12 +576,15 @@ fn main() {
# fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
# let mut r = self.reactor.lock().unwrap();
# if r.is_ready(self.id) {
# println!("POLL: TASK {} IS READY", self.id);
# *r.tasks.get_mut(&self.id).unwrap() = TaskState::Finished;
# Poll::Ready(self.id)
# } else if r.tasks.contains_key(&self.id) {
# println!("POLL: REPLACED WAKER FOR TASK: {}", self.id);
# r.tasks.insert(self.id, TaskState::NotReady(cx.waker().clone()));
# Poll::Pending
# } else {
# println!("POLL: REGISTERED TASK: {}, WAKER: {:?}", self.id, cx.waker());
# r.register(self.data, cx.waker().clone(), self.id);
# Poll::Pending
# }
@@ -676,11 +679,10 @@ fn main() {
# }
```
I added a debug printout of the events the reactor registered interest for so we can observe
two things:
I added a some debug printouts so we can observe a couple of things:
1. How the `Waker` object looks just like the _trait object_ we talked about in an earlier chapter
2. In what order the events register interest with the reactor
2. The program flow from start to finish
The last point is relevant when we move on the the last paragraph.

View File

@@ -106,12 +106,15 @@ impl Future for Task {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut r = self.reactor.lock().unwrap();
if r.is_ready(self.id) {
println!("POLL: TASK {} IS READY", self.id);
*r.tasks.get_mut(&self.id).unwrap() = TaskState::Finished;
Poll::Ready(self.id)
} else if r.tasks.contains_key(&self.id) {
println!("POLL: REPLACED WAKER FOR TASK: {}", self.id);
r.tasks.insert(self.id, TaskState::NotReady(cx.waker().clone()));
Poll::Pending
} else {
println!("POLL: REGISTERED TASK: {}, WAKER: {:?}", self.id, cx.waker());
r.register(self.data, cx.waker().clone(), self.id);
Poll::Pending
}
@@ -148,9 +151,7 @@ impl Reactor {
let reactor_clone = Arc::downgrade(&reactor);
let handle = thread::spawn(move || {
let mut handles = vec![];
// This simulates some I/O resource
for event in rx {
println!("REACTOR: {:?}", event);
let reactor = reactor_clone.clone();
match event {
Event::Close => break,