48 releases

0.9.10 Apr 25, 2024
0.9.9 Oct 17, 2023
0.9.8 Jun 5, 2023
0.9.7 Feb 1, 2023
0.2.0 Oct 4, 2016

#1190 in Concurrency

Download history 2906229/week @ 2024-07-29 3106189/week @ 2024-08-05 3204504/week @ 2024-08-12 3028596/week @ 2024-08-19 3356853/week @ 2024-08-26 3152165/week @ 2024-09-02 3118766/week @ 2024-09-09 2858200/week @ 2024-09-16 3197472/week @ 2024-09-23 3712610/week @ 2024-09-30 4158336/week @ 2024-10-07 4020035/week @ 2024-10-14 3193118/week @ 2024-10-21 2947196/week @ 2024-10-28 3065459/week @ 2024-11-04 3301985/week @ 2024-11-11

12,859,455 downloads per month
Used in 32,486 crates (27 directly)

MIT/Apache

130KB
2.5K SLoC

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

~0–5.5MB
~20K SLoC