Skip to content

Windows: Don't make RwLock writers race readers - #165337

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
R3nzTheCodeGOD:windows-rwlock-writers
Nov 25, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
R3nzTheCodeGOD:windows-rwlock-writers

Conversation

@R3nzTheCodeGOD

@R3nzTheCodeGOD R3nzTheCodeGOD commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #165314

The futex RwLock is meant to prefer writers. #95801 does that by waking a writer first and only waking readers if futex_wake says there was no writer to wake. WakeByAddressSingle doesn't report that, so on Windows futex_wake is always false and a writer and the readers are woken together to race, which is the thing #95801 set out to avoid. The writer usually loses.

Just returning true doesn't work: WRITERS_WAITING can be stale, and then nothing ever wakes the readers. So this counts the writers going to sleep on writer_notify. If the count isn't zero, a writer still has to retry the lock. It'll either take it or find it held, and either way whoever unlocks will see READERS_WAITING.

The compare-exchange before wake_writer must be Acquire. With Relaxed it can see the unlock of a writer that has already left and still read the old count, which leaves the readers asleep for good. Miri finds this if you force the interleaving, and a litmus test of just this ordering on an Apple M4 saw the old count 718829 times in 2.9 billion runs with Relaxed and never in 2.8 billion with Acquire. It can't happen on x86, where the generated code is the same.

Before and after on a Ryzen 5 5600 with Windows 10. Locks are held for 20 us unless noted.

writes/s Writer p99 reads/s
Program from the issue 1000 -> 6410 4.5 ms -> 36 us
1 writer every 1 ms, 6 readers x1.6 4 ms -> 47 us +10%
3 writers, 6 readers x2.4 2 ms -> 77 us -30%
4 writers, 12 readers, 5 us holds x31 24 ms -> 766 us -79%

The last row is the cost. Writers go first, as on Linux, so with that many of them the readers mostly wait, and total operations drop 73%. In a HashMap cache shared by 6-24 threads, with 0.1-10% writes, 0-20 us between operations and 0-20 us read holds, total throughput rose in 19 of these 54 configurations and fell in 3, by about a quarter in the two with 10% writes and nothing between operations. Reader p99 got more than 1.5x worse in 8, all but one with 10% writes. Typst doesn't contend on any RwLock while compiling, so it doesn't change.

RwLock<T> grows by 4 bytes unless T is aligned to 8. The uncontended paths are effectively unchanged. The queue-based RwLock is the obvious alternative and loses much less under heavy writes, but it makes uncontended reads 32% slower.

Tested with ./x test library/std and Miri on x86_64 only. FreeBSD, DragonFly and Fuchsia have the same futex_wake behavior and are left as they are. Adding them to the cfg_select! opts them in.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 2, 2026
@rustbot

