Skip to content

Commit c767643

Browse files
committed
std::rt: Try stealing from all schedulers
This guarantees that if there is work to do it will be found
1 parent 85c0fb7 commit c767643

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/libstd/rt/sched.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -379,21 +379,20 @@ impl Scheduler {
379379
return Some(task)
380380
}
381381
None => {
382-
// Our naive stealing, try kinda hard.
383382
rtdebug!("scheduler trying to steal");
384-
let len = self.work_queues.len();
385-
return self.try_steals(len/2);
383+
return self.try_steals();
386384
}
387385
}
388386
}
389387

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

1215+
#[test]
1216+
fn dont_starve_1() {
1217+
use rt::comm::oneshot;
1218+
1219+
do stress_factor().times {
1220+
do run_in_mt_newsched_task {
1221+
let (port, chan) = oneshot();
1222+
1223+
// This task should not be able to starve the sender;
1224+
// The sender should get stolen to another thread.
1225+
do spawntask {
1226+
while !port.peek() { }
1227+
}
1228+
1229+
chan.send(());
1230+
}
1231+
}
1232+
}
12161233
}

0 commit comments

Comments
 (0)