]> git.bitcoin.ninja Git - rust-lightning/commit
Simplify and fix `AtomicCounter`
authorMatt Corallo <git@bluematt.me>
Sun, 8 Sep 2024 19:05:28 +0000 (19:05 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 12 Sep 2024 14:32:43 +0000 (14:32 +0000)
commit2ab133d432289ca00bfd0d56f650e2e45f515a70
treefbffd22126a7fa808ce0884aac0bae1b2de9e65e
parent6e340c43de50cdba88286d3fce1f65f57b1024b2
Simplify and fix `AtomicCounter`

`AtomicCounter` was slightly race-y on 32-bit platforms because it
increments the high `AtomicUsize` independently from the low
`AtomicUsize`, leading to a potential race where another thread
could observe the low increment but not the high increment and see
a value of 0 twice.

This isn't a big deal because (a) most platforms are 64-bit these
days, (b) 32-bit platforms aren't super likely to have their
counter overflow 32 bits anyway, and (c) the two writes are
back-to-back so having another thread read during that window is
very unlikely.

However, we can also optimize the counter somewhat by using the
`target_has_atomic = "64"` cfg flag, which we do here, allowing us
to use `AtomicU64` even on 32-bit platforms where 64-bit atomics
are available.

This changes some test behavior slightly, which requires
adaptation.

Fixes #3000
lightning/src/ln/functional_tests.rs
lightning/src/ln/monitor_tests.rs
lightning/src/sign/mod.rs
lightning/src/util/atomic_counter.rs