rustbot commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @ChrisDenton (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

@joboet joboet left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll need to double-check this for correctness, but it makes sense to me...

View changes since this review

writer_notify: Futex,
// The number of writers going to sleep on `writer_notify`, for `wake_writer`.
#[cfg(target_os = "windows")]
blocked_writers: Futex,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be futex-sized, but it must be large enough to hold the number of possible threads in the system if every one of them tried to write-lock at once. I suggest using AtomicU32 explicitly, that's large enough on Windows (thread IDs are 32-bit, so there cannot be more than $2^{32}$ threads in the process).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@ChrisDenton ChrisDenton left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix in general looks good but I'm a little uncomfortable sprinkling #[cfg(windows)] about the place, especially as this affects other platforms. Maybe this can be done using a separate wrapper type (e.g. BlockedWriters) that's just a ZST on most platforms? Or some way to make it easy to opt platforms in/out of this specifically.

View changes since this review

if self.state.compare_exchange(state, READERS_WAITING, Relaxed, Relaxed).is_err() {
// Acquire, so that `wake_writer` can't see a `blocked_writers` count from
// before the unlock of a writer that has already left.
if self.state.compare_exchange(state, READERS_WAITING, Acquire, Relaxed).is_err() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't have thought so but does this change have any measurable effect on Linux?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. x86_64 generates the same code for all of std. On aarch64 it's just __aarch64_cas4_relax -> __aarch64_cas4_acq on the path where readers and writers are both waiting. I compared the asm but didn't run aarch64. Can make the Acquire conditional if you prefer.

@R3nzTheCodeGOD

Copy link
Copy Markdown
Contributor Author

Done. Windows only for now. FreeBSD, DragonFly and Fuchsia would just need adding to the cfg_select!, but I haven't tried them.

@ChrisDenton

Copy link
Copy Markdown
Member

Thanks, I'll hand this over to joboet to double check the correctness

r? joboet

@rustbot rustbot assigned joboet and unassigned ChrisDenton Nov 3, 2026
@rustbot

rustbot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

joboet is currently at their maximum review capacity.
They may take a while to respond.

Comment on lines +195 to +200

// Wake any readers it left waiting too, so that they don't have to wait
// for a whole queue of writers.
if BlockedWriters::ENABLED {
self.wake_writer_or_readers(READERS_WAITING);
}

@joboet joboet Nov 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to leave this out. The benefits here are far less clear cut, and this should probably apply to all (or no) target.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The numbers in the description are updated to match.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 13, 2026
@rustbot

rustbot commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

`WakeByAddressSingle` doesn't report whether it woke anything, so
`futex_wake` is always false on Windows and `wake_writer_or_readers`
wakes a writer and the readers together. The writer usually loses.

Count the writers going to sleep on `writer_notify` and treat a non-zero
count as a wakeup that reached a writer.
@R3nzTheCodeGOD

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 20, 2026
@joboet

joboet commented Nov 25, 2026

Copy link
Copy Markdown
Member

Alright, this looks fine. Thank you!

@bors r+

@rust-bors

rust-bors Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f0f1f89 has been approved by joboet

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 25, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 25, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #166487 (core: Use autotests to split up coretests)
 - #165145 (Various incr comp session refactors)
 - #165337 (Windows: Don't make RwLock writers race readers)
 - #165732 (feat: rename region obligations)
 - #166061 (Offload: generic kernels in a lib)
 - #166539 (Restore the section in `global-target-feature.rs`)
 - #165721 (avoid Terminator:None in coroutine drop shim building)
 - #166453 (perf: remove cache in `FoldEscapingRegions`)
 - #166547 (Pin i686 msvc)
@rust-bors
rust-bors Bot merged commit 2b69cc4 into rust-lang:main Nov 25, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 25, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 25, 2026
Rollup merge of #165337 - R3nzTheCodeGOD:windows-rwlock-writers, r=joboet

Windows: Don't make RwLock writers race readers

Fixes #165314

The futex `RwLock` is meant to prefer writers. #95801 does that by waking a writer first and only waking readers if `futex_wake` says there was no writer to wake. `WakeByAddressSingle` doesn't report that, so on Windows `futex_wake` is always false and a writer and the readers are woken together to race, which is the thing #95801 set out to avoid. The writer usually loses.

Just returning `true` doesn't work: `WRITERS_WAITING` can be stale, and then nothing ever wakes the readers. So this counts the writers going to sleep on `writer_notify`. If the count isn't zero, a writer still has to retry the lock. It'll either take it or find it held, and either way whoever unlocks will see `READERS_WAITING`.

The compare-exchange before `wake_writer` must be `Acquire`. With `Relaxed` it can see the unlock of a writer that has already left and still read the old count, which leaves the readers asleep for good. Miri finds this if you force the interleaving, and a litmus test of just this ordering on an Apple M4 saw the old count 718829 times in 2.9 billion runs with `Relaxed` and never in 2.8 billion with `Acquire`. It can't happen on x86, where the generated code is the same.

Before and after on a Ryzen 5 5600 with Windows 10. Locks are held for 20 us unless noted.

| | writes/s | Writer p99 | reads/s |
|---|---:|---:|---:|
| Program from the issue | 1000 -> 6410 | 4.5 ms -> 36 us | |
| 1 writer every 1 ms, 6 readers | x1.6 | 4 ms -> 47 us | +10% |
| 3 writers, 6 readers | x2.4 | 2 ms -> 77 us | -30% |
| 4 writers, 12 readers, 5 us holds | x31 | 24 ms -> 766 us | -79% |

The last row is the cost. Writers go first, as on Linux, so with that many of them the readers mostly wait, and total operations drop 73%. In a `HashMap` cache shared by 6-24 threads, with 0.1-10% writes, 0-20 us between operations and 0-20 us read holds, total throughput rose in 19 of these 54 configurations and fell in 3, by about a quarter in the two with 10% writes and nothing between operations. Reader p99 got more than 1.5x worse in 8, all but one with 10% writes. Typst doesn't contend on any `RwLock` while compiling, so it doesn't change.

`RwLock<T>` grows by 4 bytes unless `T` is aligned to 8. The uncontended paths are effectively unchanged. The queue-based `RwLock` is the obvious alternative and loses much less under heavy writes, but it makes uncontended reads 32% slower.

Tested with `./x test library/std` and Miri on x86_64 only. FreeBSD, DragonFly and Fuchsia have the same `futex_wake` behavior and are left as they are. Adding them to the `cfg_select!` opts them in.
mehmettoplu-rs pushed a commit to mehmettoplu-rs/miri that referenced this pull request Nov 26, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#166487 (core: Use autotests to split up coretests)
 - rust-lang/rust#165145 (Various incr comp session refactors)
 - rust-lang/rust#165337 (Windows: Don't make RwLock writers race readers)
 - rust-lang/rust#165732 (feat: rename region obligations)
 - rust-lang/rust#166061 (Offload: generic kernels in a lib)
 - rust-lang/rust#166539 (Restore the section in `global-target-feature.rs`)
 - rust-lang/rust#165721 (avoid Terminator:None in coroutine drop shim building)
 - rust-lang/rust#166453 (perf: remove cache in `FoldEscapingRegions`)
 - rust-lang/rust#166547 (Pin i686 msvc)
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this pull request Nov 27, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#166487 (core: Use autotests to split up coretests)
 - rust-lang/rust#165145 (Various incr comp session refactors)
 - rust-lang/rust#165337 (Windows: Don't make RwLock writers race readers)
 - rust-lang/rust#165732 (feat: rename region obligations)
 - rust-lang/rust#166061 (Offload: generic kernels in a lib)
 - rust-lang/rust#166539 (Restore the section in `global-target-feature.rs`)
 - rust-lang/rust#165721 (avoid Terminator:None in coroutine drop shim building)
 - rust-lang/rust#166453 (perf: remove cache in `FoldEscapingRegions`)
 - rust-lang/rust#166547 (Pin i686 msvc)
mehmettoplu-rs pushed a commit to mehmettoplu-rs/rustc_codegen_gcc that referenced this pull request Nov 27, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#166487 (core: Use autotests to split up coretests)
 - rust-lang/rust#165145 (Various incr comp session refactors)
 - rust-lang/rust#165337 (Windows: Don't make RwLock writers race readers)
 - rust-lang/rust#165732 (feat: rename region obligations)
 - rust-lang/rust#166061 (Offload: generic kernels in a lib)
 - rust-lang/rust#166539 (Restore the section in `global-target-feature.rs`)
 - rust-lang/rust#165721 (avoid Terminator:None in coroutine drop shim building)
 - rust-lang/rust#166453 (perf: remove cache in `FoldEscapingRegions`)
 - rust-lang/rust#166547 (Pin i686 msvc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RwLock writers get starved on Windows

4 participants