Table of Contents viii 2 How Programming Languages Model Asynchronous Program Flow 25 Definitions 26 Threads 27 Threads provided by the operating system 29 Creating new threads takes time 29 Each thread has its own stack 29 Context switching 30 Scheduling 30 The advantage of decoupling asynchronous operations from OS threads 31 Example 31Fibers and green threads 33 Each stack has a fixed space 34 Context switching 35 Scheduling 35 FFI 36 Callback based approaches 37 Coroutines: promises and futures 38 Coroutines and async/await 39 Summary 41 3 Understanding OS-Backed Event Queues, System Calls, and Cross-Platform Abstractions 43 Technical requirements 44 Running the Linux examples 45 Why use an OS-backed event queue? 45 Blocking I/O 46 Non-blocking I/O 46 Event queuing via epoll/kqueue and IOCP 47 Readiness-based event queues 47 Completion-based event queues 48epoll, kqueue, and IOCP 49 Cross-platform event queues 50 System calls, FFI, and cross-platform abstractions 51 The lowest level of abstraction 51 The next level of abstraction 55 The highest level of abstraction 61 Summary 61