Skip to content

Commit

Permalink
Avoid suboptimal <pointer>::replace
Browse files Browse the repository at this point in the history
There was a PR to improve it, but it was not merged for some reason.
rust-lang/rust#98197
  • Loading branch information
taiki-e committed Oct 27, 2023
1 parent bda3891 commit 17b425d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 3 additions & 1 deletion bench/benches/imp/spinlock_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ macro_rules! atomic_int {
// pointer passed in is valid because we got it from a reference.
unsafe {
let _guard = lock(self.v.get() as usize);
self.v.get().replace(val)
let prev = self.v.get().read();
self.v.get().write(val);
prev
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/imp/interrupt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ impl<T> AtomicPtr<T> {
// SAFETY: any data races are prevented by disabling interrupts (see
// module-level comments) and the raw pointer is valid because we got it
// from a reference.
with(|| unsafe { self.p.get().replace(ptr) })
with(|| unsafe {
let prev = self.p.get().read();
self.p.get().write(ptr);
prev
})
}

#[inline]
Expand Down Expand Up @@ -406,7 +410,11 @@ macro_rules! atomic_int {
// SAFETY: any data races are prevented by disabling interrupts (see
// module-level comments) and the raw pointer is valid because we got it
// from a reference.
with(|| unsafe { self.v.get().replace(val) })
with(|| unsafe {
let prev = self.v.get().read();
self.v.get().write(val);
prev
})
}

#[inline]
Expand Down Expand Up @@ -685,7 +693,11 @@ macro_rules! atomic_int {
// SAFETY: any data races are prevented by disabling interrupts (see
// module-level comments) and the raw pointer is valid because we got it
// from a reference.
with(|| unsafe { self.v.get().replace(val) })
with(|| unsafe {
let prev = self.v.get().read();
self.v.get().write(val);
prev
})
}

#[inline]
Expand Down

0 comments on commit 17b425d

Please sign in to comment.