Rust Atomics and Locks

Book description

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

Publisher resources

View/Submit Errata

Table of contents

  1. Foreword
  2. Preface
    1. Who This Book Is For
    2. Overview of the Chapters
    3. Code Examples
    4. Conventions Used in This Book
    5. Contact Information
    6. Acknowledgments
  3. 1. Basics of Rust Concurrency
    1. Threads in Rust
    2. Scoped Threads
    3. Shared Ownership and Reference Counting
      1. Statics
      2. Leaking
      3. Reference Counting
    4. Borrowing and Data Races
    5. Interior Mutability
      1. Cell
      2. RefCell
      3. Mutex and RwLock
      4. Atomics
      5. UnsafeCell
    6. Thread Safety: Send and Sync
    7. Locking: Mutexes and RwLocks
      1. Rust’s Mutex
      2. Lock Poisoning
      3. Reader-Writer Lock
    8. Waiting: Parking and Condition Variables
      1. Thread Parking
      2. Condition Variables
    9. Summary
  4. 2. Atomics
    1. Atomic Load and Store Operations
      1. Example: Stop Flag
      2. Example: Progress Reporting
      3. Example: Lazy Initialization
    2. Fetch-and-Modify Operations
      1. Example: Progress Reporting from Multiple Threads
      2. Example: Statistics
      3. Example: ID Allocation
    3. Compare-and-Exchange Operations
      1. Example: ID Allocation Without Overflow
      2. Example: Lazy One-Time Initialization
    4. Summary
  5. 3. Memory Ordering
    1. Reordering and Optimizations
    2. The Memory Model
    3. Happens-Before Relationship
      1. Spawning and Joining
    4. Relaxed Ordering
    5. Release and Acquire Ordering
      1. Example: Locking
      2. Example: Lazy Initialization with Indirection
    6. Consume Ordering
    7. Sequentially Consistent Ordering
    8. Fences
    9. Common Misconceptions
    10. Summary
  6. 4. Building Our Own Spin Lock
    1. A Minimal Implementation
    2. An Unsafe Spin Lock
    3. A Safe Interface Using a Lock Guard
    4. Summary
  7. 5. Building Our Own Channels
    1. A Simple Mutex-Based Channel
    2. An Unsafe One-Shot Channel
    3. Safety Through Runtime Checks
    4. Safety Through Types
    5. Borrowing to Avoid Allocation
    6. Blocking
    7. Summary
  8. 6. Building Our Own “Arc”
    1. Basic Reference Counting
      1. Testing It
      2. Mutation
    2. Weak Pointers
      1. Testing It
    3. Optimizing
    4. Summary
  9. 7. Understanding the Processor
    1. Processor Instructions
      1. Load and Store
      2. Read-Modify-Write Operations
      3. Load-Linked and Store-Conditional Instructions
    2. Caching
      1. Cache Coherence
      2. Impact on Performance
    3. Reordering
    4. Memory Ordering
      1. x86-64: Strongly Ordered
      2. ARM64: Weakly Ordered
      3. An Experiment
      4. Memory Fences
    5. Summary
  10. 8. Operating System Primitives
    1. Interfacing with the Kernel
    2. POSIX
      1. Wrapping in Rust
    3. Linux
      1. Futex
      2. Futex Operations
      3. Priority Inheritance Futex Operations
    4. macOS
      1. os_unfair_lock
    5. Windows
      1. Heavyweight Kernel Objects
      2. Lighter-Weight Objects
      3. Address-Based Waiting
    6. Summary
  11. 9. Building Our Own Locks
    1. Mutex
      1. Avoiding Syscalls
      2. Optimizing Further
      3. Benchmarking
    2. Condition Variable
      1. Avoiding Syscalls
      2. Avoiding Spurious Wake-ups
    3. Reader-Writer Lock
      1. Avoiding Busy-Looping Writers
      2. Avoiding Writer Starvation
    4. Summary
  12. 10. Ideas and Inspiration
    1. Semaphore
    2. RCU
    3. Lock-Free Linked List
    4. Queue-Based Locks
    5. Parking Lot–Based Locks
    6. Sequence Lock
    7. Teaching Materials
  13. Index
  14. About the Author

Product information

  • Title: Rust Atomics and Locks
  • Author(s): Mara Bos
  • Release date: January 2023
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098119447