3 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
4 // file at the top-level directory of this distribution and at
5 // http://rust-lang.org/COPYRIGHT.
7 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 // option. This file may not be copied, modified, or distributed
11 // except according to those terms.
14 * Test performance of killing many tasks in a taskgroup.
15 * Along the way, tests various edge cases of ancestor group management.
16 * In particular, this tries to get each grandchild task to hit the
17 * "nobe_is_dead" case in each_ancestor only during task exit, but not during
18 * task spawn. This makes sure that defunct ancestor groups are handled correctly
19 * w.r.t. possibly leaving stale *rust_tasks lying around.
22 // Creates in the background 'num_tasks' tasks, all blocked forever.
23 // Doesn't return until all such tasks are ready, but doesn't block forever itself.
31 fn grandchild_group(num_tasks: uint) {
32 let (po, ch) = stream();
33 let ch = SharedChan::new(ch);
35 for _ in range(0, num_tasks) {
37 do task::spawn { // linked
39 let (p, _c) = stream::<()>();
40 p.recv(); // block forever
43 error!("Grandchild group getting started");
44 for _ in range(0, num_tasks) {
45 // Make sure all above children are fully spawned; i.e., enlisted in
46 // their ancestor groups.
49 error!("Grandchild group ready to go.");
50 // Master grandchild task exits early.
53 fn spawn_supervised_blocking(myname: &str, f: ~fn()) {
55 let mut builder = task::task();
56 builder.future_result(|r| res = Some(r));
59 error!("%s group waiting", myname);
60 let x = res.unwrap().recv();
61 assert_eq!(x, task::Success);
65 let args = os::args();
66 let args = if os::getenv("RUST_BENCH").is_some() {
68 } else if args.len() <= 1u {
74 let num_tasks = uint::from_str(args[1]).unwrap();
76 // Main group #0 waits for unsupervised group #1.
77 // Grandparent group #1 waits for middle group #2, then fails, killing #3.
78 // Middle group #2 creates grandchild_group #3, waits for it to be ready, exits.
79 let x: result::Result<(),()> = do task::try { // unlinked
80 do spawn_supervised_blocking("grandparent") {
81 do spawn_supervised_blocking("middle") {
82 grandchild_group(num_tasks);
84 // When grandchild group is ready to go, make the middle group exit.
85 error!("Middle group wakes up and exits");
87 // Grandparent group waits for middle group to be gone, then fails
88 error!("Grandparent group wakes up and fails");