/
task-perf-one-million.rs
72 lines (62 loc) · 1.47 KB
/
task-perf-one-million.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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);
let mut child_chs = ~[];
let mut sum = 0;
iter::repeat (children) {||
task::spawn {||
calc(0u, chan);
};
}
iter::repeat (children) {||
alt check comm::recv(port) {
ready(child_ch) {
vec::push(child_chs, child_ch);
}
}
}
comm::send(parent_ch, ready(chan));
alt check comm::recv(port) {
start {
vec::iter (child_chs) { |child_ch|
comm::send(child_ch, start);
}
}
}
iter::repeat (children) {||
alt check comm::recv(port) {
done(child_sum) { sum += child_sum; }
}
}
comm::send(parent_ch, done(sum + 1));
}
fn main(args: ~[str]) {
let args = if os::getenv("RUST_BENCH").is_some() {
~["", "100000"]
} else if args.len() <= 1u {
~["", "100"]
} else {
args
};
let children = uint::from_str(args[1]).get();
let port = comm::port();
let chan = comm::chan(port);
task::spawn {||
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);
}