2014-04-21 16:05:57 +02:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2013-03-13 19:09:28 -05:00
|
|
|
// 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.
|
|
|
|
|
2014-04-21 16:05:57 +02:00
|
|
|
#![feature(phase)]
|
|
|
|
#[phase(syntax)] extern crate green;
|
|
|
|
green_start!(main)
|
2013-04-20 23:47:35 -07:00
|
|
|
|
2013-03-13 19:09:28 -05:00
|
|
|
fn start(n_tasks: int, token: int) {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, mut rx) = channel();
|
|
|
|
tx.send(token);
|
2014-04-21 16:05:57 +02:00
|
|
|
for i in range(2, n_tasks + 1) {
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, next_rx) = channel();
|
2014-04-21 16:05:57 +02:00
|
|
|
spawn(proc() roundtrip(i, tx, rx));
|
2014-03-09 14:58:32 -07:00
|
|
|
rx = next_rx;
|
2013-03-13 19:09:28 -05:00
|
|
|
}
|
2014-04-21 16:05:57 +02:00
|
|
|
spawn(proc() roundtrip(1, tx, rx));
|
2013-03-13 19:09:28 -05:00
|
|
|
}
|
|
|
|
|
2014-04-21 16:05:57 +02:00
|
|
|
fn roundtrip(id: int, tx: Sender<int>, rx: Receiver<int>) {
|
|
|
|
for token in rx.iter() {
|
|
|
|
if token == 1 {
|
|
|
|
println!("{}", id);
|
|
|
|
break;
|
2013-03-13 19:09:28 -05:00
|
|
|
}
|
2014-04-21 16:05:57 +02:00
|
|
|
tx.send(token - 1);
|
2013-03-13 19:09:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2014-04-21 16:05:57 +02:00
|
|
|
let args = std::os::args();
|
|
|
|
let token = if std::os::getenv("RUST_BENCH").is_some() {
|
|
|
|
2000000
|
2014-03-05 15:28:08 -08:00
|
|
|
} else {
|
2014-04-21 16:05:57 +02:00
|
|
|
args.get(1).and_then(|arg| from_str(*arg)).unwrap_or(1000)
|
2013-03-13 19:09:28 -05:00
|
|
|
};
|
2014-04-21 16:05:57 +02:00
|
|
|
let n_tasks = args.get(2).and_then(|arg| from_str(*arg)).unwrap_or(503);
|
2013-03-13 19:09:28 -05:00
|
|
|
|
2014-04-21 16:05:57 +02:00
|
|
|
start(n_tasks, token);
|
2013-03-13 19:09:28 -05:00
|
|
|
}
|