Skip to content

Commit

Permalink
fix: mutex across .await (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione authored Apr 18, 2023
1 parent 3722122 commit 0324d16
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ pub(crate) use sync::LFUPolicy;

#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
mod axync;
mod r#async;
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub(crate) use axync::AsyncLFUPolicy;
pub(crate) use r#async::AsyncLFUPolicy;

pub(crate) struct PolicyInner<S = RandomState> {
admit: TinyLFU,
Expand Down
File renamed without changes.
21 changes: 12 additions & 9 deletions src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ where
}

#[cfg(feature = "async")]

pub struct AsyncRingStripe<S> {
cons: Arc<AsyncLFUPolicy<S>>,
data: Mutex<Vec<u64>>,
capa: usize,
}

#[cfg(feature = "async")]

impl<S> AsyncRingStripe<S>
where
S: BuildHasher + Clone + 'static + Send,
Expand All @@ -59,13 +57,18 @@ where
}

pub async fn push(&self, item: u64) {
let mut data = self.data.lock();
data.push(item);
if data.len() >= self.capa {
match self.cons.push(data.clone()).await {
Ok(true) => *data = Vec::with_capacity(self.capa),
_ => data.clear(),
let data = {
let mut data = self.data.lock();
data.push(item);
if data.len() >= self.capa {
let ret = data.clone();
data.clear();
ret
} else {
return;
}
}
};

_ = self.cons.push(data).await;
}
}

0 comments on commit 0324d16

Please sign in to comment.