Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions futures-channel/src/mpsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,17 +923,16 @@ impl<T> Clone for UnboundedSenderInner<T> {
debug_assert!(curr < MAX_BUFFER);

let next = curr + 1;
let actual = self.inner.num_senders.compare_and_swap(curr, next, SeqCst);

// The ABA problem doesn't matter here. We only care that the
// number of senders never exceeds the maximum.
if actual == curr {
return Self {
inner: self.inner.clone(),
};
match self.inner.num_senders.compare_exchange(curr, next, SeqCst, SeqCst) {
Ok(_) => {
// The ABA problem doesn't matter here. We only care that the
// number of senders never exceeds the maximum.
return Self {
inner: self.inner.clone(),
};
}
Err(actual) => curr = actual,
}

curr = actual;
}
}
}
Expand All @@ -954,19 +953,18 @@ impl<T> Clone for BoundedSenderInner<T> {
debug_assert!(curr < self.inner.max_senders());

let next = curr + 1;
let actual = self.inner.num_senders.compare_and_swap(curr, next, SeqCst);

// The ABA problem doesn't matter here. We only care that the
// number of senders never exceeds the maximum.
if actual == curr {
return Self {
inner: self.inner.clone(),
sender_task: Arc::new(Mutex::new(SenderTask::new())),
maybe_parked: false,
};
match self.inner.num_senders.compare_exchange(curr, next, SeqCst, SeqCst) {
Ok(_) => {
// The ABA problem doesn't matter here. We only care that the
// number of senders never exceeds the maximum.
return Self {
inner: self.inner.clone(),
sender_task: Arc::new(Mutex::new(SenderTask::new())),
maybe_parked: false,
};
}
Err(actual) => curr = actual,
}

curr = actual;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion futures-core/src/task/__internal/atomic_waker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ impl AtomicWaker {
/// }
/// ```
pub fn register(&self, waker: &Waker) {
match self.state.compare_and_swap(WAITING, REGISTERING, Acquire) {
match self
.state
.compare_exchange(WAITING, REGISTERING, Acquire, Acquire)
.unwrap_or_else(|x| x)
{
WAITING => {
unsafe {
// Locked acquired, update the waker cell
Expand Down
27 changes: 18 additions & 9 deletions futures-util/src/future/future/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,12 @@ where

inner.record_waker(&mut this.waker_key, cx);

match inner.notifier.state.compare_and_swap(IDLE, POLLING, SeqCst) {
match inner
.notifier
.state
.compare_exchange(IDLE, POLLING, SeqCst, SeqCst)
.unwrap_or_else(|x| x)
{
IDLE => {
// Lock acquired, fall through
}
Expand Down Expand Up @@ -255,14 +260,18 @@ where

match future.poll(&mut cx) {
Poll::Pending => {
match inner.notifier.state.compare_and_swap(POLLING, IDLE, SeqCst) {
POLLING => {
// Success
drop(_reset);
this.inner = Some(inner);
return Poll::Pending;
}
_ => unreachable!(),
if inner
.notifier
.state
.compare_exchange(POLLING, IDLE, SeqCst, SeqCst)
.is_ok()
{
// Success
drop(_reset);
this.inner = Some(inner);
return Poll::Pending;
} else {
unreachable!()
}
}
Poll::Ready(output) => output,
Expand Down