mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-18 13:40:55 -04:00
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:
@@ -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)))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user