|
| 1 | +This repository contains the code examples, data structures, and links from |
| 2 | +[Rust Atomics and Locks](https://marabos.nl/atomics/). |
| 3 | + |
| 4 | +The examples from chapters 1, 2, 3, and 8 can be found in [examples/](examples/). |
| 5 | +The data structures from chapters 4, 5, 6, and 9 can be found in [src/](src/). |
| 6 | + |
| 7 | +### Chapter 1 — Basics of Rust Concurrency |
| 8 | + |
| 9 | +- [examples/ch1-01-hello.rs](examples/ch1-01-hello.rs) |
| 10 | +- [examples/ch1-02-hello-join.rs](examples/ch1-02-hello-join.rs) |
| 11 | +- [examples/ch1-03-spawn-closure.rs](examples/ch1-03-spawn-closure.rs) |
| 12 | +- [examples/ch1-04-scoped-threads.rs](examples/ch1-04-scoped-threads.rs) |
| 13 | +- [examples/ch1-05-rc.rs](examples/ch1-05-rc.rs) |
| 14 | +- [examples/ch1-06-cell.rs](examples/ch1-06-cell.rs) |
| 15 | +- [examples/ch1-07-refcell.rs](examples/ch1-07-refcell.rs) |
| 16 | +- [examples/ch1-08-mutex.rs](examples/ch1-08-mutex.rs) |
| 17 | +- [examples/ch1-09-sleep-before-unlock.rs](examples/ch1-09-sleep-before-unlock.rs) |
| 18 | +- [examples/ch1-10-unlock-before-sleep.rs](examples/ch1-10-unlock-before-sleep.rs) |
| 19 | +- [examples/ch1-11-thread-parking.rs](examples/ch1-11-thread-parking.rs) |
| 20 | +- [examples/ch1-12-condvar.rs](examples/ch1-12-condvar.rs) |
| 21 | + |
| 22 | +### Chapter 2 — Atomics |
| 23 | + |
| 24 | +- [examples/ch2-01-stop-flag.rs](examples/ch2-01-stop-flag.rs) |
| 25 | +- [examples/ch2-02-progress-reporting.rs](examples/ch2-02-progress-reporting.rs) |
| 26 | +- [examples/ch2-03-progress-reporting-unpark.rs](examples/ch2-03-progress-reporting-unpark.rs) |
| 27 | +- [examples/ch2-04-lazy-init.rs](examples/ch2-04-lazy-init.rs) |
| 28 | +- [examples/ch2-05-fetch-add.rs](examples/ch2-05-fetch-add.rs) |
| 29 | +- [examples/ch2-06-progress-reporting-multiple-threads.rs](examples/ch2-06-progress-reporting-multiple-threads.rs) |
| 30 | +- [examples/ch2-07-statistics.rs](examples/ch2-07-statistics.rs) |
| 31 | +- [examples/ch2-08-id-allocation.rs](examples/ch2-08-id-allocation.rs) |
| 32 | +- [examples/ch2-09-id-allocation-panic.rs](examples/ch2-09-id-allocation-panic.rs) |
| 33 | +- [examples/ch2-10-id-allocation-subtract-before-panic.rs](examples/ch2-10-id-allocation-subtract-before-panic.rs) |
| 34 | +- [examples/ch2-11-increment-with-compare-exchange.rs](examples/ch2-11-increment-with-compare-exchange.rs) |
| 35 | +- [examples/ch2-12-id-allocation-without-overflow.rs](examples/ch2-12-id-allocation-without-overflow.rs) |
| 36 | +- [examples/ch2-13-lazy-one-time-init.rs](examples/ch2-13-lazy-one-time-init.rs) |
| 37 | + |
| 38 | +### Chapter 3 — Memory Ordering |
| 39 | + |
| 40 | +- [examples/ch3-01-relaxed.rs](examples/ch3-01-relaxed.rs) |
| 41 | +- [examples/ch3-02-spawn-join.rs](examples/ch3-02-spawn-join.rs) |
| 42 | +- [examples/ch3-03-total-modification-order.rs](examples/ch3-03-total-modification-order.rs) |
| 43 | +- [examples/ch3-04-total-modification-order-2.rs](examples/ch3-04-total-modification-order-2.rs) |
| 44 | +- [examples/ch3-05-out-of-thin-air.rs](examples/ch3-05-out-of-thin-air.rs) |
| 45 | +- [examples/ch3-06-release-acquire.rs](examples/ch3-06-release-acquire.rs) |
| 46 | +- [examples/ch3-07-release-acquire-unsafe.rs](examples/ch3-07-release-acquire-unsafe.rs) |
| 47 | +- [examples/ch3-08-lock.rs](examples/ch3-08-lock.rs) |
| 48 | +- [examples/ch3-09-lazy-init-box.rs](examples/ch3-09-lazy-init-box.rs) |
| 49 | +- [examples/ch3-10-seqcst.rs](examples/ch3-10-seqcst.rs) |
| 50 | +- [examples/ch3-11-fence.rs](examples/ch3-11-fence.rs) |
| 51 | + |
| 52 | +### Chapter 4 — Building Our Own Spin Lock |
| 53 | + |
| 54 | +- [src/ch4_spin_lock/s1_minimal.rs](src/ch4_spin_lock/s1_minimal.rs) |
| 55 | +- [src/ch4_spin_lock/s2_unsafe.rs](src/ch4_spin_lock/s2_unsafe.rs) |
| 56 | +- [src/ch4_spin_lock/s3_guard.rs](src/ch4_spin_lock/s3_guard.rs) |
| 57 | + |
| 58 | +### Chapter 5 — Building Our Own Channels |
| 59 | + |
| 60 | +- [src/ch5_channels/s1_simple.rs](src/ch5_channels/s1_simple.rs) |
| 61 | +- [src/ch5_channels/s2_unsafe.rs](src/ch5_channels/s2_unsafe.rs) |
| 62 | +- [src/ch5_channels/s3_checks.rs](src/ch5_channels/s3_checks.rs) |
| 63 | +- [src/ch5_channels/s3_single_atomic.rs](src/ch5_channels/s3_single_atomic.rs) |
| 64 | +- [src/ch5_channels/s4_types.rs](src/ch5_channels/s4_types.rs) |
| 65 | +- [src/ch5_channels/s5_borrowing.rs](src/ch5_channels/s5_borrowing.rs) |
| 66 | +- [src/ch5_channels/s6_blocking.rs](src/ch5_channels/s6_blocking.rs) |
| 67 | + |
| 68 | +### Chapter 6 — Building Our Own “Arc” |
| 69 | + |
| 70 | +- [src/ch6_arc/s1_basic.rs](src/ch6_arc/s1_basic.rs) |
| 71 | +- [src/ch6_arc/s2_weak.rs](src/ch6_arc/s2_weak.rs) |
| 72 | +- [src/ch6_arc/s3_optimized.rs](src/ch6_arc/s3_optimized.rs) |
| 73 | + |
| 74 | +### Chapter 7 — Understanding the Processor |
| 75 | + |
| 76 | +- https://godbolt.org/ |
| 77 | + |
| 78 | +### Chapter 8 — Operating System Primitives |
| 79 | + |
| 80 | +- [examples/ch8-01-futex.rs](examples/ch8-01-futex.rs) |
| 81 | + |
| 82 | +### Chapter 9 — Building Our Own Locks |
| 83 | + |
| 84 | +- [src/ch9_locks/mutex_1.rs](src/ch9_locks/mutex_1.rs) |
| 85 | +- [src/ch9_locks/mutex_2.rs](src/ch9_locks/mutex_2.rs) |
| 86 | +- [src/ch9_locks/mutex_3.rs](src/ch9_locks/mutex_3.rs) |
| 87 | +- [src/ch9_locks/condvar_1.rs](src/ch9_locks/condvar_1.rs) |
| 88 | +- [src/ch9_locks/condvar_2.rs](src/ch9_locks/condvar_2.rs) |
| 89 | +- [src/ch9_locks/rwlock_1.rs](src/ch9_locks/rwlock_1.rs) |
| 90 | +- [src/ch9_locks/rwlock_2.rs](src/ch9_locks/rwlock_2.rs) |
| 91 | +- [src/ch9_locks/rwlock_3.rs](src/ch9_locks/rwlock_3.rs) |
| 92 | + |
| 93 | +### Chapter 10 — Ideas and Inspiration |
| 94 | + |
| 95 | +- [Wikipedia article on semaphores](https://en.wikipedia.org/wiki/Semaphore_(programming)) |
| 96 | +- [Stanford University course notes on semaphores](https://see.stanford.edu/materials/icsppcs107/23-Concurrency-Examples.pdf) |
| 97 | +- [Wikipedia article on the read-copy-update pattern](https://en.wikipedia.org/wiki/Read-copy-update) |
| 98 | +- [LWN article "What is RCU, Fundamentally?"](https://lwn.net/Articles/262464/) |
| 99 | +- [Wikipedia article on non-blocking linked lists](https://en.wikipedia.org/wiki/Non-blocking_linked_list) |
| 100 | +- [LWN article "Using RCU for Linked Lists—A Case Study"](https://lwn.net/Articles/610972/) |
| 101 | +- [Notes on the implementation of Windows SRW locks](https://github.com/rust-lang/rust/issues/93740#issuecomment-1064139337) |
| 102 | +- [A Rust implementation of queue-based locks](https://github.com/kprotty/usync) |
| 103 | +- [WebKit blog post, "Locking in WebKit"](https://webkit.org/blog/6161/locking-in-webkit/) |
| 104 | +- [Documentation of the `parking_lot` crate](https://docs.rs/parking_lot) |
| 105 | +- [Wikipedia article on Linux's Seqlock](https://en.wikipedia.org/wiki/Seqlock) |
| 106 | +- [Rust RFC 3301, `AtomicPerByte`](https://rust.tf/rfc3301) |
| 107 | +- [Documentation of the `seqlock` crate](https://docs.rs/seqlock) |
| 108 | + |
| 109 | +### License |
| 110 | + |
| 111 | +You may use all code in this repository for any purpose. |
| 112 | + |
| 113 | +Attribution is appreciated, but not required. |
| 114 | +An attrubution usually includes the book title, author, publisher, and ISBN. |
| 115 | +For example: "_Rust Atomics and Locks_ by Mara Bos (O’Reilly). Copyright 2023 Mara Bos, 978-1-098-11944-7." |
0 commit comments