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};
9 pub(crate) struct AtomicCounter {
10 // Usize needs to be at least 32 bits to avoid overflowing both low and high. If usize is 64
11 // bits we will never realistically count into high:
12 counter_low: AtomicUsize,
13 counter_high: AtomicUsize,
17 pub(crate) fn new() -> Self {
19 counter_low: AtomicUsize::new(0),
20 counter_high: AtomicUsize::new(0),
23 pub(crate) fn get_increment(&self) -> u64 {
24 let low = self.counter_low.fetch_add(1, Ordering::AcqRel) as u64;
25 let high = if low == 0 {
26 self.counter_high.fetch_add(1, Ordering::AcqRel) as u64
28 self.counter_high.load(Ordering::Acquire) as u64