Skip to content

Commit fb45532

Browse files
authored
Rollup merge of #55865 - RalfJung:unix-rwlock, r=alexcrichton
Unix RwLock: avoid racy access to write_locked We should only access `write_locked` if we really got the lock.
2 parents 3c7acc7 + db13390 commit fb45532

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/libstd/sys/unix/rwlock.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use sync::atomic::{AtomicUsize, Ordering};
1414

1515
pub struct RWLock {
1616
inner: UnsafeCell<libc::pthread_rwlock_t>,
17-
write_locked: UnsafeCell<bool>,
17+
write_locked: UnsafeCell<bool>, // guarded by the `inner` RwLock
1818
num_readers: AtomicUsize,
1919
}
2020

@@ -52,13 +52,13 @@ impl RWLock {
5252
// allow that because it could lead to aliasing issues.
5353
if r == libc::EAGAIN {
5454
panic!("rwlock maximum reader count exceeded");
55-
} else if r == libc::EDEADLK || *self.write_locked.get() {
55+
} else if r == libc::EDEADLK || (r == 0 && *self.write_locked.get()) {
5656
if r == 0 {
5757
self.raw_unlock();
5858
}
5959
panic!("rwlock read lock would result in deadlock");
6060
} else {
61-
debug_assert_eq!(r, 0);
61+
assert_eq!(r, 0);
6262
self.num_readers.fetch_add(1, Ordering::Relaxed);
6363
}
6464
}

0 commit comments

Comments
 (0)