diff --git a/src/sync/bitset.rs b/src/sync/bitset.rs index 0ce2e06..4d9f932 100644 --- a/src/sync/bitset.rs +++ b/src/sync/bitset.rs @@ -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(); @@ -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);