Sub topic 3
What a runtime adds: executor and reactor
This chapter assumes you know how async fn and .await produce futures, and that a future is advanced by calls to poll with a Waker. It also assumes basic Cargo use. Here we look at what has to exist outside the future for it to make progress.
Futures do not run themselves
The standard library defines the Future trait and the async syntax. It does not include anything that calls poll. Calling an async fn only builds a state machine. Nothing in it runs until something polls it.
[dependencies]
tokio = { version = "1", features = ["full"] }use std::time::Duration;
use tokio::time::sleep;
async fn greet() {
sleep(Duration::from_millis(50)).await;
println!("hello from a future");
}
#[tokio::main]
async fn main() {
let fut = greet();
println!("future created, nothing has run yet");
fut.await;
}The first line prints before the future does any work. The second prints only after fut.await hands the future to the runtime's polling loop. Something outside your code has to drive this loop, and that is the runtime.
The two parts of a runtime
A runtime has two cooperating components.
- The executor keeps a queue of tasks that are ready to make progress. Worker threads take a task from the queue and call
pollon it. IfpollreturnsPoll::Ready, the task is finished. If it returnsPoll::Pending, the executor sets the task aside and moves on to the next one. - The reactor waits for events from the operating system, such as a socket becoming readable or a timer expiring. Tokio builds on the OS facilities
epoll(Linux),kqueue(macOS and BSD) and IOCP (Windows). When an event arrives, the reactor callswakeon theWakerthat was registered for it. That puts the task back in the executor's ready queue.
Together they avoid busy-polling. A task that is waiting on a socket costs no CPU until the reactor reports that the socket is ready.
Why leaf futures matter
Futures like sleep and TcpStream::read are called leaf futures. They are the only futures that talk to the reactor. When a leaf future's poll finds nothing ready, it stores the current Waker with the reactor and returns Pending. Your own async fn code is a chain of such futures, joined by .await.
This design is why an .await is a point where control can return to the executor. Between two .await points, your code runs without interruption, and the executor cannot switch to another task on that thread.
Runtimes are not interchangeable
Leaf futures call into one specific reactor. A Tokio sleep polled outside a Tokio runtime panics, because there is no reactor to register with. This is why libraries often depend on a runtime, and why mixing runtimes in one program needs care.
Exercise
A task is awaiting socket.read(&mut buf) and no data has arrived. Which component calls poll on the task again, and what triggers it?
Show answer
The executor calls poll again. The trigger comes from the reactor: when the OS reports the socket as readable, the reactor calls wake on the Waker the read future registered. That puts the task back in the executor's ready queue.