Skip to content

Commit cdb246f

Browse files
committed
bench: Add threadring shootout benchmark
1 parent e818406 commit cdb246f

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/test/bench/shootout-threadring.rs

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

0 commit comments

Comments
 (0)