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