#reader-writer #lock #read-lock #thread #rwlock #data #access

seqlock

A reader-writer lock that provides extremely fast read access without starving writers

4 releases

0.2.0 Mar 30, 2023
0.1.2 Oct 27, 2020
0.1.1 Jul 10, 2016
0.1.0 May 28, 2016

#442 in Concurrency

Download history 13072/week @ 2024-01-03 10968/week @ 2024-01-10 14887/week @ 2024-01-17 12839/week @ 2024-01-24 11474/week @ 2024-01-31 10035/week @ 2024-02-07 8905/week @ 2024-02-14 11270/week @ 2024-02-21 13493/week @ 2024-02-28 12906/week @ 2024-03-06 12700/week @ 2024-03-13 15076/week @ 2024-03-20 15671/week @ 2024-03-27 17808/week @ 2024-04-03 17348/week @ 2024-04-10 16467/week @ 2024-04-17

70,083 downloads per month
Used in 90 crates (7 directly)

Apache-2.0/MIT

13KB
115 lines

SeqLock

Crates.io

Documentation

This library provides the SeqLock type, which is a form of reader-writer lock that is heavily optimized for readers.

In certain situations, SeqLock can be two orders of magnitude faster than the standard library RwLock type. Another advantage is that readers cannot starve writers: a writer will never block even if there are readers currently accessing the SeqLock.

The only downside of SeqLock is that it only works on types that are Copy. This means that it is unsuitable for types that contains pointers to owned data.

You should instead use a RwLock if you need a reader-writer lock for types that are not Copy.

Implementation

The implementation is based on the Linux seqlock type. Each SeqLock contains a sequence counter which tracks modifications to the locked data. The basic idea is that a reader will perform the following operations:

  1. Read the sequence counter.
  2. Read the data in the lock.
  3. Read the sequence counter again.
  4. If the two sequence counter values differ, loop back and try again.
  5. Otherwise return the data that was read in step 2.

Similarly, a writer will increment the sequence counter before and after writing to the data in the lock. Once a reader sees that the sequence counter has not changed while it was reading the data, it can safely return that data to the caller since it is known to be in a consistent state.

Example

use seqlock::SeqLock;

let lock = SeqLock::new(5);

{
    // Writing to the data involves a lock
    let mut w = lock.lock_write();
    *w += 1;
    assert_eq!(*w, 6);
}

{
    // Reading the data is a very fast operation
    let r = lock.read();
    assert_eq!(r, 6);
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0.4–6.5MB
~11K SLoC