blob: c114541075a2fe425e696f03c8388ad2cd957d29 (
plain)
use crate::lockable::RawLock;
/// Locks the locks in the order they are given. This causes deadlock if the
/// locks contain duplicates, or if this is called by multiple threads with the
/// locks in different orders.
pub unsafe fn ordered_try_lock(locks: &[&dyn RawLock]) -> bool {
unsafe {
for (i, lock) in locks.iter().enumerate() {
// safety: we have the thread key
let success = lock.raw_try_lock();
if !success {
for lock in &locks[0..i] {
// safety: this lock was already acquired
lock.raw_unlock();
}
return false;
}
}
true
}
}
/// Locks the locks in the order they are given. This causes deadlock if this
/// is called by multiple threads with the locks in different orders.
pub unsafe fn ordered_try_read(locks: &[&dyn RawLock]) -> bool {
unsafe {
for (i, lock) in locks.iter().enumerate() {
// safety: we have the thread key
let success = lock.raw_try_read();
if !success {
for lock in &locks[0..i] {
// safety: this lock was already acquired
lock.raw_unlock_read();
}
return false;
}
}
true
}
}
|