use super::*;
#[derive(Deref, Debug)]
pub(super) struct TotalQtyNotifier {
#[deref]
total_used: TotalQty,
reclamation_task_wakeup: mpsc::Sender<()>,
}
impl TotalQtyNotifier {
pub(super) fn new_zero(reclamation_task_wakeup: mpsc::Sender<()>) -> Self {
TotalQtyNotifier {
total_used: TotalQty::ZERO,
reclamation_task_wakeup,
}
}
pub(super) fn claim(
&mut self,
precord: &mut PRecord,
want: Qty,
config: &ConfigInner,
) -> crate::Result<ClaimedQty> {
let got = self
.total_used
.claim(&mut precord.used, want)
.ok_or_else(|| internal!("integer overflow attempting to add claim {}", want))?;
self.maybe_wakeup(config);
Ok(got)
}
pub(super) fn maybe_wakeup(&mut self, config: &ConfigInner) {
if self.total_used > config.max {
match self.reclamation_task_wakeup.try_send(()) {
Ok(()) => {}
Err(e) if e.is_full() => {}
Err(e) => debug!("could not notify reclamation task: {e}"),
};
}
}
pub(super) fn set_poisoned(&mut self) {
self.total_used.set_poisoned();
}
pub(super) fn release(&mut self, precord: &mut PRecord, have: ClaimedQty) {
self.total_used.release(&mut precord.used, have);
}
}