Book Image

Hands-On Concurrency with Rust

By : Brian L. Troutwine
Book Image

Hands-On Concurrency with Rust

By: Brian L. Troutwine

Overview of this book

Most programming languages can really complicate things, especially with regard to unsafe memory access. The burden on you, the programmer, lies across two domains: understanding the modern machine and your language's pain-points. This book will teach you to how to manage program performance on modern machines and build fast, memory-safe, and concurrent software in Rust. It starts with the fundamentals of Rust and discusses machine architecture concepts. You will be taken through ways to measure and improve the performance of Rust code systematically and how to write collections with confidence. You will learn about the Sync and Send traits applied to threads, and coordinate thread execution with locks, atomic primitives, data-parallelism, and more. The book will show you how to efficiently embed Rust in C++ code and explore the functionalities of various crates for multithreaded applications. It explores implementations in depth. You will know how a mutex works and build several yourself. You will master radically different approaches that exist in the ecosystem for structuring and managing high-scale systems. By the end of the book, you will feel comfortable with designing safe, consistent, parallel, and high-performance applications in Rust.
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Index

A

  • Amdahl's law / Diminishing returns
  • American Fuzzy Lop
    • about / Testing with American Fuzzy Lop
    • naive HashMap, testing / Testing with American Fuzzy Lop
  • associative arrays
    • performance / Performance
    • HashMap / Standard library HashMap
    • naive HashMap / Naive HashMap
  • async/await syntax / Futures and async/await
  • AtomicPtr / An incorrect atomic queue
  • atomic swap mutex / Compare and set mutex

B

  • barrier / Blocking until the gang's all here - barrier
  • beamcoin
    • reference / Into Erlang/Elixir
  • binary semaphore
    • about / Semaphore
    • versus mutex / Binary semaphore, or, a less wasteful mutex
  • bincode
    • about / The Receiver
    • reference / The Receiver, Representing the domain
  • bounded semaphore / Semaphore
  • boxed types / Boxed types

C

  • C
    • embedding, into Rust / Embedding C into Rust – feruscore without processes
    • Rust, embedding into / Into C, The Rust side, The C side
  • C-structs
    • creating / Creating C-structs from Rust
  • caches / Memory and caches
  • cargo-fuzz
    • URL / Fuzzing
  • cc
    • URL / Creating C-structs from Rust
  • Cell / Cell and RefCell
  • cernan
    • URL / A telemetry server, The problem, Embedding Lua into Rust
  • C functions
    • calling / Calling C functions
    • cross-language ownership, managing / Managing cross-language ownership
    • simulation, executing / Running the simulation
    • simulation, fuzzing / Fuzzing the simulation
    • feruscore executable / The feruscore executable
  • conc
    • URL / Options to correct the incorrect queue
  • concurrent data structures, hopper
    • testing / Testing concurrent data structures
    • QuickCheck, searching / QuickCheck and loops
    • crashes, searching with AFL / Searching for crashes with AFL
    • benchmarking / Benchmarking
  • condition variable (condvar)
    • about / Blocking until conditions change – condvar
    • using / Blocking until conditions change – condvar
  • Corewars
    • players, evolving / Data parallelism and OS processes – evolving corewars players
    • about / Corewars, The MARS C interface
  • counting semaphore / Semaphore
  • CPU / The CPU
  • crates.io
    • reference link / The community
  • criterion
    • naive HashMap, performance testing / Performance testing with Criterion
    • URL / Benchmarking
    • using / Benchmarking
  • crossbeam
    • URL / Options to correct the incorrect queue
  • crossbeam project
    • URL / Epoch-based reclamation
  • crossbeam RFCs
    • URL / The community
  • custom allocators / Custom allocators

D

  • data parallelism / Data parallelism and OS processes – evolving corewars players
  • diminishing return / Diminishing returns
  • docs.rs
    • reference link / The community
  • dynamic dispatch / Static and dynamic dispatch

E

  • Elixir
    • URL / Into Erlang/Elixir
  • epoch-based reclamation (EBR)
    • about / Options to correct the incorrect queue, Epoch-based reclamation
    • Treiber stack / An epoch-based Treiber stack
    • crossbeam_epoch / crossbeam_epoch::Atomic, crossbeam_epoch::Guard::defer, crossbeam_epoch::Local::pin
    • Treiber stack, exercising / Exercising the epoch-based Treiber stack
    • tradeoffs / Tradeoffs
  • Erlang/Elixir
    • Rust, embedding into / Into Erlang/Elixir
  • Ethernet sniffer / The Ethernet sniffer
  • exhaust 1.9.2
    • URL / The MARS C interface

