2012-02-15 13:52:43 -06:00
|
|
|
// Test for concurrent tasks
|
|
|
|
|
|
|
|
enum msg {
|
|
|
|
ready(comm::chan<msg>),
|
|
|
|
start,
|
|
|
|
done(int),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn calc(children: uint, parent_ch: comm::chan<msg>) {
|
|
|
|
let port = comm::port();
|
|
|
|
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-06-26 15:55:56 -05:00
|
|
|
do iter::repeat (children) {||
|
|
|
|
do task::spawn {||
|
2012-02-15 13:52:43 -06:00
|
|
|
calc(0u, chan);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-06-26 15:55:56 -05:00
|
|
|
do iter::repeat (children) {||
|
2012-02-15 13:52:43 -06:00
|
|
|
alt check comm::recv(port) {
|
|
|
|
ready(child_ch) {
|
2012-06-25 18:22:22 -05:00
|
|
|
vec::push(child_chs, child_ch);
|
2012-02-15 13:52:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comm::send(parent_ch, ready(chan));
|
|
|
|
|
|
|
|
alt check comm::recv(port) {
|
|
|
|
start {
|
2012-06-26 15:55:56 -05:00
|
|
|
do vec::iter (child_chs) { |child_ch|
|
2012-02-15 13:52:43 -06:00
|
|
|
comm::send(child_ch, start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 15:55:56 -05:00
|
|
|
do iter::repeat (children) {||
|
2012-02-15 13:52:43 -06:00
|
|
|
alt check comm::recv(port) {
|
|
|
|
done(child_sum) { sum += child_sum; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comm::send(parent_ch, done(sum + 1));
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn main(args: ~[str]) {
|
2012-05-24 00:53:50 -05:00
|
|
|
let args = if os::getenv("RUST_BENCH").is_some() {
|
2012-06-29 18:26:56 -05:00
|
|
|
~["", "100000"]
|
2012-05-24 00:53:50 -05:00
|
|
|
} else if args.len() <= 1u {
|
2012-06-29 18:26:56 -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-02-15 13:52:43 -06:00
|
|
|
let port = comm::port();
|
|
|
|
let chan = comm::chan(port);
|
2012-06-26 15:55:56 -05:00
|
|
|
do task::spawn {||
|
2012-02-15 13:52:43 -06:00
|
|
|
calc(children, chan);
|
|
|
|
};
|
|
|
|
alt check comm::recv(port) {
|
|
|
|
ready(chan) {
|
|
|
|
comm::send(chan, start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let sum = alt check comm::recv(port) {
|
|
|
|
done(sum) { sum }
|
|
|
|
};
|
|
|
|
#error("How many tasks? %d tasks.", sum);
|
2012-06-25 22:00:46 -05:00
|
|
|
}
|