Skip to content

Commit

Permalink
Rollup merge of rust-lang#106836 - ibraheemdev:sync-sender-spin, r=Am…
Browse files Browse the repository at this point in the history
…anieu

Remove optimistic spinning from `mpsc::SyncSender`

Per rust-lang#106701 (comment).
Closes rust-lang#106804

r? `@Amanieu`
  • Loading branch information
matthiaskrgr authored Jan 26, 2023
2 parents 59fcb7a + ab20f8d commit 35a8d6f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
18 changes: 5 additions & 13 deletions library/std/src/sync/mpmc/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,10 @@ impl<T> Channel<T> {
) -> Result<(), SendTimeoutError<T>> {
let token = &mut Token::default();
loop {
// Try sending a message several times.
let backoff = Backoff::new();
loop {
if self.start_send(token) {
let res = unsafe { self.write(token, msg) };
return res.map_err(SendTimeoutError::Disconnected);
}

if backoff.is_completed() {
break;
} else {
backoff.spin_light();
}
// Try sending a message.
if self.start_send(token) {
let res = unsafe { self.write(token, msg) };
return res.map_err(SendTimeoutError::Disconnected);
}

if let Some(d) = deadline {
Expand Down Expand Up @@ -379,6 +370,7 @@ impl<T> Channel<T> {
pub(crate) fn recv(&self, deadline: Option<Instant>) -> Result<T, RecvTimeoutError> {
let token = &mut Token::default();
loop {
// Try receiving a message.
if self.start_recv(token) {
let res = unsafe { self.read(token) };
return res.map_err(|_| RecvTimeoutError::Disconnected);
Expand Down
12 changes: 2 additions & 10 deletions library/std/src/sync/mpmc/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ impl Backoff {

/// Backs off using lightweight spinning.
///
/// This method should be used for:
/// - Retrying an operation because another thread made progress. i.e. on CAS failure.
/// - Waiting for an operation to complete by spinning optimistically for a few iterations
/// before falling back to parking the thread (see `Backoff::is_completed`).
/// This method should be used for retrying an operation because another thread made
/// progress. i.e. on CAS failure.
#[inline]
pub fn spin_light(&self) {
let step = self.step.get().min(SPIN_LIMIT);
Expand All @@ -134,10 +132,4 @@ impl Backoff {

self.step.set(self.step.get() + 1);
}

/// Returns `true` if quadratic backoff has completed and parking the thread is advised.
#[inline]
pub fn is_completed(&self) -> bool {
self.step.get() > SPIN_LIMIT
}
}

0 comments on commit 35a8d6f

Please sign in to comment.