x11rb/xcb_ffi/
atomic_u64.rs

1//! Either `AtomicU64` or emulating `AtomicU64` through a `Mutex`.
2
3// Use the `AtomicU64` from the standard library if we're on a platform that supports atomic
4// 64-bit operations.
5#[cfg(target_has_atomic = "64")]
6pub(crate) use std::sync::atomic::AtomicU64;
7
8#[cfg(not(target_has_atomic = "64"))]
9mod impl_ {
10    use std::sync::atomic::Ordering;
11    use std::sync::Mutex;
12
13    #[derive(Debug)]
14    pub(crate) struct AtomicU64(Mutex<u64>);
15
16    impl AtomicU64 {
17        pub(crate) fn new(val: u64) -> Self {
18            Self(Mutex::new(val))
19        }
20
21        pub(crate) fn load(&self, _: Ordering) -> u64 {
22            *self.0.lock().unwrap()
23        }
24
25        pub(crate) fn fetch_max(&self, val: u64, _: Ordering) -> u64 {
26            let mut lock = self.0.lock().unwrap();
27            let old = *lock;
28            *lock = old.max(val);
29            old
30        }
31    }
32}
33
34#[cfg(not(target_has_atomic = "64"))]
35pub(crate) use self::impl_::AtomicU64;