tokio/loom/std/
atomic_u16.rs

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