Skip to content

Commit

Permalink
dsf: use atomic fetch update
Browse files Browse the repository at this point in the history
  • Loading branch information
aiju committed Oct 29, 2024
1 parent e66f420 commit 4df22e7
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions dsf/src/tracked_union_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,14 @@ impl<T: Id + NoUninit> IdAlloc<T> {
pub fn alloc_block(&self, n: usize) -> T {
use atomic::Ordering::Relaxed;
debug_assert!(n > 0);
let mut current_id = self.counter.load(Relaxed);
loop {
let new_id = current_id
.id_index()
.checked_add(n)
.and_then(T::try_from_id_index)
.expect("not enough IDs remaining");
match self
.counter
.compare_exchange_weak(current_id, new_id, Relaxed, Relaxed)
{
Ok(_) => return current_id,
Err(id) => current_id = id,
}
}
self.counter
.fetch_update(Relaxed, Relaxed, |current_id| {
current_id
.id_index()
.checked_add(n)
.and_then(T::try_from_id_index)
})
.expect("not enough IDs remaining")
}
pub fn alloc(&self) -> T {
self.alloc_block(1)
Expand Down

0 comments on commit 4df22e7

Please sign in to comment.