Skip to content

Commit

Permalink
AtomicBitset::get method
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Aug 27, 2024
1 parent 64d9e16 commit 62bc153
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/sync/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ impl AtomicBitset {
self.words[index >> WORD_SIZE_SHIFT].fetch_or(mask, ordering);
}

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

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

#[inline]
pub fn drain(&self, ordering: Ordering) -> Drain {
let mut iter = self.words.iter();
Expand Down Expand Up @@ -81,7 +90,24 @@ mod tests {
use super::*;

#[test]
fn basic() {
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));

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

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

#[test]
fn drain() {
let bitset = AtomicBitset::with_len(8);

bitset.set(0, Ordering::Relaxed);
Expand Down

0 comments on commit 62bc153

Please sign in to comment.