• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H03-May-2022-4,8313,365

tests/H03-May-2022-2721

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D10-Apr-202074 65

.gitignoreH A D09-May-201618 32

.travis.ymlH A D10-Apr-20202.4 KiB9279

CHANGELOG.mdH A D10-Apr-20204.8 KiB12186

Cargo.tomlH A D10-Apr-20201.3 KiB4136

Cargo.toml.orig-cargoH A D10-Apr-2020885 3226

LICENSE-APACHEH A D11-May-201610.6 KiB202169

LICENSE-MITH A D11-May-20161 KiB2622

README.mdH A D06-Apr-20206.5 KiB141110

appveyor.ymlH A D24-Nov-20191.5 KiB6056

bors.tomlH A D10-Apr-202056 43

README.md

1parking_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
12This library provides implementations of `Mutex`, `RwLock`, `Condvar` and
13`Once` that are smaller, faster and more flexible than those in the Rust
14standard library, as well as a `ReentrantMutex` type which supports recursive
15locking. It also exposes a low-level API for creating your own efficient
16synchronization primitives.
17
18When tested on x86_64 Linux, `parking_lot::Mutex` was found to be 1.5x
19faster than `std::sync::Mutex` when uncontended, and up to 5x faster when
20contended from multiple threads. The numbers for `RwLock` vary depending on
21the number of reader and writer threads, but are almost always faster than
22the standard library `RwLock`, and even up to 50x faster in some cases.
23
24## Features
25
26The primitives provided by this library have several advantages over those
27in the Rust standard library:
28
291. `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.
352. 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!`.
403. Uncontended lock acquisition and release is done through fast inline
41   paths which only require a single atomic operation.
424. 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.
455. 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.
486. `Condvar`, `RwLock` and `Once` work on Windows XP, unlike the standard
49   library versions of those types.
507. `RwLock` takes advantage of hardware lock elision on processors that
51   support it, which can lead to huge performance wins with many readers.
528. `RwLock` uses a task-fair locking policy, which avoids reader and writer
53   starvation, whereas the standard library version makes no guarantees.
549. `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.
5610. `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.
5911. `RwLock` supports atomically downgrading a write lock into a read lock.
6012. `Mutex` and `RwLock` allow raw unlocking without a RAII guard object.
6113. `Mutex<()>` and `RwLock<()>` allow raw locking without a RAII guard
62    object.
6314. `Mutex` and `RwLock` support [eventual fairness](https://trac.webkit.org/changeset/203350)
64    which allows them to be fair on average without sacrificing performance.
6515. A `ReentrantMutex` type which supports recursive locking.
6616. 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.
6917. `RwLock` supports atomically upgrading an "upgradable" read lock into a
70    write lock.
7118. 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
77To keep these primitives small, all thread queuing and suspending
78functionality is offloaded to the *parking lot*. The idea behind this is
79based on the Webkit [`WTF::ParkingLot`](https://webkit.org/blog/6161/locking-in-webkit/)
80class, which essentially consists of a hash table mapping of lock addresses
81to queues of parked (sleeping) threads. The Webkit parking lot was itself
82inspired by Linux [futexes](http://man7.org/linux/man-pages/man2/futex.2.html),
83but it is more powerful since it allows invoking callbacks while holding a queue
84lock.
85
86## Nightly vs stable
87
88There 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
96To enable nightly-only functionality, you need to enable the `nightly` feature
97in Cargo (see below).
98
99## Usage
100
101Add this to your `Cargo.toml`:
102
103```toml
104[dependencies]
105parking_lot = "0.10"
106```
107
108To enable nightly-only features, add this to your `Cargo.toml` instead:
109
110```toml
111[dependencies]
112parking_lot = { version = "0.10", features = ["nightly"] }
113```
114
115The experimental deadlock detector can be enabled with the
116`deadlock_detection` Cargo feature.
117
118The core parking lot API is provided by the `parking_lot_core` crate. It is
119separate from the synchronization primitives in the `parking_lot` crate so that
120changes to the core API do not cause breaking changes for users of `parking_lot`.
121
122## Minimum Rust version
123
124The current minimum required Rust version is 1.36. Any change to this is
125considered a breaking change and will require a major version bump.
126
127## License
128
129Licensed 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
134at your option.
135
136### Contribution
137
138Unless you explicitly state otherwise, any contribution intentionally submitted
139for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
140additional terms or conditions.
141