2012-02-04 20:00:59 -06:00
|
|
|
// Based on threadring.erlang by Jira Isa
|
|
|
|
use std;
|
|
|
|
|
|
|
|
const n_threads: int = 503;
|
|
|
|
|
|
|
|
fn start(+token: int) {
|
|
|
|
import iter::*;
|
|
|
|
|
|
|
|
let p = comm::port();
|
2012-04-11 23:45:18 -05:00
|
|
|
let mut ch = comm::chan(p);
|
2012-05-26 02:32:08 -05:00
|
|
|
for int::range(2, n_threads + 1) { |i|
|
2012-02-04 20:00:59 -06:00
|
|
|
let id = n_threads + 2 - i;
|
2012-05-25 02:14:40 -05:00
|
|
|
let to_child = task::spawn_listener::<int> {|p, copy ch|
|
2012-02-04 20:00:59 -06:00
|
|
|
roundtrip(id, p, ch)
|
|
|
|
};
|
2012-04-11 23:45:18 -05:00
|
|
|
ch = to_child;
|
|
|
|
}
|
2012-02-04 20:00:59 -06:00
|
|
|
comm::send(ch, token);
|
|
|
|
roundtrip(1, p, ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn roundtrip(id: int, p: comm::port<int>, ch: comm::chan<int>) {
|
|
|
|
while (true) {
|
|
|
|
alt comm::recv(p) {
|
|
|
|
1 {
|
2012-03-12 22:04:27 -05:00
|
|
|
io::println(#fmt("%d\n", id));
|
2012-02-04 20:00:59 -06:00
|
|
|
ret;
|
|
|
|
}
|
|
|
|
token {
|
|
|
|
#debug("%d %d", id, token);
|
|
|
|
comm::send(ch, token - 1);
|
|
|
|
if token <= n_threads {
|
|
|
|
ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-25 22:00:46 -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-28 19:31:05 -05:00
|
|
|
["", "2000000"]/~
|
2012-05-24 00:53:50 -05:00
|
|
|
} else if args.len() <= 1u {
|
2012-06-28 19:31:05 -05:00
|
|
|
["", "1000"]/~
|
2012-05-24 00:53:50 -05:00
|
|
|
} else {
|
|
|
|
args
|
|
|
|
};
|
|
|
|
|
|
|
|
let token = int::from_str(args[1]).get();
|
2012-02-04 20:00:59 -06:00
|
|
|
|
|
|
|
start(token);
|
2012-05-24 16:49:39 -05:00
|
|
|
}
|