Skip to content

Commit

Permalink
fix cached_avail bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Skilvingr committed Jun 22, 2024
1 parent 8ad51d4 commit d84e96f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/iterators/iterator_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub(crate) mod iter_macros {
if self.index >= self.buf_len.get() {
self.index -= self.buf_len.get();
}

self.cached_avail = self.cached_avail.saturating_sub(count);

self.set_atomic_index(self.index);
}
Expand All @@ -123,12 +125,7 @@ pub(crate) mod iter_macros {
macro_rules! private_impl { () => (
#[inline(always)]
fn check(&mut self, count: usize) -> bool {
if self.cached_avail >= count || self.available() >= count {
self.cached_avail -= count;
true
} else {
false
}
self.cached_avail >= count || self.available() >= count
}

#[inline]
Expand Down
26 changes: 26 additions & 0 deletions src/iterators/sync_iterators/cons_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,29 @@ impl<B: MutRB<Item = T>, T, const W: bool> ConsIter<B, W> {
} else { None }
}
}

mod test {
#[test]
fn cached_avail() {
use crate::ConcurrentHeapRB;
use super::*;

const BUFFER_SIZE: usize = 100;

let (mut prod, mut cons) = ConcurrentHeapRB::<u32>::default(BUFFER_SIZE + 1).split();

assert_eq!(cons.cached_avail, 0);

unsafe { prod.advance(10); }

assert_eq!(cons.cached_avail, 0);

cons.check(1);

assert_eq!(cons.cached_avail, 10);

unsafe { cons.advance(9); }

assert_eq!(cons.cached_avail, 1);
}
}
30 changes: 30 additions & 0 deletions src/iterators/sync_iterators/prod_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,34 @@ impl<B: MutRB<Item = T>, T> ProdIter<B> {
pub unsafe fn get_next_slices_mut<'a>(&mut self, count: usize) -> Option<(&'a mut [T], &'a mut [T])> {
self.next_chunk_mut(count)
}
}

pub mod test {
#[test]
fn cached_avail() {
use crate::ConcurrentHeapRB;
use super::*;

const BUFFER_SIZE: usize = 10;

let (mut prod, mut cons) = ConcurrentHeapRB::<u32>::default(BUFFER_SIZE + 1).split();

assert_eq!(prod.cached_avail, 0);

prod.check(1);

assert_eq!(prod.cached_avail, BUFFER_SIZE);

unsafe { prod.advance(2); }

assert_eq!(prod.cached_avail, 8);

unsafe { cons.advance(1); }

assert_eq!(prod.cached_avail, 8);

prod.check(10);

assert_eq!(prod.cached_avail, 9);
}
}

0 comments on commit d84e96f

Please sign in to comment.