use std::cell::RefCell;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
thread_local! { static BLOB_READ_COUNTER: RefCell<Option<Arc<AtomicUsize>>> = const { RefCell::new(None) };
}
pub struct BlobReadCounterScope {
previous: Option<Arc<AtomicUsize>>,
}
impl Drop for BlobReadCounterScope {
fn drop(&mut self) {
BLOB_READ_COUNTER.with(|slot| {
*slot.borrow_mut() = self.previous.take();
});
}
}
pub fn track_blob_reads(counter: Arc<AtomicUsize>) -> BlobReadCounterScope {
let previous = BLOB_READ_COUNTER.with(|slot| slot.borrow_mut().replace(counter));
BlobReadCounterScope { previous }
}
pub(crate) fn note_blob_read() {
BLOB_READ_COUNTER.with(|slot| {
if let Some(counter) = slot.borrow().as_ref() {
counter.fetch_add(1, Ordering::Relaxed);
}
});
}