Debian Codegraph @

debian/rust-vendor/parking_lot/README.md

raw
1 parking_lot
2 ============
3
4 [![Rust](https://github.com/Amanieu/parking_lot/workflows/Rust/badge.svg)](https://github.com/Amanieu/parking_lot/actions)
5 [![Crates.io](https://img.shields.io/crates/v/parking_lot.svg)](https://crates.io/crates/parking_lot)
6
7 [Documentation (synchronization primitives)](https://docs.rs/parking_lot/)
8
9 [Documentation (core parking lot API)](https://docs.rs/parking_lot_core/)
10
11 [Documentation (type-safe lock API)](https://docs.rs/lock_api/)
12
13 This library provides implementations of `Mutex`, `RwLock`, `Condvar` and
14 `Once` that are smaller, faster and more flexible than those in the Rust
15 standard library, as well as a `ReentrantMutex` type which supports recursive
16 locking. It also exposes a low-level API for creating your own efficient
17 synchronization primitives.
18
19 When tested on x86_64 Linux, `parking_lot::Mutex` was found to be 1.5x
20 faster than `std::sync::Mutex` when uncontended, and up to 5x faster when
21 contended from multiple threads. The numbers for `RwLock` vary depending on
22 the number of reader and writer threads, but are almost always faster than
23 the standard library `RwLock`, and even up to 50x faster in some cases.
24
25 ## Features
26
27 The primitives provided by this library have several advantages over those
28 in the Rust standard library:
29
30 1. `Mutex` and `Once` only require 1 byte of storage space, while `Condvar`
31 and `RwLock` only require 1 word of storage space. On the other hand on
32 some platforms (macOS and a few others) the standard library primitives
33 require a dynamically allocated `Box` to hold OS-specific synchronization
34 primitives. The small size of `Mutex` in particular encourages the use
35 of fine-grained locks to increase parallelism.
36 2. Uncontended lock acquisition and release is done through fast inline
37 paths which only require a single atomic operation.
38 3. Microcontention (a contended lock with a short critical section) is
39 efficiently handled by spinning a few times while trying to acquire a
40 lock.
41 4. The locks are adaptive and will suspend a thread after a few failed spin
42 attempts. This makes the locks suitable for both long and short critical
43 sections.
44 5. `Condvar`, `RwLock` and `Once` work on Windows XP, unlike the standard
45 library versions of those types.
46 6. `RwLock` takes advantage of hardware lock elision on processors that
47 support it, which can lead to huge performance wins with many readers.
48 This must be enabled with the `hardware-lock-elision` feature.
49 7. `RwLock` uses a task-fair locking policy, which avoids reader and writer
50 starvation, whereas the standard library version makes no guarantees.
51 8. `Condvar` is guaranteed not to produce spurious wakeups. A thread will
52 only be woken up if it timed out or it was woken up by a notification.
53 9. `Condvar::notify_all` will only wake up a single thread and requeue the
54 rest to wait on the associated `Mutex`. This avoids a thundering herd
55 problem where all threads try to acquire the lock at the same time.
56 10. `RwLock` supports atomically downgrading a write lock into a read lock.
57 11. `Mutex` and `RwLock` allow raw unlocking without a RAII guard object.
58 12. `Mutex<()>` and `RwLock<()>` allow raw locking without a RAII guard
59 object.
60 13. `Mutex` and `RwLock` support [eventual fairness](https://trac.webkit.org/changeset/203350)
61 which allows them to be fair on average without sacrificing performance.
62 14. A `ReentrantMutex` type which supports recursive locking.
63 15. An *experimental* deadlock detector that works for `Mutex`,
64 `RwLock` and `ReentrantMutex`. This feature is disabled by default and
65 can be enabled via the `deadlock_detection` feature.
66 16. `RwLock` supports atomically upgrading an "upgradable" read lock into a
67 write lock.
68 17. Optional support for [serde](https://docs.serde.rs/serde/). Enable via the
69 feature `serde`. **NOTE!** this support is for `Mutex`, `ReentrantMutex`,
70 and `RwLock` only; `Condvar` and `Once` are not currently supported.
71 18. Lock guards can be sent to other threads when the `send_guard` feature is
72 enabled.
73
74 ## The parking lot
75
76 To keep these primitives small, all thread queuing and suspending
77 functionality is offloaded to the *parking lot*. The idea behind this is
78 based on the Webkit [`WTF::ParkingLot`](https://webkit.org/blog/6161/locking-in-webkit/)
79 class, which essentially consists of a hash table mapping of lock addresses
80 to queues of parked (sleeping) threads. The Webkit parking lot was itself
81 inspired by Linux [futexes](https://man7.org/linux/man-pages/man2/futex.2.html),
82 but it is more powerful since it allows invoking callbacks while holding a queue
83 lock.
84
85 ## Nightly vs stable
86
87 There are a few restrictions when using this library on stable Rust:
88
89 - The `wasm32-unknown-unknown` target is only fully supported on nightly with
90 `-C target-feature=+atomics` in `RUSTFLAGS` and `-Zbuild-std=panic_abort,std`
91 passed to cargo. parking_lot will work mostly fine on stable, the only
92 difference is it will panic instead of block forever if you hit a deadlock.
93 Just make sure not to enable `-C target-feature=+atomics` on stable as that
94 will allow wasm to run with multiple threads which will completely break
95 parking_lot's concurrency guarantees.
96
97 To enable nightly-only functionality, you need to enable the `nightly` feature
98 in Cargo (see below).
99
100 ## Usage
101
102 Add this to your `Cargo.toml`:
103
104 ```toml
105 [dependencies]
106 parking_lot = "0.12"
107 ```
108
109 To enable nightly-only features, add this to your `Cargo.toml` instead:
110
111 ```toml
112 [dependencies]
113 parking_lot = { version = "0.12", features = ["nightly"] }
114 ```
115
116 The experimental deadlock detector can be enabled with the
117 `deadlock_detection` Cargo feature.
118
119 To allow sending `MutexGuard`s and `RwLock*Guard`s to other threads, enable the
120 `send_guard` option.
121
122 Note that the `deadlock_detection` and `send_guard` features are incompatible
123 and cannot be used together.
124
125 Hardware lock elision support for x86 can be enabled with the
126 `hardware-lock-elision` feature. This requires Rust 1.59 due to the use of
127 inline assembly.
128
129 The core parking lot API is provided by the `parking_lot_core` crate. It is
130 separate from the synchronization primitives in the `parking_lot` crate so that
131 changes to the core API do not cause breaking changes for users of `parking_lot`.
132
133 ## Minimum Rust version
134
135 The current minimum required Rust version is 1.71, but this may change at any time.
136
137 ## License
138
139 Licensed under either of
140
141 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or https://www.apache.org/licenses/LICENSE-2.0)
142 * MIT license ([LICENSE-MIT](LICENSE-MIT) or https://opensource.org/licenses/MIT)
143
144 at your option.
145
146 ### Contribution
147
148 Unless you explicitly state otherwise, any contribution intentionally submitted
149 for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
150 additional terms or conditions.