0N/A/*
2362N/A * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
39N/A * @bug 6207928 6328220 6378321 6625723
0N/A * @summary Recursive lock invariant sanity checks
0N/A * @author Martin Buchholz
0N/A */
0N/A
39N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport java.util.concurrent.locks.*;
0N/A
0N/A// I am the Cownt, and I lahve to cownt.
0N/Apublic class Count {
39N/A final Random rnd = new Random();
39N/A
39N/A void lock(Lock lock) {
39N/A try {
39N/A switch (rnd.nextInt(4)) {
39N/A case 0: lock.lock(); break;
39N/A case 1: lock.lockInterruptibly(); break;
39N/A case 2: check(lock.tryLock()); break;
39N/A case 3: check(lock.tryLock(45, TimeUnit.MINUTES)); break;
39N/A }
39N/A } catch (Throwable t) { unexpected(t); }
39N/A }
39N/A
39N/A void test(String[] args) throws Throwable {
39N/A for (boolean fair : new boolean[] { true, false })
39N/A for (boolean serialClone : new boolean[] { true, false }) {
39N/A testReentrantLocks(fair, serialClone);
39N/A testConcurrentReadLocks(fair, serialClone);
39N/A }
39N/A }
39N/A
39N/A void testConcurrentReadLocks(final boolean fair,
39N/A final boolean serialClone) throws Throwable {
39N/A final int nThreads = 10;
39N/A final CyclicBarrier barrier = new CyclicBarrier(nThreads);
39N/A final ExecutorService es = Executors.newFixedThreadPool(nThreads);
39N/A final ReentrantReadWriteLock rwl = serialClone ?
39N/A serialClone(new ReentrantReadWriteLock(fair)) :
39N/A new ReentrantReadWriteLock(fair);
39N/A for (int i = 0; i < nThreads; i++) {
39N/A es.submit(new Runnable() { public void run() {
39N/A try {
39N/A int n = 5;
39N/A for (int i = 0; i < n; i++) {
39N/A barrier.await();
39N/A equal(rwl.getReadHoldCount(), i);
39N/A equal(rwl.getWriteHoldCount(), 0);
39N/A check(! rwl.isWriteLocked());
39N/A equal(rwl.getReadLockCount(), nThreads * i);
39N/A barrier.await();
39N/A lock(rwl.readLock());
39N/A }
39N/A for (int i = 0; i < n; i++) {
39N/A rwl.readLock().unlock();
39N/A barrier.await();
39N/A equal(rwl.getReadHoldCount(), n-i-1);
39N/A equal(rwl.getReadLockCount(), nThreads*(n-i-1));
39N/A equal(rwl.getWriteHoldCount(), 0);
39N/A check(! rwl.isWriteLocked());
39N/A barrier.await();
39N/A }
39N/A THROWS(IllegalMonitorStateException.class,
39N/A new F(){void f(){rwl.readLock().unlock();}},
39N/A new F(){void f(){rwl.writeLock().unlock();}});
39N/A barrier.await();
39N/A } catch (Throwable t) { unexpected(t); }}});}
39N/A es.shutdown();
39N/A check(es.awaitTermination(10, TimeUnit.SECONDS));
39N/A }
39N/A
39N/A void testReentrantLocks(final boolean fair,
39N/A final boolean serialClone) throws Throwable {
39N/A final ReentrantLock rl = serialClone ?
39N/A serialClone(new ReentrantLock(fair)) :
39N/A new ReentrantLock(fair);
39N/A final ReentrantReadWriteLock rwl = serialClone ?
39N/A serialClone(new ReentrantReadWriteLock(fair)) :
39N/A new ReentrantReadWriteLock(fair);
0N/A final int depth = 10;
39N/A equal(rl.isFair(), fair);
39N/A equal(rwl.isFair(), fair);
0N/A check(! rl.isLocked());
0N/A check(! rwl.isWriteLocked());
0N/A check(! rl.isHeldByCurrentThread());
0N/A check(! rwl.isWriteLockedByCurrentThread());
0N/A check(! rwl.writeLock().isHeldByCurrentThread());
0N/A
0N/A for (int i = 0; i < depth; i++) {
0N/A equal(rl.getHoldCount(), i);
0N/A equal(rwl.getReadLockCount(), i);
0N/A equal(rwl.getReadHoldCount(), i);
0N/A equal(rwl.getWriteHoldCount(), i);
0N/A equal(rwl.writeLock().getHoldCount(), i);
39N/A equal(rl.isLocked(), i > 0);
39N/A equal(rwl.isWriteLocked(), i > 0);
39N/A lock(rl);
39N/A lock(rwl.writeLock());
39N/A lock(rwl.readLock());
0N/A }
0N/A
0N/A for (int i = depth; i > 0; i--) {
0N/A check(! rl.hasQueuedThreads());
0N/A check(! rwl.hasQueuedThreads());
0N/A check(! rl.hasQueuedThread(Thread.currentThread()));
0N/A check(! rwl.hasQueuedThread(Thread.currentThread()));
0N/A check(rl.isLocked());
0N/A check(rwl.isWriteLocked());
0N/A check(rl.isHeldByCurrentThread());
0N/A check(rwl.isWriteLockedByCurrentThread());
0N/A check(rwl.writeLock().isHeldByCurrentThread());
0N/A equal(rl.getQueueLength(), 0);
0N/A equal(rwl.getQueueLength(), 0);
0N/A equal(rwl.getReadLockCount(), i);
0N/A equal(rl.getHoldCount(), i);
0N/A equal(rwl.getReadHoldCount(), i);
0N/A equal(rwl.getWriteHoldCount(), i);
0N/A equal(rwl.writeLock().getHoldCount(), i);
0N/A rwl.readLock().unlock();
0N/A rwl.writeLock().unlock();
0N/A rl.unlock();
0N/A }
39N/A THROWS(IllegalMonitorStateException.class,
39N/A new F(){void f(){rl.unlock();}},
39N/A new F(){void f(){rwl.readLock().unlock();}},
39N/A new F(){void f(){rwl.writeLock().unlock();}});
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
39N/A volatile int passed = 0, failed = 0;
39N/A void pass() {passed++;}
39N/A void fail() {failed++; Thread.dumpStack();}
39N/A void fail(String msg) {System.err.println(msg); fail();}
39N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
39N/A void check(boolean cond) {if (cond) pass(); else fail();}
39N/A void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else fail(x + " not equal to " + y);}
0N/A public static void main(String[] args) throws Throwable {
39N/A new Count().instanceMain(args);}
39N/A void instanceMain(String[] args) throws Throwable {
39N/A try {test(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
0N/A if (failed > 0) throw new AssertionError("Some tests failed");}
39N/A abstract class F {abstract void f() throws Throwable;}
39N/A void THROWS(Class<? extends Throwable> k, F... fs) {
39N/A for (F f : fs)
39N/A try {f.f(); fail("Expected " + k.getName() + " not thrown");}
39N/A catch (Throwable t) {
39N/A if (k.isAssignableFrom(t.getClass())) pass();
39N/A else unexpected(t);}}
39N/A
39N/A static byte[] serializedForm(Object obj) {
39N/A try {
39N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
39N/A new ObjectOutputStream(baos).writeObject(obj);
39N/A return baos.toByteArray();
39N/A } catch (IOException e) { throw new RuntimeException(e); }}
39N/A static Object readObject(byte[] bytes)
39N/A throws IOException, ClassNotFoundException {
39N/A InputStream is = new ByteArrayInputStream(bytes);
39N/A return new ObjectInputStream(is).readObject();}
39N/A @SuppressWarnings("unchecked")
39N/A static <T> T serialClone(T obj) {
39N/A try { return (T) readObject(serializedForm(obj)); }
39N/A catch (Exception e) { throw new RuntimeException(e); }}
0N/A}