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