tokio/loom/std/
atomic_u32.rs

1use std::cell::UnsafeCell;
2use std::fmt;
3use std::ops::Deref;
4use std::panic;
5
6/// `AtomicU32` providing an additional `unsync_load` function.
7pub(crate) struct AtomicU32 {
8    inner: UnsafeCell<std::sync::atomic::AtomicU32>,
9}
10
11unsafe impl Send for AtomicU32 {}
12unsafe impl Sync for AtomicU32 {}
13impl panic::RefUnwindSafe for AtomicU32 {}
14impl panic::UnwindSafe for AtomicU32 {}
15
16impl AtomicU32 {
17    pub(crate) const fn new(val: u32) -> AtomicU32 {
18        let inner = UnsafeCell::new(std::sync::atomic::AtomicU32::new(val));
19        AtomicU32 { inner }
20    }
21
22    /// Performs an unsynchronized load.
23    ///
24    /// # Safety
25    ///
26    /// All mutations must have happened before the unsynchronized load.
27    /// Additionally, there must be no concurrent mutations.
28    pub(crate) unsafe fn unsync_load(&self) -> u32 {
29        // See <https://github.com/tokio-rs/tokio/issues/6155>
30        self.load(std::sync::atomic::Ordering::Relaxed)
31    }
32}
33
34impl Deref for AtomicU32 {
35    type Target = std::sync::atomic::AtomicU32;
36
37    fn deref(&self) -> &Self::Target {
38        // safety: it is always safe to access `&self` fns on the inner value as
39        // we never perform unsafe mutations.
40        unsafe { &*self.inner.get() }
41    }
42}
43
44impl fmt::Debug for AtomicU32 {
45    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
46        self.deref().fmt(fmt)
47    }
48}