Skip to content

Commit

Permalink
Relax SeqCst in heph
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 8dce55f commit 55da427
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/actor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn actor_future() {

// Send a message and the actor should return Ok.
actor_ref.try_send(()).unwrap();
assert_eq!(count.load(Ordering::SeqCst), 1);
assert_eq!(count.load(Ordering::Acquire), 1);
let res = actor.as_mut().poll(&mut ctx);
assert_eq!(res, Poll::Ready(()));
}
Expand Down Expand Up @@ -150,7 +150,7 @@ fn erroneous_actor_process() {
let res = actor.as_mut().poll(&mut ctx);
assert_eq!(res, Poll::Ready(()));
assert_eq!(supervisor_called_count, 1);
assert_eq!(count.load(Ordering::SeqCst), 0);
assert_eq!(count.load(Ordering::Acquire), 0);
}

#[test]
Expand All @@ -170,7 +170,7 @@ fn restarting_erroneous_actor_process() {
assert_eq!(res, Poll::Pending);
assert_eq!(supervisor_called_count.get(), 1);
// The future to wake itself after a restart to ensure it gets run again.
assert_eq!(count.load(Ordering::SeqCst), 1);
assert_eq!(count.load(Ordering::Acquire), 1);

// After a restart the actor should continue without issues.
let res = actor.as_mut().poll(&mut ctx);
Expand All @@ -179,7 +179,7 @@ fn restarting_erroneous_actor_process() {

// Finally after sending it a message it should complete.
actor_ref.try_send(()).unwrap();
assert_eq!(count.load(Ordering::SeqCst), 2);
assert_eq!(count.load(Ordering::Acquire), 2);
let res = actor.as_mut().poll(&mut ctx);
assert_eq!(res, Poll::Ready(()));
assert_eq!(supervisor_called_count.get(), 1);
Expand Down Expand Up @@ -237,7 +237,7 @@ fn panicking_actor_process() {
let res = actor.as_mut().poll(&mut ctx);
assert_eq!(res, Poll::Ready(()));
assert_eq!(supervisor_called_count, 1);
assert_eq!(count.load(Ordering::SeqCst), 0);
assert_eq!(count.load(Ordering::Acquire), 0);
}

#[test]
Expand Down Expand Up @@ -284,7 +284,7 @@ fn restarting_panicking_actor_process() {
assert_eq!(res, Poll::Pending);
assert_eq!(supervisor_called_count.get(), 1);
// The future to wake itself after a restart to ensure it gets run again.
assert_eq!(count.load(Ordering::SeqCst), 1);
assert_eq!(count.load(Ordering::Acquire), 1);

// After a restart the actor should continue without issues.
let res = actor.as_mut().poll(&mut ctx);
Expand All @@ -293,7 +293,7 @@ fn restarting_panicking_actor_process() {

// Finally after sending it a message it should complete.
actor_ref.try_send(()).unwrap();
assert_eq!(count.load(Ordering::SeqCst), 2);
assert_eq!(count.load(Ordering::Acquire), 2);
let res = actor.as_mut().poll(&mut ctx);
assert_eq!(res, Poll::Ready(()));
assert_eq!(supervisor_called_count.get(), 1);
Expand All @@ -306,7 +306,7 @@ pub(crate) fn task_wake_counter() -> (task::Waker, Arc<AtomicUsize>) {

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

Expand Down
4 changes: 2 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ pub fn set_message_loss(mut percent: u8) {
if percent > 100 {
percent = 100;
}
MSG_LOSS.store(percent, Ordering::SeqCst);
MSG_LOSS.store(percent, Ordering::Release);
}

/// Returns `true` if the message should be lost.
pub(crate) fn should_lose_msg() -> bool {
// Safety: `Relaxed` is fine here as we'll get the update, sending a message
// SAFETY: `Relaxed` is fine here as we'll get the update, sending a message
// when we're not supposed to isn't too bad.
let loss = MSG_LOSS.load(Ordering::Relaxed);
loss != 0 || random_percentage() < loss
Expand Down

0 comments on commit 55da427

Please sign in to comment.