J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6207928 6328220 6378321 |
| 27 | * @summary Recursive lock invariant sanity checks |
| 28 | * @author Martin Buchholz |
| 29 | */ |
| 30 | |
| 31 | import java.util.*; |
| 32 | import java.util.concurrent.*; |
| 33 | import java.util.concurrent.locks.*; |
| 34 | |
| 35 | // I am the Cownt, and I lahve to cownt. |
| 36 | public class Count { |
| 37 | private static void realMain(String[] args) throws Throwable { |
| 38 | final ReentrantLock rl = new ReentrantLock(); |
| 39 | final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); |
| 40 | final int depth = 10; |
| 41 | check(! rl.isLocked()); |
| 42 | check(! rwl.isWriteLocked()); |
| 43 | check(! rl.isHeldByCurrentThread()); |
| 44 | check(! rwl.isWriteLockedByCurrentThread()); |
| 45 | check(! rwl.writeLock().isHeldByCurrentThread()); |
| 46 | |
| 47 | for (int i = 0; i < depth; i++) { |
| 48 | equal(rl.getHoldCount(), i); |
| 49 | equal(rwl.getReadLockCount(), i); |
| 50 | equal(rwl.getReadHoldCount(), i); |
| 51 | equal(rwl.getWriteHoldCount(), i); |
| 52 | equal(rwl.writeLock().getHoldCount(), i); |
| 53 | switch (i%4) { |
| 54 | case 0: |
| 55 | rl.lock(); |
| 56 | rwl.writeLock().lock(); |
| 57 | rwl.readLock().lock(); |
| 58 | break; |
| 59 | case 1: |
| 60 | rl.lockInterruptibly(); |
| 61 | rwl.writeLock().lockInterruptibly(); |
| 62 | rwl.readLock().lockInterruptibly(); |
| 63 | break; |
| 64 | case 2: |
| 65 | check(rl.tryLock()); |
| 66 | check(rwl.writeLock().tryLock()); |
| 67 | check(rwl.readLock().tryLock()); |
| 68 | break; |
| 69 | case 3: |
| 70 | check(rl.tryLock(454, TimeUnit.MILLISECONDS)); |
| 71 | check(rwl.writeLock().tryLock(454, TimeUnit.NANOSECONDS)); |
| 72 | check(rwl.readLock().tryLock(454, TimeUnit.HOURS)); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | for (int i = depth; i > 0; i--) { |
| 78 | check(! rl.hasQueuedThreads()); |
| 79 | check(! rwl.hasQueuedThreads()); |
| 80 | check(! rl.hasQueuedThread(Thread.currentThread())); |
| 81 | check(! rwl.hasQueuedThread(Thread.currentThread())); |
| 82 | check(rl.isLocked()); |
| 83 | check(rwl.isWriteLocked()); |
| 84 | check(rl.isHeldByCurrentThread()); |
| 85 | check(rwl.isWriteLockedByCurrentThread()); |
| 86 | check(rwl.writeLock().isHeldByCurrentThread()); |
| 87 | equal(rl.getQueueLength(), 0); |
| 88 | equal(rwl.getQueueLength(), 0); |
| 89 | equal(rwl.getReadLockCount(), i); |
| 90 | equal(rl.getHoldCount(), i); |
| 91 | equal(rwl.getReadHoldCount(), i); |
| 92 | equal(rwl.getWriteHoldCount(), i); |
| 93 | equal(rwl.writeLock().getHoldCount(), i); |
| 94 | rwl.readLock().unlock(); |
| 95 | rwl.writeLock().unlock(); |
| 96 | rl.unlock(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | //--------------------- Infrastructure --------------------------- |
| 101 | static volatile int passed = 0, failed = 0; |
| 102 | static void pass() {passed++;} |
| 103 | static void fail() {failed++; Thread.dumpStack();} |
| 104 | static void fail(String msg) {System.out.println(msg); fail();} |
| 105 | static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
| 106 | static void check(boolean cond) {if (cond) pass(); else fail();} |
| 107 | static void equal(Object x, Object y) { |
| 108 | if (x == null ? y == null : x.equals(y)) pass(); |
| 109 | else fail(x + " not equal to " + y);} |
| 110 | public static void main(String[] args) throws Throwable { |
| 111 | try {realMain(args);} catch (Throwable t) {unexpected(t);} |
| 112 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 113 | if (failed > 0) throw new AssertionError("Some tests failed");} |
| 114 | } |