2012-08-03 19:26:25 -04:00
|
|
|
/**
|
|
|
|
* Test performance of killing many tasks in a taskgroup.
|
|
|
|
* Along the way, tests various edge cases of ancestor group management.
|
|
|
|
* In particular, this tries to get each grandchild task to hit the
|
|
|
|
* "nobe_is_dead" case in each_ancestor only during task exit, but not during
|
|
|
|
* task spawn. This makes sure that defunct ancestor groups are handled correctly
|
|
|
|
* w.r.t. possibly leaving stale *rust_tasks lying around.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Creates in the background 'num_tasks' tasks, all blocked forever.
|
|
|
|
// Doesn't return until all such tasks are ready, but doesn't block forever itself.
|
|
|
|
fn grandchild_group(num_tasks: uint) {
|
2012-08-27 14:22:25 -07:00
|
|
|
let po = comm::Port();
|
2012-10-03 14:38:01 -07:00
|
|
|
let ch = comm::Chan(&po);
|
2012-08-03 19:26:25 -04:00
|
|
|
|
|
|
|
for num_tasks.times {
|
|
|
|
do task::spawn { // linked
|
|
|
|
comm::send(ch, ());
|
2012-08-27 14:22:25 -07:00
|
|
|
comm::recv(comm::Port::<()>()); // block forever
|
2012-08-03 19:26:25 -04:00
|
|
|
}
|
|
|
|
}
|
2012-10-12 12:32:36 -07:00
|
|
|
error!("Grandchild group getting started");
|
2012-08-03 19:26:25 -04:00
|
|
|
for num_tasks.times {
|
|
|
|
// Make sure all above children are fully spawned; i.e., enlisted in
|
|
|
|
// their ancestor groups.
|
|
|
|
comm::recv(po);
|
|
|
|
}
|
2012-10-12 12:32:36 -07:00
|
|
|
error!("Grandchild group ready to go.");
|
2012-08-03 19:26:25 -04:00
|
|
|
// Master grandchild task exits early.
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_supervised_blocking(myname: &str, +f: fn~()) {
|
2012-08-20 12:23:37 -07:00
|
|
|
let mut res = None;
|
2012-09-18 22:44:34 -07:00
|
|
|
task::task().future_result(|+r| res = Some(move r)).supervised().spawn(move f);
|
2012-10-12 12:32:36 -07:00
|
|
|
error!("%s group waiting", myname);
|
2012-10-22 19:01:37 -07:00
|
|
|
let x = option::unwrap(move res).recv();
|
2012-08-15 14:10:46 -07:00
|
|
|
assert x == task::Success;
|
2012-08-03 19:26:25 -04:00
|
|
|
}
|
|
|
|
|
2012-10-03 19:16:27 -07:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2012-08-03 19:26:25 -04:00
|
|
|
let args = if os::getenv(~"RUST_BENCH").is_some() {
|
|
|
|
~[~"", ~"100000"]
|
|
|
|
} else if args.len() <= 1u {
|
|
|
|
~[~"", ~"100"]
|
|
|
|
} else {
|
|
|
|
copy args
|
|
|
|
};
|
|
|
|
|
|
|
|
let num_tasks = uint::from_str(args[1]).get();
|
|
|
|
|
|
|
|
// Main group #0 waits for unsupervised group #1.
|
|
|
|
// Grandparent group #1 waits for middle group #2, then fails, killing #3.
|
|
|
|
// Middle group #2 creates grandchild_group #3, waits for it to be ready, exits.
|
2012-08-26 16:54:31 -07:00
|
|
|
let x: result::Result<(),()> = do task::try { // unlinked
|
2012-08-03 19:26:25 -04:00
|
|
|
do spawn_supervised_blocking("grandparent") {
|
|
|
|
do spawn_supervised_blocking("middle") {
|
|
|
|
grandchild_group(num_tasks);
|
|
|
|
}
|
|
|
|
// When grandchild group is ready to go, make the middle group exit.
|
2012-10-12 12:32:36 -07:00
|
|
|
error!("Middle group wakes up and exits");
|
2012-08-03 19:26:25 -04:00
|
|
|
}
|
|
|
|
// Grandparent group waits for middle group to be gone, then fails
|
2012-10-12 12:32:36 -07:00
|
|
|
error!("Grandparent group wakes up and fails");
|
2012-08-03 19:26:25 -04:00
|
|
|
fail;
|
|
|
|
};
|
|
|
|
assert x.is_err();
|
|
|
|
}
|