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

#1159 in Concurrency

Download history 246/week @ 2024-07-21 160/week @ 2024-07-28 151/week @ 2024-08-04 209/week @ 2024-08-11 149/week @ 2024-08-18 193/week @ 2024-08-25 150/week @ 2024-09-01 148/week @ 2024-09-08 184/week @ 2024-09-15 150/week @ 2024-09-22 149/week @ 2024-09-29 2/week @ 2024-10-06 103/week @ 2024-10-13 95/week @ 2024-10-20 128/week @ 2024-10-27 111/week @ 2024-11-03

438 downloads per month
Used in 201 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