1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::AtomicU128;

/// A write-mostly, read-rarely counter
#[derive(Default, Debug)]
pub struct FastCounter {
    counter: AtomicU128,
}

impl FastCounter {
    /// Increment the counter, and returns current value
    #[inline]
    pub fn incr(&self) -> u128 {
        let prev = self.count();

        if prev == u128::MAX {
            log::error!("fast counter overflow! try incrementing the max value of unsigned 128-bit integer");
            return prev;
        }

        match self.counter.compare_exchange(prev, prev+1) {
            Ok(v) => v,
            Err(v) => {
                log::warn!("fast counter: incr(): compare exchange failed");
                v
            }
        }
    }

    /// Decrement the counter, and returns current value
    #[inline]
    pub fn decr(&self) -> u128 {
        let prev = self.count();

        if prev == 0 {
            log::error!("fast counter overflow! try decrementing to negative number!");
            return prev;
        }

        match self.counter.compare_exchange(prev, prev-1) {
            Ok(v) => v,
            Err(v) => {
                log::warn!("fast counter: decr(): compare exchange failed");
                v
            }
        }
    }

    /// Get the total count
    #[inline]
    pub fn count(&self) -> u128 {
        self.counter.load()
    }
}