Skip to content

Commit

Permalink
issue/148 Wrapping MsQueue to drop all of its concent on Drop
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed May 14, 2017
1 parent f192228 commit 15fc049
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/core/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,39 @@ pub struct GenerationItem<T> {
item: T,
}


// See https://github.com/crossbeam-rs/crossbeam/issues/91
struct NonLeakingMsQueue<T> {
underlying_queue: MsQueue<T>
}

impl<T> Default for NonLeakingMsQueue<T> {
fn default() -> NonLeakingMsQueue<T> {
NonLeakingMsQueue {
underlying_queue: MsQueue::new(),
}
}
}

impl<T> NonLeakingMsQueue<T> {

fn pop(&self,) -> T {
self.underlying_queue.pop()
}

fn push(&self, el: T) {
self.underlying_queue.push(el);
}
}

impl<T> Drop for NonLeakingMsQueue<T> {
fn drop(&mut self) {
while let Some(_popped_item_to_be_dropped) = self.underlying_queue.try_pop() {}
}
}

pub struct Pool<T> {
queue: Arc<MsQueue<GenerationItem<T>>>,
queue: Arc<NonLeakingMsQueue<GenerationItem<T>>>,
freshest_generation: AtomicUsize,
next_generation: AtomicUsize,
}
Expand All @@ -20,7 +51,7 @@ impl<T> Pool<T> {

pub fn new() -> Pool<T> {
Pool {
queue: Arc::new(MsQueue::new()),
queue: Arc::default(),
freshest_generation: AtomicUsize::default(),
next_generation: AtomicUsize::default(),
}
Expand Down Expand Up @@ -57,7 +88,7 @@ impl<T> Pool<T> {
self.freshest_generation.load(Ordering::Acquire)
}

pub fn acquire(&self,) -> LeasedItem<T> {
pub fn acquire(&self) -> LeasedItem<T> {
let generation = self.generation();
loop {
let gen_item = self.queue.pop();
Expand All @@ -80,7 +111,7 @@ impl<T> Pool<T> {

pub struct LeasedItem<T> {
gen_item: Option<GenerationItem<T>>,
recycle_queue: Arc<MsQueue<GenerationItem<T>>>,
recycle_queue: Arc<NonLeakingMsQueue<GenerationItem<T>>>,
}

impl<T> Deref for LeasedItem<T> {
Expand Down

0 comments on commit 15fc049

Please sign in to comment.