]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
1a4d82fc | 11 | // Test performance of a task "spawn ladder", in which children task have |
223e47cc LB |
12 | // many ancestor taskgroups, but with only a few such groups alive at a time. |
13 | // Each child task has to enlist as a descendant in each of its ancestor | |
14 | // groups, but that shouldn't have to happen for already-dead groups. | |
15 | // | |
16 | // The filename is a song reference; google it in quotes. | |
17 | ||
1a4d82fc JJ |
18 | // ignore-pretty very bad with line comments |
19 | ||
20 | use std::sync::mpsc::{channel, Sender}; | |
85aaf69f SL |
21 | use std::env; |
22 | use std::thread; | |
223e47cc | 23 | |
c34b1796 | 24 | fn child_generation(gens_left: usize, tx: Sender<()>) { |
223e47cc LB |
25 | // This used to be O(n^2) in the number of generations that ever existed. |
26 | // With this code, only as many generations are alive at a time as tasks | |
27 | // alive at a time, | |
85aaf69f | 28 | thread::spawn(move|| { |
223e47cc | 29 | if gens_left & 1 == 1 { |
85aaf69f | 30 | thread::yield_now(); // shake things up a bit |
223e47cc LB |
31 | } |
32 | if gens_left > 0 { | |
1a4d82fc | 33 | child_generation(gens_left - 1, tx); // recurse |
223e47cc | 34 | } else { |
1a4d82fc | 35 | tx.send(()).unwrap() |
223e47cc | 36 | } |
1a4d82fc | 37 | }); |
223e47cc LB |
38 | } |
39 | ||
40 | fn main() { | |
85aaf69f SL |
41 | let args = env::args(); |
42 | let args = if env::var_os("RUST_BENCH").is_some() { | |
1a4d82fc | 43 | vec!("".to_string(), "100000".to_string()) |
223e47cc | 44 | } else if args.len() <= 1 { |
1a4d82fc | 45 | vec!("".to_string(), "100".to_string()) |
223e47cc | 46 | } else { |
85aaf69f | 47 | args.collect() |
223e47cc LB |
48 | }; |
49 | ||
1a4d82fc JJ |
50 | let (tx, rx) = channel(); |
51 | child_generation(args[1].parse().unwrap(), tx); | |
52 | if rx.recv().is_err() { | |
53 | panic!("it happened when we slumbered"); | |
223e47cc LB |
54 | } |
55 | } |