Adding back-off instead of busy-spinning

Also changed the total number of spinning similar to Cilk
This commit is contained in:
LalehB 2014-09-27 15:12:22 -07:00 committed by Clark Gaebel
parent 005cfed6e9
commit b0d16462ff

View file

@ -16,6 +16,7 @@ use std::rand::weak_rng;
use std::sync::atomics::{AtomicUint, SeqCst}; use std::sync::atomics::{AtomicUint, SeqCst};
use std::sync::deque::{Abort, BufferPool, Data, Empty, Stealer, Worker}; use std::sync::deque::{Abort, BufferPool, Data, Empty, Stealer, Worker};
use std::task::TaskBuilder; use std::task::TaskBuilder;
use libc::funcs::posix88::unistd::usleep;
/// A unit of work. /// A unit of work.
/// ///
@ -70,7 +71,7 @@ struct WorkerThread<QueueData, WorkData> {
rng: XorShiftRng, rng: XorShiftRng,
} }
static SPIN_COUNT: uint = 1000; static SPIN_COUNT: uint = 128;
impl<QueueData: Send, WorkData: Send> WorkerThread<QueueData, WorkData> { impl<QueueData: Send, WorkData: Send> WorkerThread<QueueData, WorkData> {
/// The main logic. This function starts up the worker and listens for /// The main logic. This function starts up the worker and listens for
@ -87,6 +88,7 @@ impl<QueueData: Send, WorkData: Send> WorkerThread<QueueData, WorkData> {
// We're off! // We're off!
// //
// FIXME(pcwalton): Can't use labeled break or continue cross-crate due to a Rust bug. // 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 { loop {
// FIXME(pcwalton): Nasty workaround for the lack of labeled break/continue // FIXME(pcwalton): Nasty workaround for the lack of labeled break/continue
// cross-crate. // cross-crate.
@ -107,10 +109,15 @@ impl<QueueData: Send, WorkData: Send> WorkerThread<QueueData, WorkData> {
} }
Data(work) => { Data(work) => {
work_unit = work; work_unit = work;
back_off_sleep = 0 as u32;
break break
} }
} }
if (i>100) {
unsafe {usleep(back_off_sleep as u32)};
back_off_sleep = back_off_sleep + 5;
}
if i == SPIN_COUNT { if i == SPIN_COUNT {
match self.port.try_recv() { match self.port.try_recv() {
Ok(StopMsg) => { Ok(StopMsg) => {