#mutex #shared #condvar #rwlock #locking #rest

shared-mutex

A RwLock that can be used with a Condvar

7 releases

Uses old Rust 2015

0.3.1 Jun 2, 2016
0.3.0 Feb 16, 2016
0.2.3 Feb 12, 2016
0.1.0 Feb 6, 2016

#345 in Concurrency

Download history 7/week @ 2022-10-12 7/week @ 2022-10-19 15/week @ 2022-10-26 21/week @ 2022-11-02 19/week @ 2022-11-09 19/week @ 2022-11-16 6/week @ 2022-11-23 9/week @ 2022-11-30 21/week @ 2022-12-07 7/week @ 2022-12-14 22/week @ 2022-12-21 12/week @ 2022-12-28 4/week @ 2023-01-04 25/week @ 2023-01-11 250/week @ 2023-01-18 205/week @ 2023-01-25

486 downloads per month
Used in 161 crates (3 directly)

MIT license

40KB
734 lines

shared-mutex

A reader-writer lock that can be used with a Condvar.

Documentation

Reader writer locks can be more efficient than mutual exclusion locks in cases where many operations require read-only access to the data, as they can significantly lower contention by allowing multiple readers to proceed at once.

As a result, most operating system's native libraries come with a reader-writer lock implementation in addition to mutual exclusion locks. However, on unix systems, this reader-writer lock (which is used in std::sync::RwLock) cannot be associated with a condition variable, which are limited to the native mutex (which is used in std::sync::Mutex). On Windows the native reader-writer lock supports waiting on a condition variable but this functionality is not yet exposed in the standard library.

SharedMutex is an implementation of a reader-writer lock without this (and other) restrictions - shared mutex guards provide methods for waiting on condition variables. In addition to this extremely useful new API, this crate also features some useful combinators, such as mapped guards, and removes other restrictions by making the guard types Send and Sync.

The library also provides some other useful APIs, like a RawSharedMutex and utilities for building your own internally poisoned interior mutability types in the poison module (these are used in the implementation of SharedMutex).

Safety

The locking strategy has been adapted from the implementation of std::shared_mutex from libc++ in llvm, and has the same fairness and starvation guarantees (readers cannot starve writers, waiting writers block readers).

I have carefully reviewed the code for safety in addition to using automated tests, but as with all concurrent and unsafe code, more eyes and brains would be better.

Usage

Use the crates.io repository; add this to your Cargo.toml along with the rest of your dependencies:

[dependencies]
shared-mutex = "0.2"

Author

Jonathan Reem is the primary author and maintainer of shared-mutex.

License

MIT

Dependencies