How Async is implemented in Rust Tokio
Instead of creating threads by itself tokio uses epoll()
Tokio usually starts with threads equal to cores and use these to
schedule tasks on epoll()
// Tokio asks kernel to create non-blocking socket
sockfd = socket(O_NONBLOCK) // Create non blocking socket
// Tokio try reading.
// No data returns EWOULDBLOCK
read(sockfd)
// Tokio asks kernel to put on epoll()
// Make me aware when data arrives
epoll(sockfd)
Tokio Components
1. Reactor: Interfaces with the OS kernel (epoll/kqueue) to track
network socket readiness.
2. Scheduler: It manages a pool of worker threads. When a future
becomes ready (data arrived), the reactor pushes it back to a worker
thread's queue to be executed.