feat(sdk): Remove AtomicU64::load in ChunkIdentifierGenerator.

As suggested in https://github.com/matrix-org/matrix-rust-sdk/
pull/3251#discussion_r1532103818 by Poljar, it is possible that the
value of the atomic changes between the `fetch_add` and the `load` (if
and only if it is used in a concurrency model, which is not the case
right now, but anyway… better being correct now!). The idea is not
`load` but repeat the addition manually to compute the “current” value.
This commit is contained in:
Ivan Enderlin
2024-03-20 21:08:01 +01:00
parent c120da79d1
commit ab2b5bfa23

View File

@@ -593,15 +593,14 @@ impl ChunkIdentifierGenerator {
/// In this case, `Result::Err` contains the previous unique identifier.
pub fn generate_next(&self) -> Result<ChunkIdentifier, ChunkIdentifier> {
let previous = self.next.fetch_add(1, Ordering::Relaxed);
let current = self.next.load(Ordering::Relaxed);
// Check for overflows.
// unlikely — TODO: call `std::intrinsics::unlikely` once it's stable.
if current < previous {
if previous == u64::MAX {
return Err(ChunkIdentifier(previous));
}
Ok(ChunkIdentifier(current))
Ok(ChunkIdentifier(previous.saturating_add(1)))
}
}