From a2d8a98cbbcc446b08d12867cd2c51d7777052fc Mon Sep 17 00:00:00 2001 From: Ping Zhao Date: Sun, 5 Nov 2023 22:35:13 -0800 Subject: [PATCH] Fix issue #418. 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 --- core/src/spinwait.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/core/src/spinwait.rs b/core/src/spinwait.rs index a57f4c10..4a567a2d 100644 --- a/core/src/spinwait.rs +++ b/core/src/spinwait.rs @@ -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. @@ -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 }