]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // The Computer Language Benchmarks Game |
2 | // http://benchmarksgame.alioth.debian.org/ | |
223e47cc | 3 | // |
1a4d82fc | 4 | // contributed by the Rust Project Developers |
223e47cc | 5 | |
1a4d82fc JJ |
6 | // Copyright (c) 2012-2014 The Rust Project Developers |
7 | // | |
8 | // All rights reserved. | |
9 | // | |
10 | // Redistribution and use in source and binary forms, with or without | |
11 | // modification, are permitted provided that the following conditions | |
12 | // are met: | |
13 | // | |
14 | // - Redistributions of source code must retain the above copyright | |
15 | // notice, this list of conditions and the following disclaimer. | |
16 | // | |
17 | // - Redistributions in binary form must reproduce the above copyright | |
18 | // notice, this list of conditions and the following disclaimer in | |
19 | // the documentation and/or other materials provided with the | |
20 | // distribution. | |
21 | // | |
22 | // - Neither the name of "The Computer Language Benchmarks Game" nor | |
23 | // the name of "The Computer Language Shootout Benchmarks" nor the | |
24 | // names of its contributors may be used to endorse or promote | |
25 | // products derived from this software without specific prior | |
26 | // written permission. | |
27 | // | |
28 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
29 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
30 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
31 | // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
32 | // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
33 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
34 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
35 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
36 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
37 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
38 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
39 | // OF THE POSSIBILITY OF SUCH DAMAGE. | |
223e47cc | 40 | |
1a4d82fc | 41 | use std::sync::mpsc::{channel, Sender, Receiver}; |
85aaf69f | 42 | use std::thread; |
970d7e83 | 43 | |
85aaf69f | 44 | fn start(n_tasks: i32, token: i32) { |
1a4d82fc | 45 | let (tx, mut rx) = channel(); |
85aaf69f SL |
46 | tx.send(token).unwrap(); |
47 | let mut guards = Vec::with_capacity(n_tasks as usize); | |
48 | for i in 2 .. n_tasks + 1 { | |
1a4d82fc | 49 | let (tx, next_rx) = channel(); |
85aaf69f SL |
50 | let cur_rx = std::mem::replace(&mut rx, next_rx); |
51 | guards.push(thread::spawn(move|| roundtrip(i, tx, cur_rx))); | |
223e47cc | 52 | } |
85aaf69f | 53 | let guard = thread::spawn(move|| roundtrip(1, tx, rx)); |
223e47cc LB |
54 | } |
55 | ||
85aaf69f | 56 | fn roundtrip(id: i32, tx: Sender<i32>, rx: Receiver<i32>) { |
1a4d82fc JJ |
57 | for token in rx.iter() { |
58 | if token == 1 { | |
59 | println!("{}", id); | |
60 | break; | |
223e47cc | 61 | } |
85aaf69f | 62 | tx.send(token - 1).unwrap(); |
223e47cc LB |
63 | } |
64 | } | |
65 | ||
66 | fn main() { | |
85aaf69f SL |
67 | let mut args = std::env::args(); |
68 | let token = if std::env::var_os("RUST_BENCH").is_some() { | |
1a4d82fc JJ |
69 | 2000000 |
70 | } else { | |
85aaf69f | 71 | args.nth(1).and_then(|arg| arg.parse().ok()).unwrap_or(1000) |
223e47cc | 72 | }; |
85aaf69f SL |
73 | let n_tasks = args.next() |
74 | .and_then(|arg| arg.parse().ok()) | |
1a4d82fc | 75 | .unwrap_or(503); |
223e47cc | 76 | |
1a4d82fc | 77 | start(n_tasks, token); |
223e47cc | 78 | } |