solana_atomic_u64/
lib.rs

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
pub use implementation::AtomicU64;

#[cfg(target_pointer_width = "64")]
mod implementation {
    use std::sync::atomic;

    pub struct AtomicU64(atomic::AtomicU64);

    impl AtomicU64 {
        pub const fn new(initial: u64) -> Self {
            Self(atomic::AtomicU64::new(initial))
        }

        pub fn fetch_add(&self, v: u64) -> u64 {
            self.0.fetch_add(v, atomic::Ordering::Relaxed)
        }
    }
}

#[cfg(not(target_pointer_width = "64"))]
mod implementation {
    use parking_lot::{const_mutex, Mutex};

    pub struct AtomicU64(Mutex<u64>);

    impl AtomicU64 {
        pub const fn new(initial: u64) -> Self {
            Self(const_mutex(initial))
        }

        pub fn fetch_add(&self, v: u64) -> u64 {
            let mut lock = self.0.lock();
            let i = *lock;
            *lock = i + v;
            i
        }
    }
}