mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
remove optimistic spinning from mpsc::SyncSender
This commit is contained in:
parent
ca5d92d789
commit
ab20f8d5ba
@ -319,19 +319,10 @@ impl<T> Channel<T> {
|
|||||||
) -> Result<(), SendTimeoutError<T>> {
|
) -> Result<(), SendTimeoutError<T>> {
|
||||||
let token = &mut Token::default();
|
let token = &mut Token::default();
|
||||||
loop {
|
loop {
|
||||||
// Try sending a message several times.
|
// Try sending a message.
|
||||||
let backoff = Backoff::new();
|
if self.start_send(token) {
|
||||||
loop {
|
let res = unsafe { self.write(token, msg) };
|
||||||
if self.start_send(token) {
|
return res.map_err(SendTimeoutError::Disconnected);
|
||||||
let res = unsafe { self.write(token, msg) };
|
|
||||||
return res.map_err(SendTimeoutError::Disconnected);
|
|
||||||
}
|
|
||||||
|
|
||||||
if backoff.is_completed() {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
backoff.spin_light();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(d) = deadline {
|
if let Some(d) = deadline {
|
||||||
@ -379,6 +370,7 @@ impl<T> Channel<T> {
|
|||||||
pub(crate) fn recv(&self, deadline: Option<Instant>) -> Result<T, RecvTimeoutError> {
|
pub(crate) fn recv(&self, deadline: Option<Instant>) -> Result<T, RecvTimeoutError> {
|
||||||
let token = &mut Token::default();
|
let token = &mut Token::default();
|
||||||
loop {
|
loop {
|
||||||
|
// Try receiving a message.
|
||||||
if self.start_recv(token) {
|
if self.start_recv(token) {
|
||||||
let res = unsafe { self.read(token) };
|
let res = unsafe { self.read(token) };
|
||||||
return res.map_err(|_| RecvTimeoutError::Disconnected);
|
return res.map_err(|_| RecvTimeoutError::Disconnected);
|
||||||
|
@ -105,10 +105,8 @@ impl Backoff {
|
|||||||
|
|
||||||
/// Backs off using lightweight spinning.
|
/// Backs off using lightweight spinning.
|
||||||
///
|
///
|
||||||
/// This method should be used for:
|
/// This method should be used for retrying an operation because another thread made
|
||||||
/// - Retrying an operation because another thread made progress. i.e. on CAS failure.
|
/// 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`).
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn spin_light(&self) {
|
pub fn spin_light(&self) {
|
||||||
let step = self.step.get().min(SPIN_LIMIT);
|
let step = self.step.get().min(SPIN_LIMIT);
|
||||||
@ -134,10 +132,4 @@ impl Backoff {
|
|||||||
|
|
||||||
self.step.set(self.step.get() + 1);
|
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user