Asynchronous Programming in Rust: Building Working Examples of Futures, Green Threads, and Runtimes [1 ed.]
9781805128137
Explore the essence of asynchronous program flow and its significance. Understand the difference between concurrency and
131
31
English
Pages 682
Year 2024
Report DMCA / Copyright
DOWNLOAD EPUB FILE
Table of contents :
Asynchronous Programming in Rust
Contributors
About the author
About the reviewer
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Conventions used
Get in touch
Share your thoughts
Download a free PDF copy of this book
Part 1:Asynchronous Programming Fundamentals
1
Concurrency and Asynchronous Programming: a Detailed Overview
Technical requirements
An evolutionary journey of multitasking
Non-preemptive multitasking
Preemptive multitasking
Hyper-threading
Multicore processors
Do you really write synchronous code?
Concurrency versus parallelism
The mental model I use
Let’s draw some parallels to process economics
Concurrency and its relation to I/O
What about threads provided by the operating system?
Choosing the right reference frame
Asynchronous versus concurrent
The role of the operating system
Concurrency from the operating system’s perspective
Teaming up with the operating system
Communicating with the operating system
The CPU and the operating system
Down the rabbit hole
How does the CPU prevent us from accessing memory we’re not supposed to access?
But can’t we just change the page table in the CPU?
Interrupts, firmware, and I/O
A simplified overview
Interrupts
Firmware
Summary
2
How Programming Languages Model Asynchronous Program Flow
Definitions
Threads
Threads provided by the operating system
Creating new threads takes time
Each thread has its own stack
Context switching
Scheduling
The advantage of decoupling asynchronous operations from OS threads
Example
Fibers and green threads
Each stack has a fixed space
Context switching
Scheduling
FFI
Callback based approaches
Coroutines: promises and futures
Coroutines and async/await
Summary
3
Understanding OS-Backed Event Queues, System Calls, and Cross-Platform Abstractions
Technical requirements
Running the Linux examples
Why use an OS-backed event queue?
Blocking I/O
Non-blocking I/O
Event queuing via epoll/kqueue and IOCP
Readiness-based event queues
Completion-based event queues
epoll, kqueue, and IOCP
Cross-platform event queues
System calls, FFI, and cross-platform abstractions
The lowest level of abstraction
The next level of abstraction
The highest level of abstraction
Summary
Part 2:Event Queues and Green Threads
4
Create Your Own Event Queue
Technical requirements
Design and introduction to epoll
Is all I/O blocking?
The ffi module
Bitflags and bitmasks
Level-triggered versus edge-triggered events
The Poll module
The main program
Summary
5
Creating Our Own Fibers
Technical requirements
How to use the repository alongside the book
Background information
Instruction sets, hardware architectures, and ABIs
The System V ABI for x86-64
A quick introduction to Assembly language
An example we can build upon
Setting up our project
An introduction to Rust inline assembly macro
Running our example
The stack
What does the stack look like?
Stack sizes
Implementing our own fibers
Implementing the runtime
Guard, skip, and switch functions
Finishing thoughts
Summary
Part 3:Futures and async/await in Rust
6
Futures in Rust
What is a future?
Leaf futures
Non-leaf futures
A mental model of an async runtime
What the Rust language and standard library take care of
I/O vs CPU-intensive tasks
Summary
7
Coroutines and async/await
Technical requirements
Introduction to stackless coroutines
An example of hand-written coroutines
Futures module
HTTP module
Do all futures have to be lazy?
Creating coroutines
async/await
coroutine/wait
corofy—the coroutine preprocessor
b-async-await—an example of a coroutine/wait transformation
c-async-await—concurrent futures
Final thoughts
Summary
8
Runtimes, Wakers, and the Reactor-Executor Pattern
Technical requirements
Introduction to runtimes and why we need them
Reactors and executors
Improving our base example
Design
Changing the current implementation
Creating a proper runtime
Step 1 – Improving our runtime design by adding a Reactor and a Waker
Creating a Waker
Changing the Future definition
Step 2 – Implementing a proper Executor
Step 3 – Implementing a proper Reactor
Experimenting with our new runtime
An example using concurrency
Running multiple futures concurrently and in parallel
Summary
9
Coroutines, Self-Referential Structs, and Pinning
Technical requirements
Improving our example 1 – variables
Setting up the base example
Improving our base example
Improving our example 2 – references
Improving our example 3 – this is… not… good…
Discovering self-referential structs
What is a move?
Pinning in Rust
Pinning in theory
Definitions
Pinning to the heap
Pinning to the stack
Pin projections and structural pinning
Improving our example 4 – pinning to the rescue
future.rs
http.rs
Main.rs
executor.rs
Summary
10
Creating Your Own Runtime
Technical requirements
Setting up our example
main.rs
future.rs
http.rs
executor.rs
reactor.rs
Experimenting with our runtime
Challenges with asynchronous Rust
Explicit versus implicit reactor instantiation
Ergonomics versus efficiency and flexibility
Common traits that everyone agrees about
Async drop
The future of asynchronous Rust
Summary
Epilogue
Index
Why subscribe?
Other Books You May Enjoy
Packt is searching for authors like you
Share your thoughts
Download a free PDF copy of this book