lockpool
This library is not maintained anymore. Please use the lockable crate instead.
It offers a LockPool data structure with pretty much the same functionality that was offered by this crate. There is an example for a lock pool in the README.
This library offers a pool of locks where individual locks can be locked/unlocked by key. It initially considers all keys as "unlocked", but they can be locked and if a second thread tries to acquire a lock for the same key, they will have to wait.
use ;
let pool = new;
let guard1 = pool.lock?;
let guard2 = pool.lock?;
// This next line would cause a deadlock or panic because `4` is already locked on this thread
// let guard3 = pool.lock(4)?;
// After dropping the corresponding guard, we can lock it again
drop;
let guard3 = pool.lock?;
You can use an arbitrary type to index locks by, as long as that type implements PartialEq + Eq + Hash + Clone + Debug.
use ;
;
let pool = new;
let guard = pool.lock?;
Under the hood, a LockPool is a HashMap of Mutexes, with some logic making sure there aren't any race conditions when accessing the hash map.
If the tokio feature is enabled, then this crate also offers [TokioLockPool] which allows locks to be held across await points.
License: MIT OR Apache-2.0