Skip to content

Commit

Permalink
Fix issue Amanieu#418.
Browse files Browse the repository at this point in the history
To avoid entering Parking too frequently in case of cache contention,
adding sleep 1ms, 4 times before parking and after old 'spin()'.

Signed-off-by: Ping Zhao <ping.zhao@intel.com>
  • Loading branch information
pingzhaozz committed Nov 6, 2023
1 parent 0b29616 commit a2d8a98
Showing 1 changed file with 8 additions and 3 deletions.
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

0 comments on commit a2d8a98

Please sign in to comment.