#[cfg(not(feature = "ledger"))]
pub(crate) use off::*;
#[cfg(feature = "ledger")]
pub(crate) use on::*;
#[cfg(not(feature = "ledger"))]
mod off {
pub(crate) struct Track;
pub(crate) struct Guard;
impl Track {
pub(crate) const NONE: Track = Track;
#[inline(always)]
pub(crate) fn root() -> Self {
Track
}
#[inline(always)]
pub(crate) fn child(&self) -> Self {
Track
}
#[inline(always)]
pub(crate) fn alias(&self) -> Self {
Track
}
#[inline(always)]
pub(crate) fn task(&self) -> Guard {
Guard
}
#[inline(always)]
pub(crate) fn release(&self) -> Guard {
Guard
}
#[inline(always)]
#[allow(dead_code)] pub(crate) fn note_install(&self) {}
}
}
#[cfg(feature = "ledger")]
mod on {
use crate::ledger::{register, Counts};
use std::sync::atomic::Ordering;
use std::sync::Arc;
pub(crate) struct Track {
counts: Option<Arc<Counts>>,
counted: bool,
}
pub(crate) struct Guard<'a> {
counts: Option<&'a Counts>,
end_is_release: bool,
}
impl Track {
pub(crate) const NONE: Track = Track {
counts: None,
counted: false,
};
pub(crate) fn root() -> Self {
Track {
counts: Some(register()),
counted: false,
}
}
pub(crate) fn child(&self) -> Self {
Track {
counts: self.counts.clone(),
counted: true,
}
}
pub(crate) fn alias(&self) -> Self {
Track {
counts: self.counts.clone(),
counted: self.counted,
}
}
pub(crate) fn root_id(&self) -> Option<u64> {
self.counts.as_ref().map(|c| c.id)
}
pub(crate) fn task(&self) -> Guard<'_> {
if let Some(c) = &self.counts {
c.enter();
}
Guard {
counts: self.counts.as_deref(),
end_is_release: false,
}
}
pub(crate) fn release(&self) -> Guard<'_> {
let counts = if self.counted {
self.counts.as_deref()
} else {
None
};
if let Some(c) = counts {
c.live.fetch_sub(1, Ordering::SeqCst);
}
Guard {
counts,
end_is_release: true,
}
}
#[allow(dead_code)] pub(crate) fn note_install(&self) {
if let Some(c) = &self.counts {
c.installs.fetch_add(1, Ordering::SeqCst);
}
}
}
impl Drop for Guard<'_> {
fn drop(&mut self) {
if let Some(c) = self.counts {
if self.end_is_release {
c.enter();
} else {
c.live.fetch_sub(1, Ordering::SeqCst);
}
}
}
}
}