F

  • feruscore
    • about / Feruscore – a Corewars evolver
    • domain, representing / Representing the domain
    • source, exploring / Exploring the source
    • instruction module / Instructions
    • individual module / Individuals
    • mutation / Mutation and reproduction
    • reproduction / Mutation and reproduction
    • pMARS, calling out / Competition – calling out to pMARS
    • main section, exploring / Main
    • executing / Running feruscore
  • fuzzing / Fuzzing

G

  • generative testing / Testing with QuickCheck

H

  • HashMap / Standard library HashMap
  • hazard-pointer-based reclamation (HPBR) / Options to correct the incorrect queue
  • hazard pointers
    • about / Hazard pointers
    • Treiber stack / A hazard-pointer Treiber stack
    • nightly / The hazard of Nightly
    • Treiber stack, exercizing / Exercizing the hazard-pointer Treiber stack
    • tradeoffs / Tradeoffs
  • hex encoding / Hex encoding
  • honggfuzz-rs
    • URL / Fuzzing
  • hopper
    • use case / Hopper—an MPSC specialization
    • problem / The problem
    • about / The problem
    • URL / The problem
    • using / Hopper in use
    • conceptual view / A conceptual view of hopper
    • deque / The deque
    • Receiver / The Receiver
    • Sender / The Sender
    • concurrent data structures, testing / Testing concurrent data structures
  • hopper-fuzz
    • URL / Searching for crashes with AFL
    • using / Searching for crashes with AFL

I

  • incorrect atomic queue
    • about / An incorrect atomic queue
    • correcting / Options to correct the incorrect queue
  • isize type / Vec
  • iterators
    • about / Iterators
    • smallcheck iteration / Smallcheck iteration
    • parallel iterator / rayon – parallel iterators
  • itertools
    • reference / Iterators

L

  • libcds
    • URL / Options to correct the incorrect queue
  • libfuzzer
    • URL / Fuzzing
  • library clap
    • URL / The server
  • library slog
    • URL / The server
  • linearizability / Linearizability
  • Linux perf
    • naive HashMap, inspecting / Inspecting with Linux perf
  • lock-free reference counting (LFRC) / Options to correct the incorrect queue
  • Lua
    • embedding, into Rust / Embedding Lua into Rust
  • Lua interpreter
    • URL / Embedding Lua into Rust

M

  • machine model
    • selecting / The machine
    • CPU / The CPU
    • memory / Memory and caches
    • caches / Memory and caches
  • MARS C interface / The MARS C interface
  • memory
    • about / Memory and caches
    • memory model / Memory model
    • layout / Memory layout
    • pointers / Pointers to memory
    • allocating / Allocating and deallocating memory
    • deallocating / Allocating and deallocating memory
  • memory model
    • reference / Memory ordering – happens-before and synchronizes-with
  • memory ordering
    • about / Memory ordering – happens-before and synchronizes-with
    • Relaxed / Ordering::Relaxed
    • Acquire / Ordering::Acquire
    • Release / Ordering::Release
    • AcqRel / Ordering::AcqRel
    • SeqCst / Ordering::SeqCst
  • memory reclamation
    • approaches / Approaches to memory reclamation
    • reference counting / Reference counting
    • hazard pointers / Hazard pointers
    • epoch-based reclamation (EBR) / Epoch-based reclamation
  • move closure / Racing threads
  • MPSC
    • using / Using MPSC
    • telemetry server, building / A telemetry server
  • mutex
    • about / Mutexes
    • soft-mutex / Mutexes
    • comparing / Compare and set mutex
    • setting / Compare and set mutex
    • versus binary semaphore / Binary semaphore, or, a less wasteful mutex

N

  • naive HashMap
    • about / Naive HashMap
    • starting / Naive HashMap
    • editing / Naive HashMap
    • source code / Naive HashMap
    • insertion, examining / Naive HashMap
    • implementation / Naive HashMap, A better naive HashMap
    • testing, with QuickCheck / Testing with QuickCheck
    • testing, with American Fuzzy Lop / Testing with American Fuzzy Lop
    • performance testing, with criterion / Performance testing with Criterion
    • inspecting, with Valgrind Suite / Inspecting with the Valgrind Suite
    • inspecting, with Linux perf / Inspecting with Linux perf

O

  • Option / Option
  • OS processes / Data parallelism and OS processes – evolving corewars players

P

  • pMARS
    • URL / Corewars
  • pointers
    • about / Pointers to memory
    • memory, allocating / Allocating and deallocating memory
    • memory, deallocating / Allocating and deallocating memory
    • type, size / The size of a type
    • static dispatch / Static and dynamic dispatch
    • dynamic dispatch / Static and dynamic dispatch
    • zero sized types (ZST) / Zero sized types
    • boxed types / Boxed types
    • custom allocators / Custom allocators
  • projects
    • fuzzing / Fuzzing
    • Seer / Seer, a symbolic execution engine for Rust
  • property-based testing / Testing with QuickCheck
  • Python
    • Rust, embedding into / Into Python

