Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #418. #419

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions core/src/spinwait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
// copied, modified, or distributed except according to those terms.

use crate::thread_parker;
use core::hint::spin_loop;
use core::{hint::spin_loop, time::Duration};
use std::thread;

// Wastes some CPU time for the given number of iterations,
// using a hint to indicate to the CPU that we are spinning.
Expand Down Expand Up @@ -44,16 +45,20 @@ impl SpinWait {
///
/// The spin strategy will initially use a CPU-bound loop but will fall back
/// to yielding the CPU to the OS after a few iterations.
/// Before parking, 'spin()' will finally try sleep 1ms, 4 times to avoid
/// entering parking too frequently in case of cache contention.
#[inline]
pub fn spin(&mut self) -> bool {
if self.counter >= 10 {
if self.counter >= 15 {
return false;
}
self.counter += 1;
if self.counter <= 3 {
cpu_relax(1 << self.counter);
} else {
} else if self.counter <= 10 {
thread_parker::thread_yield();
} else {
thread::sleep(Duration::from_millis(1));
}
true
}
Expand Down