From b0d16462ff4e214ade8357b1a9e40b943a74ae5b Mon Sep 17 00:00:00 2001 From: LalehB Date: Sat, 27 Sep 2014 15:12:22 -0700 Subject: [PATCH 1/2] Adding back-off instead of busy-spinning Also changed the total number of spinning similar to Cilk --- components/util/workqueue.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/components/util/workqueue.rs b/components/util/workqueue.rs index 3a0b3e32f915..c2b81741e0c9 100644 --- a/components/util/workqueue.rs +++ b/components/util/workqueue.rs @@ -16,6 +16,7 @@ use std::rand::weak_rng; use std::sync::atomics::{AtomicUint, SeqCst}; use std::sync::deque::{Abort, BufferPool, Data, Empty, Stealer, Worker}; use std::task::TaskBuilder; +use libc::funcs::posix88::unistd::usleep; /// A unit of work. /// @@ -70,7 +71,7 @@ struct WorkerThread { rng: XorShiftRng, } -static SPIN_COUNT: uint = 1000; +static SPIN_COUNT: uint = 128; impl WorkerThread { /// The main logic. This function starts up the worker and listens for @@ -87,6 +88,7 @@ impl WorkerThread { // We're off! // // FIXME(pcwalton): Can't use labeled break or continue cross-crate due to a Rust bug. + let mut back_off_sleep = 0 as u32; loop { // FIXME(pcwalton): Nasty workaround for the lack of labeled break/continue // cross-crate. @@ -107,10 +109,15 @@ impl WorkerThread { } Data(work) => { work_unit = work; + back_off_sleep = 0 as u32; break } } + if (i>100) { + unsafe {usleep(back_off_sleep as u32)}; + back_off_sleep = back_off_sleep + 5; + } if i == SPIN_COUNT { match self.port.try_recv() { Ok(StopMsg) => { From 293969cb7d0a97ad61f580d2e3f5f73b39740f71 Mon Sep 17 00:00:00 2001 From: Clark Gaebel Date: Mon, 27 Oct 2014 14:19:44 -0700 Subject: [PATCH 2/2] Addressed code review comments. --- components/util/workqueue.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/components/util/workqueue.rs b/components/util/workqueue.rs index c2b81741e0c9..8708d8585a6c 100644 --- a/components/util/workqueue.rs +++ b/components/util/workqueue.rs @@ -71,7 +71,9 @@ struct WorkerThread { rng: XorShiftRng, } -static SPIN_COUNT: uint = 128; +static SPIN_COUNT: u32 = 128; +static SPINS_UNTIL_BACKOFF: u32 = 100; +static BACKOFF_INCREMENT_IN_US: u32 = 5; impl WorkerThread { /// The main logic. This function starts up the worker and listens for @@ -85,10 +87,11 @@ impl WorkerThread { ExitMsg => return, }; + let mut back_off_sleep = 0 as u32; + // We're off! // // FIXME(pcwalton): Can't use labeled break or continue cross-crate due to a Rust bug. - let mut back_off_sleep = 0 as u32; loop { // FIXME(pcwalton): Nasty workaround for the lack of labeled break/continue // cross-crate. @@ -114,10 +117,13 @@ impl WorkerThread { } } - if (i>100) { - unsafe {usleep(back_off_sleep as u32)}; - back_off_sleep = back_off_sleep + 5; + if i > SPINS_UNTIL_BACKOFF { + unsafe { + usleep(back_off_sleep as u32); + } + back_off_sleep += BACKOFF_INCREMENT_IN_US; } + if i == SPIN_COUNT { match self.port.try_recv() { Ok(StopMsg) => {