Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions library/std/src/sys/sync/rwlock/no_threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl RwLock {
pub fn read(&self) {
let m = self.mode.get();
if m >= 0 {
self.mode.set(m + 1);
self.mode.set(m.checked_add(1).expect("rwlock overflowed read locks"));
} else {
rtabort!("rwlock locked for writing");
}
Expand All @@ -28,6 +28,9 @@ impl RwLock {
pub fn try_read(&self) -> bool {
let m = self.mode.get();
if m >= 0 {
if m == isize::MAX {
return false;
}
self.mode.set(m + 1);
true
} else {
Expand Down Expand Up @@ -56,16 +59,19 @@ impl RwLock {

#[inline]
pub unsafe fn read_unlock(&self) {
self.mode.set(self.mode.get() - 1);
assert!(
self.mode.replace(self.mode.get() - 1) > 0,
"rwlock has not been locked for reading"
);
}

#[inline]
pub unsafe fn write_unlock(&self) {
assert_eq!(self.mode.replace(0), -1);
assert_eq!(self.mode.replace(0), -1, "rwlock has not been locked for writing");
}

#[inline]
pub unsafe fn downgrade(&self) {
assert_eq!(self.mode.replace(1), -1);
assert_eq!(self.mode.replace(1), -1, "rwlock has not been locked for writing");
}
}
Loading