Amanieu / parking_lot

Compact and efficient synchronization primitives for Rust. Also provides an API for creating custom synchronization primitives.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

lock_api: `RwLockReadGuard::unlocked` incorrectly claims fairness

CAD97 opened this issue · comments

RwLockReadGuard::unlocked:

/// Temporarily unlocks the `RwLock` to execute the given function.
///
/// The `RwLock` is unlocked a fair unlock protocol.
///
/// This is safe because `&mut` guarantees that there exist no other
/// references to the data protected by the `RwLock`.
#[inline]
pub fn unlocked<F, U>(s: &mut Self, f: F) -> U
where
F: FnOnce() -> U,
{
// Safety: An RwLockReadGuard always holds a shared lock.
unsafe {
s.rwlock.raw.unlock_shared();
}
defer!(s.rwlock.raw.lock_shared());
f()
}

RwLockReadGuard::unlocked_fair:

/// Temporarily unlocks the `RwLock` to execute the given function.
///
/// The `RwLock` is unlocked a fair unlock protocol.
///
/// This is safe because `&mut` guarantees that there exist no other
/// references to the data protected by the `RwLock`.
#[inline]
pub fn unlocked_fair<F, U>(s: &mut Self, f: F) -> U
where
F: FnOnce() -> U,
{
// Safety: An RwLockReadGuard always holds a shared lock.
unsafe {
s.rwlock.raw.unlock_shared_fair();
}
defer!(s.rwlock.raw.lock_shared());
f()
}

Fix: just remove the line about being fair from RwLockReadGuard::unlocked's documentation.

This mistake is not present on upgradable read guards, write guards, nor mutex guards.