Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 73 lines (62 sloc) 1.505 kb
0438e6e @brson bench: Add a test for task concurrency
brson authored
1 // Test for concurrent tasks
2
3 enum msg {
4 ready(comm::chan<msg>),
5 start,
6 done(int),
7 }
8
9 fn calc(children: uint, parent_ch: comm::chan<msg>) {
10 let port = comm::port();
11 let chan = comm::chan(port);
329eca6 @msullivan Make vectors uglier ([]/~). Sorry. Should be temporary. Closes #2725.
msullivan authored
12 let mut child_chs = []/~;
dc07280 @nikomatsakis make --enforce-mut-vars always on, add mut annotations to remaining f…
nikomatsakis authored
13 let mut sum = 0;
0438e6e @brson bench: Add a test for task concurrency
brson authored
14
15 iter::repeat (children) {||
16 task::spawn {||
17 calc(0u, chan);
18 };
19 }
20
21 iter::repeat (children) {||
22 alt check comm::recv(port) {
23 ready(child_ch) {
b19c98e @eholk Some perf fixes, although vec::slice is still too slow (Issue #2719)
eholk authored
24 vec::push(child_chs, child_ch);
0438e6e @brson bench: Add a test for task concurrency
brson authored
25 }
26 }
27 }
28
29 comm::send(parent_ch, ready(chan));
30
31 alt check comm::recv(port) {
32 start {
33 vec::iter (child_chs) { |child_ch|
34 comm::send(child_ch, start);
35 }
36 }
37 }
38
39 iter::repeat (children) {||
40 alt check comm::recv(port) {
41 done(child_sum) { sum += child_sum; }
42 }
43 }
44
45 comm::send(parent_ch, done(sum + 1));
46 }
47
329eca6 @msullivan Make vectors uglier ([]/~). Sorry. Should be temporary. Closes #2725.
msullivan authored
48 fn main(args: [str]/~) {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
49 let args = if os::getenv("RUST_BENCH").is_some() {
50 ["", "100000"]
51 } else if args.len() <= 1u {
52 ["", "100"]
0438e6e @brson bench: Add a test for task concurrency
brson authored
53 } else {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
54 args
0438e6e @brson bench: Add a test for task concurrency
brson authored
55 };
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
56
57 let children = uint::from_str(args[1]).get();
0438e6e @brson bench: Add a test for task concurrency
brson authored
58 let port = comm::port();
59 let chan = comm::chan(port);
60 task::spawn {||
61 calc(children, chan);
62 };
63 alt check comm::recv(port) {
64 ready(chan) {
65 comm::send(chan, start);
66 }
67 }
68 let sum = alt check comm::recv(port) {
69 done(sum) { sum }
70 };
71 #error("How many tasks? %d tasks.", sum);
329eca6 @msullivan Make vectors uglier ([]/~). Sorry. Should be temporary. Closes #2725.
msullivan authored
72 }
Something went wrong with that request. Please try again.