ITADN

Fix spinlock race condition (rwlock)

#560Pull Requesttrim-the-main 创建于 2026-02-02
T
trim-the-maincommented
Readers try to acquire the lock using `fetch_add(2)` (least significant bit is reserved for the writers) and they check if a writer exists after they perform the addition. If there is a writer they undo their change using `fetch_sub(2)` . However if the writer unlocks before the `fetch_sub(2)` of the reader, setting the state to `UNLOCKED = 0`, then the subsequent `fetch_sub` of the reader ends up corrupting the state, overflowing and wrapping around. With this change the `unlock_exclusive` just clears the writer bit from the state, leaving the reader counts unchanged. The caller must hold the lock so the state must have the writer bit set prior. ``` $ RUSTFLAGS="--cfg loom" cargo test blocking::rwlock running 2 tests test blocking::rwlock::tests::read_write ... ok test blocking::rwlock::tests::write ... ok test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 43 filtered out; finished in 0.00s```
合并状态:未合并 1 条评论