#[cfg(feature = "work-count")]
thread_local! {
static WORK: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
#[cfg(feature = "work-count")]
#[inline]
pub fn tick(n: u64) {
WORK.with(|w| w.set(w.get().wrapping_add(n)));
}
#[cfg(not(feature = "work-count"))]
#[inline(always)]
pub fn tick(_n: u64) {}
#[cfg(feature = "work-count")]
pub fn reset() {
WORK.with(|w| w.set(0));
}
#[cfg(not(feature = "work-count"))]
#[inline(always)]
pub fn reset() {}
#[cfg(feature = "work-count")]
pub fn get() -> u64 {
WORK.with(std::cell::Cell::get)
}
#[cfg(not(feature = "work-count"))]
#[inline(always)]
pub fn get() -> u64 {
0
}
pub fn count<R>(f: impl FnOnce() -> R) -> (R, u64) {
reset();
let r = f();
(r, get())
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "work-count")]
#[test]
fn counts_per_thread_and_resets() {
let ((), w) = count(|| {
tick(3);
tick(4);
});
assert_eq!(w, 7);
std::thread::spawn(|| assert_eq!(get(), 0)).join().unwrap();
reset();
assert_eq!(get(), 0);
}
#[cfg(not(feature = "work-count"))]
#[test]
fn counts_nothing_without_the_feature() {
let ((), w) = count(|| tick(3));
assert_eq!(w, 0);
}
}