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/licenses/publicdomain
  32  */
  33 
  34 /*
  35  * @test
  36  * @bug 4486658 5031862


  37  * @summary Checks for responsiveness of locks to timeouts.
  38  * Runs under the assumption that ITERS computations require more than
  39  * TIMEOUT msecs to complete, which seems to be a safe assumption for
  40  * another decade.
  41  */
  42 
  43 import java.util.concurrent.*;
  44 import java.util.concurrent.locks.*;
  45 import java.util.*;
  46 
  47 public final class TimeoutLockLoops {
  48     static final ExecutorService pool = Executors.newCachedThreadPool();
  49     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
  50     static boolean print = false;
  51     static final int ITERS = Integer.MAX_VALUE;
  52     static final long TIMEOUT = 100;
  53 
  54     public static void main(String[] args) throws Exception {
  55         int maxThreads = 100;
  56         if (args.length > 0)
  57             maxThreads = Integer.parseInt(args[0]);
  58 
  59 
  60         print = true;
  61 
  62         for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
  63             System.out.print("Threads: " + i);
  64             new ReentrantLockLoop(i).test();
  65             Thread.sleep(10);
  66         }
  67         pool.shutdown();
  68         if (! pool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS))
  69             throw new Error();
  70     }
  71 
  72     static final class ReentrantLockLoop implements Runnable {
  73         private int v = rng.next();
  74         private volatile boolean completed;
  75         private volatile int result = 17;
  76         private final ReentrantLock lock = new ReentrantLock();
  77         private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
  78         private final CyclicBarrier barrier;
  79         private final int nthreads;
  80         ReentrantLockLoop(int nthreads) {
  81             this.nthreads = nthreads;
  82             barrier = new CyclicBarrier(nthreads+1, timer);
  83         }
  84 
  85         final void test() throws Exception {
  86             for (int i = 0; i < nthreads; ++i) {
  87                 lock.lock();
  88                 pool.execute(this);
  89                 lock.unlock();
  90             }
  91             barrier.await();
  92             Thread.sleep(TIMEOUT);
  93             while (!lock.tryLock()); // Jam lock
  94             //            lock.lock();
  95             barrier.await();
  96             if (print) {
  97                 long time = timer.getTime();
  98                 double secs = (double)(time) / 1000000000.0;
  99                 System.out.println("\t " + secs + "s run time");
 100             }
 101 
 102             if (completed)
 103                 throw new Error("Some thread completed instead of timing out");
 104             int r = result;
 105             if (r == 0) // avoid overoptimization
 106                 System.out.println("useless result: " + r);
 107         }
 108 
 109         public final void run() {
 110             try {
 111                 barrier.await();
 112                 int sum = v;
 113                 int x = 17;
 114                 int n = ITERS;
 115                 final ReentrantLock lock = this.lock;
 116                 for (;;) {
 117                     if (x != 0) {
 118                         if (n-- <= 0)
 119                             break;
 120                     }
 121                     if (!lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS))
 122                         break;
 123                     try {
 124                         v = x = LoopHelpers.compute1(v);
 125                     }
 126                     finally {
 127                         lock.unlock();
 128                     }
 129                     sum += LoopHelpers.compute2(x);
 130                 }
 131                 if (n <= 0)
 132                     completed = true;
 133                 barrier.await();
 134                 result += sum;
 135             }
 136             catch (Exception ex) {
 137                 ex.printStackTrace();
 138                 return;
 139             }
 140         }
 141     }
 142 
 143 
 144 }
--- EOF ---