Skip to content

std::rt: Try stealing from all schedulers #9353

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

Merged
merged 1 commit into from
Sep 22, 2013
Merged
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
37 changes: 27 additions & 10 deletions src/libstd/rt/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,21 +379,20 @@ impl Scheduler {
return Some(task)
}
None => {
// Our naive stealing, try kinda hard.
rtdebug!("scheduler trying to steal");
let len = self.work_queues.len();
return self.try_steals(len/2);
return self.try_steals();
}
}
}

// With no backoff try stealing n times from the queues the
// scheduler knows about. This naive implementation can steal from
// our own queue or from other special schedulers.
fn try_steals(&mut self, n: uint) -> Option<~Task> {
for _ in range(0, n) {
let index = self.rng.gen_uint_range(0, self.work_queues.len());
let work_queues = &mut self.work_queues;
// Try stealing from all queues the scheduler knows about. This
// naive implementation can steal from our own queue or from other
// special schedulers.
fn try_steals(&mut self) -> Option<~Task> {
let work_queues = &mut self.work_queues;
let len = work_queues.len();
let start_index = self.rng.gen_uint_range(0, len);
for index in range(0, len).map(|i| (i + start_index) % len) {
match work_queues[index].steal() {
Some(task) => {
rtdebug!("found task by stealing");
Expand Down Expand Up @@ -1213,4 +1212,22 @@ mod test {
}
}

#[test]
fn dont_starve_1() {
use rt::comm::oneshot;

do stress_factor().times {
do run_in_mt_newsched_task {
let (port, chan) = oneshot();

// This task should not be able to starve the sender;
// The sender should get stolen to another thread.
do spawntask {
while !port.peek() { }
}

chan.send(());
}
}
}
}