1 //! A simple atomic counter that uses AtomicUsize to give a u64 counter.
3 #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
4 compile_error!("We need at least 32-bit pointers for atomic counter (and to have enough memory to run LDK)");
6 use core::sync::atomic::{AtomicUsize, Ordering};
8 pub(crate) struct AtomicCounter {
9 // Usize needs to be at least 32 bits to avoid overflowing both low and high. If usize is 64
10 // bits we will never realistically count into high:
11 counter_low: AtomicUsize,
12 counter_high: AtomicUsize,
16 pub(crate) fn new() -> Self {
18 counter_low: AtomicUsize::new(0),
19 counter_high: AtomicUsize::new(0),
22 pub(crate) fn get_increment(&self) -> u64 {
23 let low = self.counter_low.fetch_add(1, Ordering::AcqRel) as u64;
24 let high = if low == 0 {
25 self.counter_high.fetch_add(1, Ordering::AcqRel) as u64
27 self.counter_high.load(Ordering::Acquire) as u64