https://marabos.nl/atomics/ Rust Atomics and Locks Low-Level Concurrency in Practice by Mara Bos A printed and ebook version of this book is published by O'Reilly: Book cover with a cute Kodiak bear Release date: January 2023 Page count: 250 pages ISBN: 9781098119447 Available at: Your local book store O'Reilly Learning Platform eBooks.com Amazon.com Bookshop.org Bol.com Hive.co.uk Amazon.co.uk Amazon.de Amazon.be Amazon.fr Lavoisier.fr LibreriaUniversitaria.it Wook.pt Books-Express.ro Exlibris.ch Saxo.com Adlibris.com Adlibris.com Adlibris.com Indigo.ca Booktopia.com.au ShroffPublishers.com And many other book stores. Translations available in: Japanese Korean About this Book The Rust programming language is extremely well suited for concurrency, and its ecosystem has many libraries that include lots of concurrent data structures, locks, and more. But implementing those structures correctly can be difficult. Even in the most well-used libraries, memory ordering bugs are not uncommon. In this practical book, Mara Bos, team lead of the Rust library team, helps Rust programmers of all levels gain a clear understanding of low-level concurrency. You'll learn everything about atomics and memory ordering and how they're combined with basic operating system APIs to build common primitives like mutexes and condition variables. Once you're done, you'll have a firm grasp of how Rust's memory model, the processor, and the role of the operating system all fit together. With this guide, you'll learn: * How Rust's type system works exceptionally well for programming concurrency correctly * All about mutexes, condition variables, atomics, and memory ordering * What happens in practice with atomic operations on Intel and ARM processors * How locks are implemented with support from the operating system * How to write correct code that includes concurrency, atomics, and locks * How to build your own locking and synchronization primitives correctly Start reading Code Examples The code examples from this book are available on GitHub: https:// github.com/m-ou-se/rust-atomics-and-locks Table of Contents * Foreword by Paul E. McKenney * Preface + Who This Book Is For + Overview of the Chapters + Code Examples + Conventions Used in This Book + Acknowledgments * Chapter 1. Basics of Rust Concurrency + Threads in Rust + Scoped Threads + Shared Ownership and Reference Counting o Statics o Leaking o Reference Counting + Borrowing and Data Races + Interior Mutability (Cell, RefCell, Mutex and RwLock, Atomics , UnsafeCell) + Thread Safety: Send and Sync + Locking: Mutexes and RwLocks o Rust's Mutex o Lock Poisoning o Reader-Writer Lock + Waiting: Parking and Condition Variables o Thread Parking o Condition Variables + Summary * Chapter 2. Atomics + Atomic Load and Store Operations o Example: Stop Flag o Example: Progress Reporting # Synchronization o Example: Lazy Initialization + Fetch-and-Modify Operations o Example: Progress Reporting from Multiple Threads o Example: Statistics o Example: ID Allocation + Compare-and-Exchange Operations o Example: ID Allocation Without Overflow o Example: Lazy One-Time Initialization + Summary * Chapter 3. Memory Ordering + Reordering and Optimizations + The Memory Model + Happens-Before Relationship o Spawning and Joining + Relaxed Ordering + Release and Acquire Ordering o Example: Locking o Example: Lazy Initialization with Indirection + Consume Ordering + Sequentially Consistent Ordering + Fences + Common Misconceptions + Summary * Chapter 4. Building Our Own Spin Lock + A Minimal Implementation + An Unsafe Spin Lock + A Safe Interface Using a Lock Guard + Summary * Chapter 5. Building Our Own Channels + A Simple Mutex-Based Channel + An Unsafe One-Shot Channel + Safety Through Runtime Checks + Safety Through Types + Borrowing to Avoid Allocation + Blocking + Summary * Chapter 6. Building Our Own "Arc" + Basic Reference Counting o Testing It o Mutation + Weak Pointers o Testing It + Optimizing + Summary * Chapter 7. Understanding the Processor + Processor Instructions o Load and Store o Read-Modify-Write Operations # x86 lock prefix # x86 compare-and-exchange instruction o Load-Linked and Store-Conditional Instructions # ARM load-exclusive and store-exclusive # Compare-and-exchange on ARM + Caching o Cache Coherence # The write-through protocol # The MESI protocol o Impact on Performance + Reordering + Memory Ordering o x86-64: Strongly Ordered o ARM64: Weakly Ordered o An Experiment o Memory Fences + Summary * Chapter 8. Operating System Primitives + Interfacing with the Kernel + POSIX o Wrapping in Rust + Linux o Futex o Futex Operations o Priority Inheritance Futex Operations + macOS o os_unfair_lock + Windows o Heavyweight Kernel Objects o Lighter-Weight Objects # Slim reader-writer locks o Address-Based Waiting + Summary * Chapter 9. Building Our Own Locks + Mutex o Avoiding Syscalls o Optimizing Further o Benchmarking + Condition Variable o Avoiding Syscalls o Avoiding Spurious Wake-ups + Reader-Writer Lock o Avoiding Busy-Looping Writers o Avoiding Writer Starvation + Summary * Chapter 10. Ideas and Inspiration + Semaphore + RCU + Lock-Free Linked List + Queue-Based Locks + Parking Lot-Based Locks + Sequence Lock + Teaching Materials * Index Rust Atomics and Locks (c) 2023 Mara Bos Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License Report an issue