]> git.lizzy.rs Git - rust.git/blob - src/test/bench/task-perf-linked-failure.rs
remove `extra::iter`
[rust.git] / src / test / bench / task-perf-linked-failure.rs
1 // xfail-pretty
2
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.
6 //
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.
12
13 /**
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.
20  */
21
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.
24
25 use std::comm::*;
26 use std::os;
27 use std::result;
28 use std::task;
29 use std::uint;
30
31 fn grandchild_group(num_tasks: uint) {
32     let (po, ch) = stream();
33     let ch = SharedChan::new(ch);
34
35     for _ in range(0, num_tasks) {
36         let ch = ch.clone();
37         do task::spawn { // linked
38             ch.send(());
39             let (p, _c) = stream::<()>();
40             p.recv(); // block forever
41         }
42     }
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.
47         po.recv();
48     }
49     error!("Grandchild group ready to go.");
50     // Master grandchild task exits early.
51 }
52
53 fn spawn_supervised_blocking(myname: &str, f: ~fn()) {
54     let mut res = None;
55     let mut builder = task::task();
56     builder.future_result(|r| res = Some(r));
57     builder.supervised();
58     builder.spawn(f);
59     error!("%s group waiting", myname);
60     let x = res.unwrap().recv();
61     assert_eq!(x, task::Success);
62 }
63
64 fn main() {
65     let args = os::args();
66     let args = if os::getenv("RUST_BENCH").is_some() {
67         ~[~"", ~"100000"]
68     } else if args.len() <= 1u {
69         ~[~"", ~"100"]
70     } else {
71         args.clone()
72     };
73
74     let num_tasks = uint::from_str(args[1]).unwrap();
75
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);
83             }
84             // When grandchild group is ready to go, make the middle group exit.
85             error!("Middle group wakes up and exits");
86         }
87         // Grandparent group waits for middle group to be gone, then fails
88         error!("Grandparent group wakes up and fails");
89         fail!();
90     };
91     assert!(x.is_err());
92 }