2012-12-10 19:32:48 -06: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-02-15 13:52:43 -06:00
|
|
|
// Test for concurrent tasks
|
|
|
|
|
2013-02-02 05:10:12 -06:00
|
|
|
use core::comm::*;
|
2012-02-15 13:52:43 -06:00
|
|
|
|
2013-01-29 01:54:39 -06:00
|
|
|
fn calc(children: uint, parent_wait_chan: &Chan<Chan<Chan<int>>>) {
|
2012-02-15 13:52:43 -06:00
|
|
|
|
2013-01-29 01:54:39 -06:00
|
|
|
let wait_ports: ~[Port<Chan<Chan<int>>>] = do vec::from_fn(children) |_| {
|
|
|
|
let (wait_port, wait_chan) = stream::<Chan<Chan<int>>>();
|
2012-07-04 14:04:28 -05:00
|
|
|
do task::spawn {
|
2013-01-29 01:54:39 -06:00
|
|
|
calc(children / 2, &wait_chan);
|
2012-02-15 13:52:43 -06:00
|
|
|
}
|
2013-01-29 01:54:39 -06:00
|
|
|
wait_port
|
|
|
|
};
|
2012-02-15 13:52:43 -06:00
|
|
|
|
2013-01-29 01:54:39 -06:00
|
|
|
let child_start_chans: ~[Chan<Chan<int>>] = vec::map_consume(wait_ports, |port| port.recv());
|
2012-02-15 13:52:43 -06:00
|
|
|
|
2013-01-29 01:54:39 -06:00
|
|
|
let (start_port, start_chan) = stream::<Chan<int>>();
|
|
|
|
parent_wait_chan.send(start_chan);
|
|
|
|
let parent_result_chan: Chan<int> = start_port.recv();
|
2012-02-15 13:52:43 -06:00
|
|
|
|
2013-01-29 01:54:39 -06:00
|
|
|
let child_sum_ports: ~[Port<int>] = do vec::map_consume(child_start_chans) |child_start_chan| {
|
|
|
|
let (child_sum_port, child_sum_chan) = stream::<int>();
|
|
|
|
child_start_chan.send(child_sum_chan);
|
|
|
|
child_sum_port
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut sum = 0;
|
|
|
|
vec::consume(child_sum_ports, |_, sum_port| sum += sum_port.recv() );
|
2012-02-15 13:52:43 -06:00
|
|
|
|
2013-01-29 01:54:39 -06:00
|
|
|
parent_result_chan.send(sum + 1);
|
2012-02-15 13:52:43 -06:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = if os::getenv(~"RUST_BENCH").is_some() {
|
2013-01-29 01:54:39 -06:00
|
|
|
~[~"", ~"30"]
|
2012-05-24 00:53:50 -05:00
|
|
|
} else if args.len() <= 1u {
|
2013-01-29 01:54:39 -06:00
|
|
|
~[~"", ~"10"]
|
2012-02-15 13:52:43 -06:00
|
|
|
} else {
|
2012-05-24 00:53:50 -05:00
|
|
|
args
|
2012-02-15 13:52:43 -06:00
|
|
|
};
|
2012-05-24 00:53:50 -05:00
|
|
|
|
|
|
|
let children = uint::from_str(args[1]).get();
|
2013-01-29 01:54:39 -06:00
|
|
|
let (wait_port, wait_chan) = stream();
|
2012-07-04 14:04:28 -05:00
|
|
|
do task::spawn {
|
2013-01-29 01:54:39 -06:00
|
|
|
calc(children, &wait_chan);
|
2012-02-15 13:52:43 -06:00
|
|
|
};
|
2013-01-29 01:54:39 -06:00
|
|
|
|
|
|
|
let start_chan = wait_port.recv();
|
|
|
|
let (sum_port, sum_chan) = stream::<int>();
|
|
|
|
start_chan.send(sum_chan);
|
|
|
|
let sum = sum_port.recv();
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("How many tasks? %d tasks.", sum);
|
2012-06-25 22:00:46 -05:00
|
|
|
}
|