2013-03-13 19:09:28 -05:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Based on threadring.erlang by Jira Isa
|
|
|
|
|
2013-05-17 19:46:01 -05:00
|
|
|
use std::os;
|
2013-04-21 01:47:35 -05:00
|
|
|
|
2013-03-13 19:09:28 -05:00
|
|
|
fn start(n_tasks: int, token: int) {
|
2013-12-15 20:17:43 -06:00
|
|
|
let (p, ch1) = Chan::new();
|
2013-06-28 07:05:11 -05:00
|
|
|
let mut p = p;
|
2013-07-17 14:31:20 -05:00
|
|
|
let ch1 = ch1;
|
2013-03-13 19:09:28 -05:00
|
|
|
ch1.send(token);
|
|
|
|
// XXX could not get this to work with a range closure
|
|
|
|
let mut i = 2;
|
|
|
|
while i <= n_tasks {
|
2013-12-15 20:17:43 -06:00
|
|
|
let (next_p, ch) = Chan::new();
|
2013-03-13 19:09:28 -05:00
|
|
|
let imm_i = i;
|
|
|
|
let imm_p = p;
|
2014-01-27 17:29:50 -06:00
|
|
|
spawn(proc() {
|
2013-03-13 19:09:28 -05:00
|
|
|
roundtrip(imm_i, n_tasks, &imm_p, &ch);
|
2014-01-27 17:29:50 -06:00
|
|
|
});
|
2013-03-13 19:09:28 -05:00
|
|
|
p = next_p;
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
let imm_p = p;
|
|
|
|
let imm_ch = ch1;
|
2014-01-27 17:29:50 -06:00
|
|
|
spawn(proc() {
|
2013-03-13 19:09:28 -05:00
|
|
|
roundtrip(1, n_tasks, &imm_p, &imm_ch);
|
2014-01-27 17:29:50 -06:00
|
|
|
});
|
2013-03-13 19:09:28 -05:00
|
|
|
}
|
|
|
|
|
2013-05-17 19:46:01 -05:00
|
|
|
fn roundtrip(id: int, n_tasks: int, p: &Port<int>, ch: &Chan<int>) {
|
2014-01-19 02:21:14 -06:00
|
|
|
loop {
|
2013-03-13 19:09:28 -05:00
|
|
|
match p.recv() {
|
|
|
|
1 => {
|
2013-09-25 00:16:43 -05:00
|
|
|
println!("{}\n", id);
|
2013-03-13 19:09:28 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
token => {
|
2013-10-21 15:08:31 -05:00
|
|
|
info!("thread: {} got token: {}", id, token);
|
2013-03-13 19:09:28 -05:00
|
|
|
ch.send(token - 1);
|
|
|
|
if token <= n_tasks {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2013-07-17 14:31:20 -05:00
|
|
|
let args = if os::getenv("RUST_BENCH").is_some() {
|
2013-03-13 19:09:28 -05:00
|
|
|
~[~"", ~"2000000", ~"503"]
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
os::args()
|
|
|
|
};
|
|
|
|
let token = if args.len() > 1u {
|
2013-08-03 18:59:24 -05:00
|
|
|
FromStr::from_str(args[1]).unwrap()
|
2013-03-13 19:09:28 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
1000
|
|
|
|
};
|
|
|
|
let n_tasks = if args.len() > 2u {
|
2013-08-03 18:59:24 -05:00
|
|
|
FromStr::from_str(args[2]).unwrap()
|
2013-03-13 19:09:28 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
503
|
|
|
|
};
|
|
|
|
start(n_tasks, token);
|
|
|
|
|
|
|
|
}
|