From 7bb3deb9f0006c61e4a9a799f06dbfffb26b83ea Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Thu, 30 Nov 2023 14:04:08 +0100 Subject: [PATCH 1/2] chore: use relaxed load for `unsync_load` --- tokio/src/loom/std/atomic_u16.rs | 4 +++- tokio/src/loom/std/atomic_u32.rs | 4 +++- tokio/src/loom/std/atomic_usize.rs | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tokio/src/loom/std/atomic_u16.rs b/tokio/src/loom/std/atomic_u16.rs index c9e105c1934..86064fd16fe 100644 --- a/tokio/src/loom/std/atomic_u16.rs +++ b/tokio/src/loom/std/atomic_u16.rs @@ -23,7 +23,9 @@ impl AtomicU16 { /// All mutations must have happened before the unsynchronized load. /// Additionally, there must be no concurrent mutations. pub(crate) unsafe fn unsync_load(&self) -> u16 { - core::ptr::read(self.inner.get() as *const u16) + self.load(std::sync::atomic::Ordering::Relaxed) + // See + // core::ptr::read(self.inner.get() as *const u16) } } diff --git a/tokio/src/loom/std/atomic_u32.rs b/tokio/src/loom/std/atomic_u32.rs index ee0d2d38050..c0716eda2f3 100644 --- a/tokio/src/loom/std/atomic_u32.rs +++ b/tokio/src/loom/std/atomic_u32.rs @@ -23,7 +23,9 @@ impl AtomicU32 { /// All mutations must have happened before the unsynchronized load. /// Additionally, there must be no concurrent mutations. pub(crate) unsafe fn unsync_load(&self) -> u32 { - core::ptr::read(self.inner.get() as *const u32) + self.load(std::sync::atomic::Ordering::Relaxed) + // See + // core::ptr::read(self.inner.get() as *const u32) } } diff --git a/tokio/src/loom/std/atomic_usize.rs b/tokio/src/loom/std/atomic_usize.rs index c5503a2c122..d3e9b703503 100644 --- a/tokio/src/loom/std/atomic_usize.rs +++ b/tokio/src/loom/std/atomic_usize.rs @@ -23,7 +23,9 @@ impl AtomicUsize { /// All mutations must have happened before the unsynchronized load. /// Additionally, there must be no concurrent mutations. pub(crate) unsafe fn unsync_load(&self) -> usize { - core::ptr::read(self.inner.get() as *const usize) + self.load(std::sync::atomic::Ordering::Relaxed) + // See + // core::ptr::read(self.inner.get() as *const usize) } pub(crate) fn with_mut(&mut self, f: impl FnOnce(&mut usize) -> R) -> R { From e75181ff7ac42659bd59a85c903b151c3b1743b5 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Thu, 7 Dec 2023 09:24:23 +0100 Subject: [PATCH 2/2] cfg(miri) --- tokio/src/loom/std/atomic_u16.rs | 6 ++++-- tokio/src/loom/std/atomic_u32.rs | 6 ++++-- tokio/src/loom/std/atomic_usize.rs | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tokio/src/loom/std/atomic_u16.rs b/tokio/src/loom/std/atomic_u16.rs index 86064fd16fe..f352e4fae5b 100644 --- a/tokio/src/loom/std/atomic_u16.rs +++ b/tokio/src/loom/std/atomic_u16.rs @@ -23,9 +23,11 @@ impl AtomicU16 { /// All mutations must have happened before the unsynchronized load. /// Additionally, there must be no concurrent mutations. pub(crate) unsafe fn unsync_load(&self) -> u16 { - self.load(std::sync::atomic::Ordering::Relaxed) // See - // core::ptr::read(self.inner.get() as *const u16) + #[cfg(miri)] + return self.load(std::sync::atomic::Ordering::Relaxed); + #[cfg(not(miri))] + return core::ptr::read(self.inner.get() as *const u16); } } diff --git a/tokio/src/loom/std/atomic_u32.rs b/tokio/src/loom/std/atomic_u32.rs index c0716eda2f3..422dc789037 100644 --- a/tokio/src/loom/std/atomic_u32.rs +++ b/tokio/src/loom/std/atomic_u32.rs @@ -23,9 +23,11 @@ impl AtomicU32 { /// All mutations must have happened before the unsynchronized load. /// Additionally, there must be no concurrent mutations. pub(crate) unsafe fn unsync_load(&self) -> u32 { - self.load(std::sync::atomic::Ordering::Relaxed) // See - // core::ptr::read(self.inner.get() as *const u32) + #[cfg(miri)] + return self.load(std::sync::atomic::Ordering::Relaxed); + #[cfg(not(miri))] + return core::ptr::read(self.inner.get() as *const u32); } } diff --git a/tokio/src/loom/std/atomic_usize.rs b/tokio/src/loom/std/atomic_usize.rs index d3e9b703503..03f2b566550 100644 --- a/tokio/src/loom/std/atomic_usize.rs +++ b/tokio/src/loom/std/atomic_usize.rs @@ -23,9 +23,11 @@ impl AtomicUsize { /// All mutations must have happened before the unsynchronized load. /// Additionally, there must be no concurrent mutations. pub(crate) unsafe fn unsync_load(&self) -> usize { - self.load(std::sync::atomic::Ordering::Relaxed) // See - // core::ptr::read(self.inner.get() as *const usize) + #[cfg(miri)] + return self.load(std::sync::atomic::Ordering::Relaxed); + #[cfg(not(miri))] + return core::ptr::read(self.inner.get() as *const usize); } pub(crate) fn with_mut(&mut self, f: impl FnOnce(&mut usize) -> R) -> R {