8 Runtimes, Wakers, and the Reactor-Executor Pattern In the previous chapter, we created our own pausable tasks (coroutines) by writing them as state machines. We created a common API for these tasks by requiring them to implement the Future trait. We also showed how we can create these coroutines using some keywords and programmatically rewrite them so that we don't have to implement these state machines by hand, and instead write our programs pretty much the same way we normally would. If we stop for a moment and take a bird's eye view over what we got so far, it's conceptually pretty simple: we have an interface for pausable tasks (the Future trait), and we have two keywords (coroutine/wait ) to indicate code segments we want rewritten as a state machine that divides our code into segments we can pause between. However, we have no event loop, and we have no scheduler yet. In this chapter, we'll expand on our example and add a runtime that allows us to run our program efficiently and opens up the possibility to schedule tasks concurrently much more efficiently than what we do now. This chapter will take you on a journey where we implement our runtime in two stages, gradually making it more useful, efficient, and capable. We'll start with a brief overview of what runtimes are and why we want to understand some of their characteristics. We'll build on what we just learned in Chapter 7, and show how we can make it much more efficient and avoid continuously polling the future to make it progress by leveraging the knowledge we gained in Chapter 4. Next, we'll show how we can get a more flexible and loosely coupled design by dividing the runtime into two parts: an executor and a reactor. In this chapter, you will learn about basic runtime design, reactors, executors, wakers, and spawning, and we'll build on a lot of the knowledge we've gained throughout the book. This will be one of the big chapters in this book, not because the topic is too complex or difficult, but because we have quite a bit of code to write. In addition to that, I try to give you a good mental model of what's happening by providing quite a few diagrams and explaining everything very thoroughly. It's 