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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * 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/publicdomain/zero/1.0/
  32  */
  33 
  34 /*
  35  * @test
  36  * @bug 4486658
  37  * @summary Checks for missed signals by locking and unlocking each of an array of locks once per thread
  38  * @library /test/lib
  39  */
  40 
  41 import static java.util.concurrent.TimeUnit.MILLISECONDS;
  42 
  43 import java.util.concurrent.CyclicBarrier;
  44 import java.util.concurrent.ExecutorService;
  45 import java.util.concurrent.Executors;
  46 import java.util.concurrent.ThreadLocalRandom;
  47 import java.util.concurrent.locks.ReentrantLock;
  48 import jdk.test.lib.Utils;
  49 
  50 public final class LockOncePerThreadLoops {
  51     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
  52     static final ExecutorService pool = Executors.newCachedThreadPool();
  53     static boolean print = false;
  54     static int nlocks = 20_000;
  55     static int nthreads = 20;
  56     static int replications = 3;
  57 
  58     public static void main(String[] args) throws Exception {
  59         if (args.length > 0)
  60             replications = Integer.parseInt(args[0]);
  61 
  62         if (args.length > 1)
  63             nlocks = Integer.parseInt(args[1]);
  64 
  65         print = true;
  66 
  67         for (int i = 0; i < replications; ++i) {
  68             System.out.print("Iteration: " + i);
  69             new ReentrantLockLoop().test();
  70         }
  71         pool.shutdown();
  72         if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
  73             throw new Error();
  74     }
  75 
  76     static final class ReentrantLockLoop implements Runnable {
  77         private int v = ThreadLocalRandom.current().nextInt();
  78         private volatile int result = 17;
  79         final ReentrantLock[]locks = new ReentrantLock[nlocks];
  80 
  81         private final ReentrantLock lock = new ReentrantLock();
  82         private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
  83         private final CyclicBarrier barrier;
  84         ReentrantLockLoop() {
  85             barrier = new CyclicBarrier(nthreads+1, timer);
  86             for (int i = 0; i < nlocks; ++i)
  87                 locks[i] = new ReentrantLock();
  88         }
  89 
  90         final void test() throws Exception {
  91             for (int i = 0; i < nthreads; ++i)
  92                 pool.execute(this);
  93             barrier.await();
  94             barrier.await();
  95             if (print) {
  96                 long time = timer.getTime();
  97                 double secs = (double)time / 1000000000.0;
  98                 System.out.println("\t " + secs + "s run time");
  99             }
 100 
 101             int r = result;
 102             if (r == 0) // avoid overoptimization
 103                 System.out.println("useless result: " + r);
 104         }
 105 
 106         public final void run() {
 107             try {
 108                 barrier.await();
 109                 int sum = v;
 110                 int x = 0;
 111                 for (int i = 0; i < locks.length; ++i) {
 112                     locks[i].lock();
 113                     try {
 114                         v = x += ~(v - i);
 115                     }
 116                     finally {
 117                         locks[i].unlock();
 118                     }
 119                     // Once in a while, do something more expensive
 120                     if ((~i & 255) == 0) {
 121                         sum += LoopHelpers.compute1(LoopHelpers.compute2(x));
 122                     }
 123                     else
 124                         sum += sum ^ x;
 125                 }
 126                 barrier.await();
 127                 result += sum;
 128             }
 129             catch (Exception ex) {
 130                 return;
 131             }
 132         }
 133     }
 134 
 135 }