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