8140468: java/util/concurrent/locks/ReentrantLock/CancelledLockLoops.java failed with java.lang.Error: Completed == 4; expected 2

8040928: java/util/concurrent/locks/ReentrantLock/CancelledLockLoops.java: java.lang.Error: Completed != 2

Reviewed-by: martin, psandoz, chegar, shade, plevart
This commit is contained in:
Doug Lea 2015-11-25 18:35:27 -08:00
parent 22fa6c1f25
commit 247c08f85d

View File

@ -33,51 +33,45 @@
/* /*
* @test * @test
* @bug 4486658 * @bug 4486658 8040928 8140468
* @run main/timeout=2800 CancelledLockLoops
* @summary tests ReentrantLock.lockInterruptibly. * @summary tests ReentrantLock.lockInterruptibly.
* Checks for responsiveness of locks to interrupts. Runs under the * Checks for responsiveness of locks to interrupts.
* assumption that ITERS computations require more than TIMEOUT msecs
* to complete.
*/ */
import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.NANOSECONDS;
import java.util.concurrent.locks.*;
import java.util.*; import java.util.SplittableRandom;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.locks.ReentrantLock;
public final class CancelledLockLoops { public final class CancelledLockLoops {
static final Random rng = new Random(); static final SplittableRandom rnd = new SplittableRandom();
static boolean print = false;
static final int ITERS = 1000000;
static final long TIMEOUT = 100;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
int maxThreads = (args.length > 0) ? Integer.parseInt(args[0]) : 5; final int maxThreads = (args.length > 0) ? Integer.parseInt(args[0]) : 5;
final int reps = 1; // increase for stress testing
print = true; for (int j = 0; j < reps; j++) {
for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) { new Loops(i).test();
System.out.print("Threads: " + i);
try {
new ReentrantLockLoop(i).test();
} }
catch (BrokenBarrierException bb) {
// OK, ignore
}
Thread.sleep(TIMEOUT);
} }
} }
static final class ReentrantLockLoop implements Runnable { static final class Loops implements Runnable {
private int v = rng.nextInt(); private final boolean print = false;
private int completed; private volatile boolean done = false;
private int v = rnd.nextInt();
private int completed = 0;
private volatile int result = 17; private volatile int result = 17;
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
private final CyclicBarrier barrier; private final CyclicBarrier barrier;
private final int nthreads; private final int nthreads;
ReentrantLockLoop(int nthreads) { private volatile Throwable fail = null;
Loops(int nthreads) {
this.nthreads = nthreads; this.nthreads = nthreads;
if (print) System.out.print("Threads: " + nthreads);
barrier = new CyclicBarrier(nthreads+1, timer); barrier = new CyclicBarrier(nthreads+1, timer);
} }
@ -88,15 +82,15 @@ public final class CancelledLockLoops {
for (int i = 0; i < threads.length; ++i) for (int i = 0; i < threads.length; ++i)
threads[i].start(); threads[i].start();
Thread[] cancels = threads.clone(); Thread[] cancels = threads.clone();
Collections.shuffle(Arrays.asList(cancels), rng);
barrier.await(); barrier.await();
Thread.sleep(TIMEOUT); Thread.sleep(rnd.nextInt(5));
for (int i = 0; i < cancels.length-2; ++i) { for (int i = 0; i < cancels.length-2; ++i) {
cancels[i].interrupt(); cancels[i].interrupt();
// make sure all OK even when cancellations spaced out // make sure all OK even when cancellations spaced out
if ( (i & 3) == 0) if ( (i & 3) == 0)
Thread.sleep(1 + rng.nextInt(10)); Thread.sleep(1 + rnd.nextInt(5));
} }
done = true;
barrier.await(); barrier.await();
if (print) { if (print) {
long time = timer.getTime(); long time = timer.getTime();
@ -117,20 +111,25 @@ public final class CancelledLockLoops {
int r = result; int r = result;
if (r == 0) // avoid overoptimization if (r == 0) // avoid overoptimization
System.out.println("useless result: " + r); System.out.println("useless result: " + r);
if (fail != null) throw new RuntimeException(fail);
} }
public final void run() { public final void run() {
try { try {
barrier.await(); barrier.await();
boolean interrupted = false;
long startTime = System.nanoTime();
int sum = v; int sum = v;
int x = 0; int x = 0;
int n = ITERS; while (!done || Thread.currentThread().isInterrupted()) {
boolean done = false;
do {
try { try {
lock.lockInterruptibly(); lock.lockInterruptibly();
} }
catch (InterruptedException ie) { catch (InterruptedException ie) {
interrupted = true;
if (print)
System.out.printf("interrupted after %d millis%n",
NANOSECONDS.toMillis(System.nanoTime() - startTime));
break; break;
} }
try { try {
@ -140,8 +139,11 @@ public final class CancelledLockLoops {
lock.unlock(); lock.unlock();
} }
sum += LoopHelpers.compute2(x); sum += LoopHelpers.compute2(x);
} while (n-- > 0); }
if (n <= 0) { if (!interrupted) {
if (print)
System.out.printf("completed after %d millis%n",
NANOSECONDS.toMillis(System.nanoTime() - startTime));
lock.lock(); lock.lock();
try { try {
++completed; ++completed;
@ -153,9 +155,9 @@ public final class CancelledLockLoops {
barrier.await(); barrier.await();
result += sum; result += sum;
} }
catch (Exception ex) { catch (Throwable ex) {
ex.printStackTrace(); fail = ex;
return; throw new RuntimeException(ex);
} }
} }
} }