From b25b1e18638adecc8116825f88acc8f93073df5d Mon Sep 17 00:00:00 2001 From: Calascibetta Romain Date: Mon, 25 Mar 2024 11:08:52 +0100 Subject: [PATCH] Use backoff to be sure to be in sync when we update sources (@reynir) --- rng/entropy.ml | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/rng/entropy.ml b/rng/entropy.ml index 008bdb80..46bf492f 100644 --- a/rng/entropy.ml +++ b/rng/entropy.ml @@ -55,10 +55,15 @@ let _sources = Atomic.make S.empty type source = Rng.source let register_source name = - let n = S.cardinal (Atomic.get _sources) in - let source = (n, name) in - Atomic.set _sources (S.add source (Atomic.get _sources)); - source + let rec go backoff = + let seen = Atomic.get _sources in + let n = S.cardinal seen in + let source = (n, name) in + let after = S.add source (Atomic.get _sources) in + if Atomic.compare_and_set _sources seen after = false + then go (Backoff.once backoff) + else source in + go Backoff.default let id (idx, _) = idx