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
|
|
|
|
|
|
|
|
enum msg {
|
2012-08-15 16:10:46 -05:00
|
|
|
ready(comm::Chan<msg>),
|
2012-02-15 13:52:43 -06:00
|
|
|
start,
|
|
|
|
done(int),
|
|
|
|
}
|
|
|
|
|
2012-08-15 16:10:46 -05:00
|
|
|
fn calc(children: uint, parent_ch: comm::Chan<msg>) {
|
2012-08-27 16:22:25 -05:00
|
|
|
let port = comm::Port();
|
2012-10-03 16:38:01 -05:00
|
|
|
let chan = comm::Chan(&port);
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut child_chs = ~[];
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut sum = 0;
|
2012-02-15 13:52:43 -06:00
|
|
|
|
2012-07-04 14:04:28 -05:00
|
|
|
for iter::repeat (children) {
|
|
|
|
do task::spawn {
|
2012-02-15 13:52:43 -06:00
|
|
|
calc(0u, chan);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-07-04 14:04:28 -05:00
|
|
|
for iter::repeat (children) {
|
2012-08-23 16:44:58 -05:00
|
|
|
match comm::recv(port) {
|
2012-08-03 21:59:04 -05:00
|
|
|
ready(child_ch) => {
|
2012-09-26 19:33:34 -05:00
|
|
|
child_chs.push(child_ch);
|
2012-02-15 13:52:43 -06:00
|
|
|
}
|
2012-08-23 16:44:58 -05:00
|
|
|
_ => fail ~"task-perf-one-million failed (port not ready)"
|
2012-02-15 13:52:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comm::send(parent_ch, ready(chan));
|
|
|
|
|
2012-08-23 16:44:58 -05:00
|
|
|
match comm::recv(port) {
|
2012-08-03 21:59:04 -05:00
|
|
|
start => {
|
2012-09-18 23:41:37 -05:00
|
|
|
for vec::each(child_chs) |child_ch| {
|
|
|
|
comm::send(*child_ch, start);
|
|
|
|
}
|
2012-02-15 13:52:43 -06:00
|
|
|
}
|
2012-08-23 16:44:58 -05:00
|
|
|
_ => fail ~"task-perf-one-million failed (port not in start state)"
|
2012-02-15 13:52:43 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 14:04:28 -05:00
|
|
|
for iter::repeat (children) {
|
2012-08-23 16:44:58 -05:00
|
|
|
match comm::recv(port) {
|
2012-08-03 21:59:04 -05:00
|
|
|
done(child_sum) => { sum += child_sum; }
|
2012-08-23 16:44:58 -05:00
|
|
|
_ => fail ~"task-perf-one-million failed (port not done)"
|
2012-02-15 13:52:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comm::send(parent_ch, done(sum + 1));
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
~[~"", ~"100000"]
|
2012-05-24 00:53:50 -05:00
|
|
|
} else if args.len() <= 1u {
|
2012-07-14 00:57:48 -05:00
|
|
|
~[~"", ~"100"]
|
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();
|
2012-08-27 16:22:25 -05:00
|
|
|
let port = comm::Port();
|
2012-10-03 16:38:01 -05:00
|
|
|
let chan = comm::Chan(&port);
|
2012-07-04 14:04:28 -05:00
|
|
|
do task::spawn {
|
2012-02-15 13:52:43 -06:00
|
|
|
calc(children, chan);
|
|
|
|
};
|
2012-08-23 16:44:58 -05:00
|
|
|
match comm::recv(port) {
|
2012-08-03 21:59:04 -05:00
|
|
|
ready(chan) => {
|
2012-02-15 13:52:43 -06:00
|
|
|
comm::send(chan, start);
|
|
|
|
}
|
2012-08-23 16:44:58 -05:00
|
|
|
_ => fail ~"task-perf-one-million failed (port not ready)"
|
2012-02-15 13:52:43 -06:00
|
|
|
}
|
2012-08-23 16:44:58 -05:00
|
|
|
let sum = match comm::recv(port) {
|
2012-08-03 21:59:04 -05:00
|
|
|
done(sum) => { sum }
|
2012-08-23 16:44:58 -05:00
|
|
|
_ => fail ~"task-perf-one-million failed (port not done)"
|
2012-02-15 13:52:43 -06:00
|
|
|
};
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("How many tasks? %d tasks.", sum);
|
2012-06-25 22:00:46 -05:00
|
|
|
}
|