Skip to content

Commit b4e221b

Browse files
committed
bench: Add threadring shootout benchmark
1 parent 5d4b372 commit b4e221b

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/test/bench/shootout-threadring.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Based on threadring.erlang by Jira Isa
2+
use std;
3+
4+
const n_threads: int = 503;
5+
6+
fn start(+token: int) {
7+
import iter::*;
8+
9+
let p = comm::port();
10+
let ch = iter::foldl(bind int::range(2, n_threads + 1, _),
11+
comm::chan(p)) { |ch, i|
12+
// FIXME: Some twiddling because we don't have a standard
13+
// reverse range function yet
14+
let id = n_threads + 2 - i;
15+
let {to_child, _} = task::spawn_connected::<int, int> {|p, _ch|
16+
roundtrip(id, p, ch)
17+
};
18+
to_child
19+
};
20+
comm::send(ch, token);
21+
roundtrip(1, p, ch);
22+
}
23+
24+
fn roundtrip(id: int, p: comm::port<int>, ch: comm::chan<int>) {
25+
while (true) {
26+
alt comm::recv(p) {
27+
1 {
28+
std::io::println(#fmt("%d\n", id));
29+
ret;
30+
}
31+
token {
32+
#debug("%d %d", id, token);
33+
comm::send(ch, token - 1);
34+
if token <= n_threads {
35+
ret;
36+
}
37+
}
38+
}
39+
}
40+
}
41+
42+
fn main(args: [str]) {
43+
let token = if vec::len(args) < 2u {
44+
1000
45+
} else {
46+
int::from_str(args[1])
47+
};
48+
49+
start(token);
50+
}

0 commit comments

Comments
 (0)