1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  19  * CA 95054 USA or visit www.sun.com if you need additional information or
  20  * have any questions.
  21  */
  22 
  23 /*
  24  * This file is available under and governed by the GNU General Public
  25  * License version 2 only, as published by the Free Software Foundation.
  26  * However, the following notice accompanied the original version of this
  27  * file:
  28  *
  29  * Written by Doug Lea with assistance from members of JCP JSR-166
  30  * Expert Group and released to the public domain, as explained at
  31  * http://creativecommons.org/licenses/publicdomain
  32  */
  33 
  34 /*
  35  * @test
  36  * @bug 4486658
  37  * @compile -source 1.5 ExchangeLoops.java
  38  * @run main/timeout=720 ExchangeLoops
  39  * @summary checks to make sure a pipeline of exchangers passes data.
  40  */
  41 
  42 import java.util.concurrent.*;
  43 
  44 public class ExchangeLoops {
  45     static final ExecutorService pool = Executors.newCachedThreadPool();
  46     static boolean print = false;
  47 
  48     static class Int {
  49         public int value;
  50         Int(int i) { value = i; }
  51     }
  52 
  53 
  54     public static void main(String[] args) throws Exception {
  55         int maxStages = 5;
  56         int iters = 10000;
  57 
  58         if (args.length > 0)
  59             maxStages = Integer.parseInt(args[0]);
  60 
  61         print = false;
  62         System.out.println("Warmup...");
  63         oneRun(2, 100000);
  64         print = true;
  65 
  66         for (int i = 2; i <= maxStages; i += (i+1) >>> 1) {
  67             System.out.print("Threads: " + i + "\t: ");
  68             oneRun(i, iters);
  69         }
  70         pool.shutdown();
  71         if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
  72             throw new Error();
  73    }
  74 
  75     static class Stage implements Runnable {
  76         final int iters;
  77         final Exchanger<Int> left;
  78         final Exchanger<Int> right;
  79         final CyclicBarrier barrier;
  80         volatile int result;
  81         Stage (Exchanger<Int> left,
  82                Exchanger<Int> right,
  83                CyclicBarrier b, int iters) {
  84             this.left = left;
  85             this.right = right;
  86             barrier = b;
  87             this.iters = iters;
  88         }
  89 
  90         public void run() {
  91             try {
  92                 barrier.await();
  93                 Int item = new Int(hashCode());
  94                 for (int i = 0; i < iters; ++i) {
  95                     if (left != null) {
  96                         item.value = LoopHelpers.compute1(item.value);
  97                         Int other = left.exchange(item);
  98                         if (other == item || other == null)
  99                             throw new Error("Failed Exchange");
 100                         item = other;
 101 
 102                     }
 103                     if (right != null) {
 104                         item.value = LoopHelpers.compute2(item.value);
 105                         Int other = right.exchange(item);
 106                         if (other == item || other == null)
 107                             throw new Error("Failed Exchange");
 108                         item = other;
 109                     }
 110                 }
 111                 barrier.await();
 112 
 113             }
 114             catch (Exception ie) {
 115                 ie.printStackTrace();
 116                 return;
 117             }
 118         }
 119     }
 120 
 121     static void oneRun(int nthreads, int iters) throws Exception {
 122         LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
 123         CyclicBarrier barrier = new CyclicBarrier(nthreads + 1, timer);
 124         Exchanger<Int> l = null;
 125         Exchanger<Int> r = new Exchanger<Int>();
 126         for (int i = 0; i < nthreads; ++i) {
 127             pool.execute(new Stage(l, r, barrier, iters));
 128             l = r;
 129             r = (i+2 < nthreads) ? new Exchanger<Int>() : null;
 130         }
 131         barrier.await();
 132         barrier.await();
 133         long time = timer.getTime();
 134         if (print)
 135             System.out.println(LoopHelpers.rightJustify(time / (iters * nthreads + iters * (nthreads-2))) + " ns per transfer");
 136     }
 137 
 138 }