Skip to content

Commit

Permalink
AtomicBitset::reset method to set a particular bit to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcoil committed Aug 30, 2024
1 parent c57d777 commit 67a5a6e
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/sync/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ impl AtomicBitset {
self.words[index >> WORD_SIZE_SHIFT].fetch_or(mask, ordering);
}

#[inline]
pub fn reset(&self, index: usize, ordering: Ordering) {
assert!(index < self.len);

let mask = 1 << (index & WORD_SIZE_MASK);
self.words[index >> WORD_SIZE_SHIFT].fetch_and(!mask, ordering);
}

#[inline]
pub fn get(&self, index: usize, ordering: Ordering) -> bool {
assert!(index < self.len);
Expand Down Expand Up @@ -93,17 +101,15 @@ mod tests {
fn set_get() {
let bitset = AtomicBitset::with_len(8);

assert!(!bitset.get(0, Ordering::Relaxed));
bitset.set(0, Ordering::Relaxed);
assert!(bitset.get(0, Ordering::Relaxed));
for index in 0..8 {
assert!(!bitset.get(index, Ordering::Relaxed));

assert!(!bitset.get(3, Ordering::Relaxed));
bitset.set(3, Ordering::Relaxed);
assert!(bitset.get(3, Ordering::Relaxed));
bitset.set(index, Ordering::Relaxed);
assert!(bitset.get(index, Ordering::Relaxed));

assert!(!bitset.get(7, Ordering::Relaxed));
bitset.set(7, Ordering::Relaxed);
assert!(bitset.get(7, Ordering::Relaxed));
bitset.reset(index, Ordering::Relaxed);
assert!(!bitset.get(index, Ordering::Relaxed));
}
}

#[test]
Expand Down

0 comments on commit 67a5a6e

Please sign in to comment.