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 CancelledLockLoops.java 38 * @run main/timeout=2800 CancelledLockLoops 39 * @summary tests lockInterruptibly. 40 * Checks for responsiveness of locks to interrupts. Runs under that 41 * assumption that ITERS_VALUE computations require more than TIMEOUT 42 * msecs to complete. 43 */ 44 45 import java.util.concurrent.*; 46 import java.util.concurrent.locks.*; 47 import java.util.*; 48 49 public final class CancelledLockLoops { 50 static final Random rng = new Random(); 51 static boolean print = false; 52 static final int ITERS = 5000000; 53 static final long TIMEOUT = 100; 54 55 public static void main(String[] args) throws Exception { 56 int maxThreads = (args.length > 0) ? Integer.parseInt(args[0]) : 5; 57 print = true; 58 59 for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) { 60 System.out.print("Threads: " + i); 61 try { 62 new ReentrantLockLoop(i).test(); 63 } 64 catch (BrokenBarrierException bb) { 65 // OK, ignore 66 } 67 Thread.sleep(TIMEOUT); 68 } 69 } 70 71 static final class ReentrantLockLoop implements Runnable { 72 private int v = rng.nextInt(); 73 private int completed; 74 private volatile int result = 17; 75 private final ReentrantLock lock = new ReentrantLock(); 76 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); 77 private final CyclicBarrier barrier; 78 private final int nthreads; 79 ReentrantLockLoop(int nthreads) { 80 this.nthreads = nthreads; 81 barrier = new CyclicBarrier(nthreads+1, timer); 82 } 83 84 final void test() throws Exception { 85 Thread[] threads = new Thread[nthreads]; 86 for (int i = 0; i < threads.length; ++i) 87 threads[i] = new Thread(this); 88 for (int i = 0; i < threads.length; ++i) 89 threads[i].start(); 90 Thread[] cancels = threads.clone(); 91 Collections.shuffle(Arrays.asList(cancels), rng); 92 barrier.await(); 93 Thread.sleep(TIMEOUT); 94 for (int i = 0; i < cancels.length-2; ++i) { 95 cancels[i].interrupt(); 96 // make sure all OK even when cancellations spaced out 97 if ( (i & 3) == 0) 98 Thread.sleep(1 + rng.nextInt(10)); 99 } 100 barrier.await(); 101 if (print) { 102 long time = timer.getTime(); 103 double secs = (double)(time) / 1000000000.0; 104 System.out.println("\t " + secs + "s run time"); 105 } 106 107 int c; 108 lock.lock(); 109 try { 110 c = completed; 111 } 112 finally { 113 lock.unlock(); 114 } 115 if (c != 2) 116 throw new Error("Completed != 2"); 117 int r = result; 118 if (r == 0) // avoid overoptimization 119 System.out.println("useless result: " + r); 120 } 121 122 public final void run() { 123 try { 124 barrier.await(); 125 int sum = v; 126 int x = 0; 127 int n = ITERS; 128 boolean done = false; 129 do { 130 try { 131 lock.lockInterruptibly(); 132 } 133 catch (InterruptedException ie) { 134 break; 135 } 136 try { 137 v = x = LoopHelpers.compute1(v); 138 } 139 finally { 140 lock.unlock(); 141 } 142 sum += LoopHelpers.compute2(x); 143 } while (n-- > 0); 144 if (n <= 0) { 145 lock.lock(); 146 try { 147 ++completed; 148 } 149 finally { 150 lock.unlock(); 151 } 152 } 153 barrier.await(); 154 result += sum; 155 } 156 catch (Exception ex) { 157 ex.printStackTrace(); 158 return; 159 } 160 } 161 } 162 163 }