2012-12-10 17:32:48 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-08-02 00:07:12 -04:00
|
|
|
// Test performance of a task "spawn ladder", in which children task have many
|
|
|
|
// many ancestor taskgroups, but with only a few such groups alive at a time.
|
|
|
|
// Each child task has to enlist as a descendant in each of its ancestor
|
|
|
|
// groups, but that shouldn't have to happen for already-dead groups.
|
|
|
|
//
|
2012-08-03 19:26:25 -04:00
|
|
|
// The filename is a song reference; google it in quotes.
|
2012-08-02 00:07:12 -04:00
|
|
|
|
2012-08-28 11:11:15 -07:00
|
|
|
fn child_generation(gens_left: uint, -c: pipes::Chan<()>) {
|
2012-08-02 00:07:12 -04:00
|
|
|
// This used to be O(n^2) in the number of generations that ever existed.
|
|
|
|
// With this code, only as many generations are alive at a time as tasks
|
2012-08-03 19:26:25 -04:00
|
|
|
// alive at a time,
|
2013-02-15 02:44:18 -08:00
|
|
|
let c = ~mut Some(c);
|
|
|
|
do task::spawn_supervised || {
|
2012-08-06 17:15:44 -04:00
|
|
|
let c = option::swap_unwrap(c);
|
2012-08-02 00:07:12 -04:00
|
|
|
if gens_left & 1 == 1 {
|
|
|
|
task::yield(); // shake things up a bit
|
|
|
|
}
|
|
|
|
if gens_left > 0 {
|
2013-02-15 02:44:18 -08:00
|
|
|
child_generation(gens_left - 1, c); // recurse
|
2012-08-06 17:15:44 -04:00
|
|
|
} else {
|
|
|
|
c.send(())
|
2012-08-02 00:07:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 19:16:27 -07:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2012-08-02 00:07:12 -04:00
|
|
|
let args = if os::getenv(~"RUST_BENCH").is_some() {
|
|
|
|
~[~"", ~"100000"]
|
2012-09-18 22:44:34 -07:00
|
|
|
} else if args.len() <= 1 {
|
2012-08-02 00:07:12 -04:00
|
|
|
~[~"", ~"100"]
|
|
|
|
} else {
|
|
|
|
copy args
|
|
|
|
};
|
|
|
|
|
2012-12-11 12:26:41 -08:00
|
|
|
let (p,c) = pipes::stream();
|
2013-02-15 02:44:18 -08:00
|
|
|
child_generation(uint::from_str(args[1]).get(), c);
|
2012-08-06 17:15:44 -04:00
|
|
|
if p.try_recv().is_none() {
|
2013-02-11 19:26:38 -08:00
|
|
|
fail!(~"it happened when we slumbered");
|
2012-08-06 17:15:44 -04:00
|
|
|
}
|
2012-08-02 00:07:12 -04:00
|
|
|
}
|