Skip to content

Commit 76958dd

Browse files
Merge pull request #88 from rodrirejala/fix-ch02
fix(ch02): enhance async diagram — add Reactor and OS layers (fixes #49)
2 parents 158830e + bede90e commit 76958dd

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

async-book/src/ch02-the-future-trait.md

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,34 @@ That's it. A `Future` is anything that can be *polled* — asked "are you done y
3030
```mermaid
3131
sequenceDiagram
3232
participant E as Executor
33-
participant F as Future
34-
participant R as Resource (I/O)
35-
36-
E->>F: poll(cx)
37-
F->>R: Check: is data ready?
38-
R-->>F: Not yet
39-
F->>R: Register waker from cx
40-
F-->>E: Poll::Pending
41-
42-
Note over R: ... time passes, data arrives ...
43-
44-
R->>E: waker.wake() — "I'm ready!"
45-
E->>F: poll(cx) — try again
46-
F->>R: Check: is data ready?
47-
R-->>F: Yes! Here's the data
48-
F-->>E: Poll::Ready(data)
33+
participant F as Future (Task)
34+
participant OS as Operating System<br/>(e.g., epoll/kqueue)
35+
participant R as Reactor (Runtime)
36+
37+
E->>F: Calls poll(cx)
38+
Note right of F: Future attempts operation
39+
F->>OS: Syscall (e.g., read TCP socket)
40+
OS-->>F: Returns Error: Not Ready
41+
42+
F->>R: Registers: (Waker)
43+
F-->>E: Returns Poll::Pending
44+
Note left of E: Task is moved out<br/>of run queue
45+
46+
E->>E: (Executor runs other tasks OR sleeps)
47+
R->>OS: epoll_wait() / Polls OS for events
48+
49+
Note right of OS: (Sometime Later) New data arrives
50+
OS-->>R: Wakes Reactor: data is NOW READY
51+
52+
R->>R: Reactor finds Waker
53+
R->>E: Calls Waker::wake()
54+
Note right of E: Task is pushed back<br/>to Executor's run queue
55+
56+
E->>F: Calls poll(cx) again
57+
Note right of F: Future attempts operation again
58+
F->>OS: Syscall (e.g., read TCP socket)
59+
OS-->>F: Success: Returns Data Buffer
60+
F-->>E: Returns Poll::Ready(Data)
4961
```
5062

5163
Let's break down each piece:

0 commit comments

Comments
 (0)