Skip to content

Commit

Permalink
Relax SeqCst in heph-inbox
Browse files Browse the repository at this point in the history
This replaces all SeqCst atomic operations with Release, Acquire or
AcqRel.

Also see rust-lang/rust#122729.
  • Loading branch information
Thomasdezeeuw committed Apr 14, 2024
1 parent 7ed5678 commit 8dce55f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions inbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ fn try_send<T>(channel: &Channel<T>, value: T) -> Result<(), SendError<T>> {
/// enough for most practical use cases.
impl<T> Clone for Sender<T> {
fn clone(&self) -> Sender<T> {
// For the reasoning behind this relaxed ordering see `Arc::clone`.
// SAFETY: for the reasoning behind this relaxed ordering see `Arc::clone`.
let old_ref_count = self.channel().ref_count.fetch_add(1, Ordering::Relaxed);
debug_assert!(old_ref_count & SENDER_ACCESS != 0);
Sender {
Expand Down Expand Up @@ -452,7 +452,7 @@ impl<T> Drop for Sender<T> {
return;
}

// For the reasoning behind this ordering see `Arc::drop`.
// SAFETY: for the reasoning behind this ordering see `Arc::drop`.
fence!(self.channel().ref_count, Ordering::Acquire);

// Drop the memory.
Expand Down
6 changes: 3 additions & 3 deletions inbox/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct AwokenCount {

impl PartialEq<usize> for AwokenCount {
fn eq(&self, other: &usize) -> bool {
self.inner.count.load(Ordering::SeqCst) == *other
self.inner.count.load(Ordering::Acquire) == *other
}
}

Expand All @@ -33,11 +33,11 @@ struct WakerInner {

impl Wake for WakerInner {
fn wake(self: Arc<Self>) {
let _ = self.count.fetch_add(1, Ordering::SeqCst);
let _ = self.count.fetch_add(1, Ordering::AcqRel);
}

fn wake_by_ref(self: &Arc<Self>) {
let _ = self.count.fetch_add(1, Ordering::SeqCst);
let _ = self.count.fetch_add(1, Ordering::AcqRel);
}
}

Expand Down
10 changes: 5 additions & 5 deletions inbox/src/waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl WakerRegistration {
let stored_waker = self.waker.read().unwrap();
if let Some(stored_waker) = &*stored_waker {
if stored_waker.will_wake(waker) {
self.needs_wakeup.store(true, Ordering::SeqCst);
self.needs_wakeup.store(true, Ordering::Release);
return false;
}
}
Expand All @@ -36,26 +36,26 @@ impl WakerRegistration {
// again.
if let Some(stored_waker) = &*stored_waker {
if stored_waker.will_wake(waker) {
self.needs_wakeup.store(true, Ordering::SeqCst);
self.needs_wakeup.store(true, Ordering::Release);
return false;
}
}
*stored_waker = Some(waker.clone());
drop(stored_waker);

self.needs_wakeup.store(true, Ordering::SeqCst);
self.needs_wakeup.store(true, Ordering::Release);
true
}

/// Wake the waker registered, if required.
pub(crate) fn wake(&self) {
if !self.needs_wakeup.load(Ordering::SeqCst) {
if !self.needs_wakeup.load(Ordering::Acquire) {
// Receiver doesn't need a wake-up.
return;
}

// Mark that we've woken and after actually do the waking.
if self.needs_wakeup.swap(false, Ordering::SeqCst) {
if self.needs_wakeup.swap(false, Ordering::AcqRel) {
if let Some(waker) = &*self.waker.read().unwrap() {
waker.wake_by_ref();
}
Expand Down
6 changes: 3 additions & 3 deletions inbox/tests/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct AwokenCount {
impl AwokenCount {
/// Get the current count.
pub fn get(&self) -> usize {
self.inner.count.load(Ordering::SeqCst)
self.inner.count.load(Ordering::Acquire)
}
}

Expand All @@ -39,11 +39,11 @@ struct WakerInner {

impl Wake for WakerInner {
fn wake(self: Arc<Self>) {
let _ = self.count.fetch_add(1, Ordering::SeqCst);
let _ = self.count.fetch_add(1, Ordering::AcqRel);
}

fn wake_by_ref(self: &Arc<Self>) {
let _ = self.count.fetch_add(1, Ordering::SeqCst);
let _ = self.count.fetch_add(1, Ordering::AcqRel);
}
}

Expand Down

0 comments on commit 8dce55f

Please sign in to comment.