about summary refs log tree commit diff
path: root/library/std/src/sys/unix/stack_overflow.rs
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-04-11 20:00:41 +0200
committerGitHub <noreply@github.com>2022-04-11 20:00:41 +0200
commita15ac301627b33f87ca6f166b7cdbcc453543ee3 (patch)
tree4cdc27dae2502c9a72a77f7d5213b0769dad5f78 /library/std/src/sys/unix/stack_overflow.rs
parent2ad701e45036fb2ccab8d4b4e23f9a3325e12817 (diff)
parent83393817419e197d696fb724560bc55b18346a01 (diff)
downloadrust-a15ac301627b33f87ca6f166b7cdbcc453543ee3.tar.gz
rust-a15ac301627b33f87ca6f166b7cdbcc453543ee3.zip
Rollup merge of #95801 - m-ou-se:futex-rwlock, r=Amanieu
Replace RwLock by a futex based one on Linux

This replaces the pthread-based RwLock on Linux by a futex based one.

This implementation is similar to [the algorithm](https://gist.github.com/kprotty/3042436aa55620d8ebcddf2bf25668bc) suggested by `@kprotty,` but modified to prefer writers and spin before sleeping. It uses two futexes: One for the readers to wait on, and one for the writers to wait on. The readers futex contains the state of the RwLock: The number of readers, a bit indicating whether writers are waiting, and a bit indicating whether readers are waiting. The writers futex is used as a simple condition variable and its contents are meaningless; it just needs to be changed on every notification.

Using two futexes rather than one has the obvious advantage of allowing a separate queue for readers and writers, but it also means we avoid the problem a single-futex RwLock would have of making it hard for a writer to go to sleep while the number of readers is rapidly changing up and down, as the writers futex is only changed when we actually want to wake up a writer.

It always prefers writers, as we decided [here](https://github.com/rust-lang/rust/issues/93740#issuecomment-1070696128).

To be able to prefer writers, it relies on futex_wake to return the number of awoken threads to be able to handle write-unlocking while both the readers-waiting and writers-waiting bits are set. Instead of waking both and letting them race, it first wakes writers and only continues to wake the readers too if futex_wake reported there were no writers to wake up.

r? `@Amanieu`
Diffstat (limited to 'library/std/src/sys/unix/stack_overflow.rs')
0 files changed, 0 insertions, 0 deletions