𝔖 Scriptorium
✦   LIBER   ✦

πŸ“

Rust Atomics and Locks: Low-Level Concurrency in Practice

✍ Scribed by Mara Bos


Publisher
O'Reilly Media
Year
2023
Tongue
English
Leaves
252
Edition
1
Category
Library

⬇  Acquire This Volume

No coin nor oath required. For personal study only.

✦ Synopsis


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 very difficult. Even in the most well-used libraries, memory ordering bugs are not uncommon.

In this practical book, Mara Bos, leader 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

✦ Table of Contents


Cover
Copyright
Table of Contents
Foreword
Preface
Who This Book Is For
Overview of the Chapters
Code Examples
Conventions Used in This Book
Contact Information
Acknowledgments
Chapter 1. Basics of Rust Concurrency
Threads in Rust
Scoped Threads
Shared Ownership and Reference Counting
Statics
Leaking
Reference Counting
Borrowing and Data Races
Interior Mutability
Cell
RefCell
Mutex and RwLock
Atomics
UnsafeCell
Thread Safety: Send and Sync
Locking: Mutexes and RwLocks
Rust’s Mutex
Lock Poisoning
Reader-Writer Lock
Waiting: Parking and Condition Variables
Thread Parking
Condition Variables
Summary
Chapter 2. Atomics
Atomic Load and Store Operations
Example: Stop Flag
Example: Progress Reporting
Example: Lazy Initialization
Fetch-and-Modify Operations
Example: Progress Reporting from Multiple Threads
Example: Statistics
Example: ID Allocation
Compare-and-Exchange Operations
Example: ID Allocation Without Overflow
Example: Lazy One-Time Initialization
Summary
Chapter 3. Memory Ordering
Reordering and Optimizations
The Memory Model
Happens-Before Relationship
Spawning and Joining
Relaxed Ordering
Release and Acquire Ordering
Example: Locking
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
Testing It
Mutation
Weak Pointers
Testing It
Optimizing
Summary
Chapter 7. Understanding the Processor
Processor Instructions
Load and Store
Read-Modify-Write Operations
Load-Linked and Store-Conditional Instructions
Caching
Cache Coherence
Impact on Performance
Reordering
Memory Ordering
x86-64: Strongly Ordered
ARM64: Weakly Ordered
An Experiment
Memory Fences
Summary
Chapter 8. Operating System Primitives
Interfacing with the Kernel
POSIX
Wrapping in Rust
Linux
Futex
Futex Operations
Priority Inheritance Futex Operations
macOS
os_unfair_lock
Windows
Heavyweight Kernel Objects
Lighter-Weight Objects
Address-Based Waiting
Summary
Chapter 9. Building Our Own Locks
Mutex
Avoiding Syscalls
Optimizing Further
Benchmarking
Condition Variable
Avoiding Syscalls
Avoiding Spurious Wake-ups
Reader-Writer Lock
Avoiding Busy-Looping Writers
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
About the Author
Colophon

✦ Subjects


Concurrency; Rust; Atomics; Memory Ordering; Locking


πŸ“œ SIMILAR VOLUMES


Rust Atomics and Locks
✍ Mara Bos πŸ“‚ Library πŸ“… 2022 πŸ› O'Reilly Media 🌐 English

In this practical book, Mara Bos, leader 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

Rust for Network Programming and Automat
✍ Brian Anderson πŸ“‚ Library 🌐 English

<h4><span>Automate Network Infrastructure, Optimize Network Performance and Analyze Packets with Rust Ecosystem</span></h4><span><br></span><p><span>Rust for Network Programming and Automation</span><span> is a pragmatic guide that trains you through Rust to design networks and begin with automating

SQL Server Concurrency: Locking, Blockin
✍ Kalen Delaney πŸ“‚ Library πŸ› Red Gate Books 🌐 English

If you've designed your SQL code intelligently, and implemented a sensible indexing strategy, there's a good chance your queries will "fly", when tested in isolation. In the real world, however, where multiple processes can access the same data at the same time, SQL Server often has to make one proc

SQL Server Concurrency Locking, Blockin
✍ Kalen Delaney πŸ“‚ Library πŸ“… 2012 πŸ› Simple Talk Publishing 🌐 English

Your application can have impeachable indexes and queries, but they wont help you if you cant get to your data because another application has it locked. Thats why every DBA and developer must understand SQL Server concurrency and how to troubleshoot excessive blocking or deadlocking. If youve desi

Java Concurrency in Practice
✍ Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea πŸ“‚ Library πŸ“… 2006 πŸ› Addison-Wesley Professional 🌐 English

"I was fortunate indeed to have worked with a fantastic team on the design and implementation of the concurrency features added to the Java platform in Java 5.0 and Java 6. Now this same team provides the best explanation yet of these new features, and of concurrency in general. Concurrency is no lo