Unix RwLock: avoid racy access to write_locked

This commit is contained in:
Ralf Jung 2018-11-11 10:06:41 +01:00
parent 9b8f902976
commit 0c6a093afa

View file

@ -14,7 +14,7 @@ use sync::atomic::{AtomicUsize, Ordering};
pub struct RWLock {
inner: UnsafeCell<libc::pthread_rwlock_t>,
write_locked: UnsafeCell<bool>,
write_locked: UnsafeCell<bool>, // guarded by the `inner` RwLock
num_readers: AtomicUsize,
}
@ -52,7 +52,7 @@ impl RWLock {
// allow that because it could lead to aliasing issues.
if r == libc::EAGAIN {
panic!("rwlock maximum reader count exceeded");
} else if r == libc::EDEADLK || *self.write_locked.get() {
} else if r == libc::EDEADLK || (r == 0 && *self.write_locked.get()) {
if r == 0 {
self.raw_unlock();
}