Sort Lock
A crate providing ordered locking. That is locks that will always lock in the same order regardless of the order in which the methods are called. This reduces deadlocks caused by cycles as one lock waits for another. In particular the following case cannot happen when exclusively using lock sorting:
- Thread A acquires
lock1. - Thread B acquires
lock2. - Thread A tries to acquire
lock2but it is locked by thread B. - Thread B tries to acquire
lock1but it is locked by thread A. - Neither thread can continue and will not unlock so a deadlock has occured.
With lock sorting this cannot occur as locks are always locked in the same order for both
threads. This is done by first requesting to lock each lock. Then, by placing the locks in a
tuple and calling the lock_all method, the locks will be locked in the same order regardless
of their order in the tuple.
To allow for sorted locking, this crates provides two new types of lock:
SortMuted- A sorted version ofMutex.SortRwLock- A sorted version ofRwLock.
Examples
use ;
let lock1 = new;
let lock2 = new;
// Here lock1 is locked then lock2.
let = .lock_all;
println!;
println!;
// Unlock so we can lock again.
drop;
drop;
// Despite the order change the same is true here.
let = .lock_all;
println!;
println!;