Panic/return false on overflow in no_threads read/try_read impl#154526
Panic/return false on overflow in no_threads read/try_read impl#154526asder8215 wants to merge 2 commits intorust-lang:mainfrom
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
72d3ad4 to
f4404dc
Compare
This comment has been minimized.
This comment has been minimized.
f4404dc to
4b0caec
Compare
| assert!(m == isize::MAX, "too many active read locks on RwLock"); | ||
|
|
||
| if m >= 0 { | ||
| self.mode.set(m + 1); |
There was a problem hiding this comment.
Let's write this as m.checked_add(1).expect("rwlock overflowed read locks"), and same with below.
I would also move the subtraction in the unlock code to do the same. It's not as necessary, but I think it would be good to confirm we are in fact holding a read lock and should be cheap for this implementation.
There was a problem hiding this comment.
Done.
For RwLock::read_unlock, I used mem::replace, just like in RwLock::write_unlock,and checked if the old value is greater than 0 to see if it was read locked, asserting an error if it wasn't. I added an error msg for RwLock::write and RwLock::downgrade.
However, I do wonder if it's better if we use an assertion check to see if the lock is read/write locked, and then set the RwLock mode accordingly instead of mem::replace since we probably shouldn't mem::replace the mode value with 0 if in RwLock::read_unlock/RwLock::write_unlock if it was write locked/read locked.
|
Reminder, once the PR becomes ready for a review, use |
As per discussion with Mark in #153555, it's possible for
no_threadsimpl ofRwLockto trigger a silent overflow onRwLock::read/RwLock::try_readif we try to acquire more thanisize::MAXread locks. This PR adds an explicit panic/return false when our read lock counter is atisize::MAXforRwLock::read/RwLock::try_read; the message is similar to that of sys/sync/rwlock/futex.rs here.