Skip to content

Commit

Permalink
fallback: Use optimistic_read in read
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 23, 2023
1 parent 5c3a43b commit fd51324
Showing 1 changed file with 3 additions and 23 deletions.
26 changes: 3 additions & 23 deletions src/imp/fallback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,9 @@ macro_rules! atomic {

#[inline]
fn read(&self, _guard: &SeqLockWriteGuard<'static>) -> $int_type {
// SAFETY:
// - The guard guarantees that we hold the lock to write.
// - The raw pointer is valid because we got it from a reference.
//
// Unlike optimistic_read/write, the atomic operation is not required,
// because we hold the lock to write so that other threads cannot
// perform concurrent write operations.
//
// At the hardware level, core::sync::atomic::Atomic*::load used in optimistic_read
// may be lowered to atomic write operations by LLVM, but it is still considered a
// read operation from the view of the (software) memory model, except that it is
// not allowed in read-only memory (due to UnsafeCell, self.v is not read-only memory).
// See also https://github.com/rust-lang/miri/issues/2463.
// (Note that the above property is about the assembly generated by inline assembly
// or LLVM's backend. Doing it using write operations written in normal Rust code
// or LLVM IR is considered UB, even if it never mutates the value. See also the
// above Miri issue and https://github.com/rust-lang/rust/issues/32976#issuecomment-446775360)
//
// Also, according to atomic-memcpy's asm test, there seems
// to be no tier 1 or tier 2 platform that generates such code
// for a pointer-width relaxed load + acquire fence:
// https://github.com/taiki-e/atomic-memcpy/tree/v0.1.3/tests/asm-test/asm
unsafe { self.v.get().read() }
// This calls optimistic_read that can return teared value, but the resulting value
// is guaranteed not to be teared because we hold the lock to write.
self.optimistic_read()
}

#[inline]
Expand Down

0 comments on commit fd51324

Please sign in to comment.