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 8031651
  37  * @compile LockOncePerThreadLoops.java
  38  * @run main/timeout=15000 LockOncePerThreadLoops
  39  * @summary Checks for missed signals by locking and unlocking each of an array of locks once per thread
  40  */
  41 
  42 import java.util.concurrent.*;
  43 import java.util.concurrent.locks.*;
  44 import java.util.*;
  45 
  46 public final class LockOncePerThreadLoops {
  47     static final ExecutorService pool = Executors.newCachedThreadPool();
  48     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
  49     static boolean print = false;
  50     static int nlocks = 50000;
  51     static int nthreads = 100;
  52     static int replications = 5;
  53 
  54     public static void main(String[] args) throws Exception {
  55         if (args.length > 0)
  56             replications = Integer.parseInt(args[0]);
  57 
  58         if (args.length > 1)
  59             nlocks = Integer.parseInt(args[1]);
  60 
  61         print = true;
  62 
  63         for (int i = 0; i < replications; ++i) {
  64             System.out.print("Iteration: " + i);
  65             new ReentrantLockLoop().test();
  66             Thread.sleep(100);
  67         }
  68         pool.shutdown();
  69         if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
  70             throw new Error();
  71     }
  72 
  73     static final class ReentrantLockLoop implements Runnable {
  74         private int v = rng.next();
  75         private volatile int result = 17;
  76         final ReentrantLock[]locks = new ReentrantLock[nlocks];
  77 
  78         private final ReentrantLock lock = new ReentrantLock();
  79         private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
  80         private final CyclicBarrier barrier;
  81         ReentrantLockLoop() {
  82             barrier = new CyclicBarrier(nthreads+1, timer);
  83             for (int i = 0; i < nlocks; ++i)
  84                 locks[i] = new ReentrantLock();
  85         }
  86 
  87         final void test() throws Exception {
  88             for (int i = 0; i < nthreads; ++i)
  89                 pool.execute(this);
  90             barrier.await();
  91             barrier.await();
  92             if (print) {
  93                 long time = timer.getTime();
  94                 double secs = (double)(time) / 1000000000.0;
  95                 System.out.println("\t " + secs + "s run time");
  96             }
  97 
  98             int r = result;
  99             if (r == 0) // avoid overoptimization
 100                 System.out.println("useless result: " + r);
 101         }
 102 
 103         public final void run() {
 104             try {
 105                 barrier.await();
 106                 int sum = v;
 107                 int x = 0;
 108                 for (int i = 0; i < locks.length; ++i) {
 109                     locks[i].lock();
 110                     try {
 111                             v = x += ~(v - i);
 112                     }
 113                     finally {
 114                         locks[i].unlock();
 115                     }
 116                     // Once in a while, do something more expensive
 117                     if ((~i & 255) == 0) {
 118                         sum += LoopHelpers.compute1(LoopHelpers.compute2(x));
 119                     }
 120                     else
 121                         sum += sum ^ x;
 122                 }
 123                 barrier.await();
 124                 result += sum;
 125             }
 126             catch (Exception ie) {
 127                 return;
 128             }
 129         }
 130     }
 131 
 132 }