Q

  • quantiles library
    • URL / A telemetry server, Into C
  • queue
    • defining / An incorrect atomic queue
  • QuickCheck
    • naive HashMap, testing / Testing with QuickCheck
    • about / Testing with QuickCheck
  • quiescent-state-based reclamation (QSRB) / Options to correct the incorrect queue

R

  • rayon
    • about / rayon – parallel iterators
    • URL / rayon – parallel iterators
    • rayon-futures / rayon – parallel iterators
    • rayon-core / rayon – parallel iterators
    • dependencies / rayon – parallel iterators
    • implementing / rayon – parallel iterators
  • Rc / Rc
  • reactor pattern
    • reference link / Futures and async/await
  • Redox project
    • URL / Hazard pointers
  • RefCell / Cell and RefCell
  • reference counting
    • about / Reference counting
    • tradeoffs / Tradeoffs
  • Ring
    • flaws / The flaw of the Ring
  • rlua
    • URL / Embedding Lua into Rust
  • rocket preparation problem
    • example / The rocket preparation problem
  • rope bridge problem
    • example / The rope bridge problem
  • Rust
    • setting up / Getting set up
    • cargo project, creating / The interesting part
    • programs, debugging / Debugging Rust programs
    • reference / Vec
    • C, embedding into / Embedding C into Rust – feruscore without processes
    • Lua, embedding / Embedding Lua into Rust
    • embedding / Embedding Rust
    • embedding, into C / Into C, The Rust side, The C side
    • embedding, into Python / Into Python
    • embedding, into Erlang/Elixir / Into Erlang/Elixir
    • about / Near-term improvements
    • SIMD system / SIMD
    • hex encoding / Hex encoding
    • futures / Futures and async/await
    • async/await syntax / Futures and async/await
    • specialization / Specialization
    • community / The community
    • community, reference / The community
  • Rust PR 31844
    • reference / A better naive HashMap
  • Rust RFC 1210
    • reference / A better naive HashMap
  • RwLock
    • about / Read many, write exclusive locks – RwLock
    • using / Read many, write exclusive locks – RwLock
    • example / Read many, write exclusive locks – RwLock

S

  • seer
    • about / Seer, a symbolic execution engine for Rust
    • reference link / Seer, a symbolic execution engine for Rust
  • semaphore
    • implementing / Semaphore
    • bounded semaphore / Semaphore
    • counting semaphore / Semaphore
    • binary semaphore / Semaphore
    • binary semaphore, versus mutex / Binary semaphore, or, a less wasteful mutex
  • Send trait / Sync and Send
  • serde
    • URL / Representing the domain
  • SIMD system / SIMD
  • slowloris attack
    • about / Slowloris – attacking thread-per-connection servers
    • server / The server
    • client / The client
    • thread-pooling server / A thread-pooling server
  • soft-mutex / Mutexes
  • specialization / Specialization
  • Stable SIMD
    • reference link / Hex encoding
  • standard library
    • dissecting / Implementations
    • Option / Option
    • Cell / Cell and RefCell
    • RefCell / Cell and RefCell
    • Rc / Rc
    • Vec / Vec
  • static dispatch / Static and dynamic dispatch
  • symbolic execution
    • reference link / Seer, a symbolic execution engine for Rust
  • synchronization
    • building / Building synchronization
    • mutex / Mutexes
    • incorrect atomic queue / An incorrect atomic queue
    • semaphore / Semaphore
  • Sync trait
    • about / Sync and Send
    • threads, racing / Racing threads

T

  • telemetry server
    • building / A telemetry server
  • tempdir
    • URL / QuickCheck and loops
    • using / QuickCheck and loops
  • thread-pooling library
    • reference / Looking into thread pool
  • thread pooling
    • about / Thread pooling
    • slowloris attack / Slowloris – attacking thread-per-connection servers
    • implementation / Looking into thread pool
    • Ethernet sniffer / The Ethernet sniffer
    • iterators / Iterators
    • corewars players, evolving / Data parallelism and OS processes – evolving corewars players
    • feruscore / Feruscore – a Corewars evolver
  • threads
    • racing / Racing threads
    • Ring, flaws / The flaw of the Ring
    • safety measures / Getting back to safety
    • safety, by exclusion / Safety by exclusion
    • MPSC, using / Using MPSC
  • toml crate
    • URL / Main

U

  • unsafe blocks / Should I use unsafe?

V

  • Valgrind Suite
    • naive HashMap, inspecting / Inspecting with the Valgrind Suite
  • Vec / Vec

Z

  • zero sized types (ZST) / Zero sized types