exponential_histogram/
shared.rs

1use std::sync::{Arc, Mutex};
2
3use crate::ExponentialHistogram;
4
5/// An ExponentialHistogram with interior mutability
6#[derive(Debug, Clone, Default)]
7pub struct SharedExponentialHistogram {
8    inner: Arc<Mutex<ExponentialHistogram>>,
9}
10
11impl SharedExponentialHistogram {
12    /// Observe a value, increasing its bucket's count by 1
13    pub fn accumulate(&self, value: f64) {
14        self.inner
15            .lock()
16            .expect("local mutex works")
17            .accumulate(value)
18    }
19
20    /// Get the current snapshot of the histogram. This gives you an owned clone of the backing histogram
21    /// at a point in time, so you can work with it without holding a lock.
22    pub fn snapshot(&self) -> ExponentialHistogram {
23        self.inner.lock().expect("local mutex works").clone()
24    }
25
26    /// Get the current snapshot of the histogram. This gives you an owned clone of the backing histogram
27    /// at a point in time, so you can work with it without holding a lock.
28    pub fn snapshot_and_reset(&self) -> ExponentialHistogram {
29        let mut histogram = self.inner.lock().expect("local mutex works");
30        let snapshot = histogram.clone();
31        histogram.reset();
32        snapshot
33    }
34}