Create a simple `FairRwLock` to avoid readers starving writers
authorMatt Corallo <git@bluematt.me>
Wed, 6 Oct 2021 16:58:56 +0000 (16:58 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 10 May 2022 23:40:20 +0000 (23:40 +0000)
commitae4ceb71a584f0aa9e0c1a14a6219d87f1668eba
treec63ca8abad44b57653b948c870b32557fada210d
parentf90983180eac9950256d47cb5eb77353f19fec6b
Create a simple `FairRwLock` to avoid readers starving writers

Because we handle messages (which can take some time, persisting
things to disk or validating cryptographic signatures) with the
top-level read lock, but require the top-level write lock to
connect new peers or handle disconnection, we are particularly
sensitive to writer starvation issues.

Rust's libstd RwLock does not provide any fairness guarantees,
using whatever the OS provides as-is. On Linux, pthreads defaults
to starving writers, which Rust's RwLock exposes to us (without
any configurability).

Here we work around that issue by blocking readers if there are
pending writers, optimizing for readable code over
perfectly-optimized blocking.
lightning/src/debug_sync.rs
lightning/src/lib.rs
lightning/src/ln/peer_handler.rs
lightning/src/sync.rs
lightning/src/util/fairrwlock.rs [new file with mode: 0644]
lightning/src/util/mod.rs