#mutex #condvar #rwlock #once #thread

parking_lot_core

An advanced API for creating custom synchronization primitives

22 releases

0.3.0 Aug 29, 2018
0.2.14 Apr 27, 2018
0.2.13 Feb 20, 2018
0.2.9 Dec 29, 2017
0.2.0 Oct 4, 2016

#6 in Concurrency

Download history 17662/week @ 2018-08-04 19901/week @ 2018-08-11 17400/week @ 2018-08-18 21002/week @ 2018-08-25 35448/week @ 2018-09-01 39738/week @ 2018-09-08 47855/week @ 2018-09-15 47240/week @ 2018-09-22 46341/week @ 2018-09-29 54097/week @ 2018-10-06 51581/week @ 2018-10-13 52169/week @ 2018-10-20 46609/week @ 2018-10-27

78,041 downloads per month
Used in 1,841 crates (6 directly)

Apache-2.0/MIT

102KB
2K SLoC


lib.rs:

This library exposes a low-level API for creating your own efficient synchronization primitives.

The parking lot

To keep synchronization primitives small, all thread queuing and suspending functionality is offloaded to the parking lot. The idea behind this is based on the Webkit WTF::ParkingLot class, which essentially consists of a hash table mapping of lock addresses to queues of parked (sleeping) threads. The Webkit parking lot was itself inspired by Linux futexes, but it is more powerful since it allows invoking callbacks while holding a queue lock.

There are two main operations that can be performed on the parking lot:

  • Parking refers to suspending the thread while simultaneously enqueuing it on a queue keyed by some address.
  • Unparking refers to dequeuing a thread from a queue keyed by some address and resuming it.

See the documentation of the individual functions for more details.

Building custom synchronization primitives

Building custom synchronization primitives is very simple since the parking lot takes care of all the hard parts for you. A simple example for a custom primitive would be to integrate a Mutex inside another data type. Since a mutex only requires 2 bits, it can share space with other data. For example, one could create an ArcMutex type that combines the atomic reference count and the two mutex bits in the same atomic word.

Dependencies

~1MB
~15K SLoC