jdk/test/java/lang/management/ThreadMXBean/SynchronizerLockingThread.java
author mchung
Tue, 26 May 2009 18:09:32 -0700
changeset 2934 7b3a71a2fc3c
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6535104: TEST_BUG: FindDeadlocks.java fails intermittently. Summary: Retry a few times to check thread status before reporting failure Reviewed-by: swamyv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug     5086470 6358247
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary SynchronizersLockingThread is used by LockedSynchronizers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          It will create threads that acquire ReentrantLock and also object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          monitors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
2934
7b3a71a2fc3c 6535104: TEST_BUG: FindDeadlocks.java fails intermittently.
mchung
parents: 2
diff changeset
    31
 * @build ThreadDump Utils
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.concurrent.locks.ReentrantLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.locks.Condition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class SynchronizerLockingThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static ReentrantLock lock1 = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static ReentrantLock lock2 = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static ReentrantLock lock3 = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static ReentrantLock lock4 = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static Lock lock5 = new Lock("lock5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static Lock lock6 = new Lock("lock6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static Lock lock7 = new Lock("lock7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static ReentrantLock lock8 = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static SynchronizerLockingThread t1 = new Thread1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static SynchronizerLockingThread t2 = new Thread2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static int count = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void startLockingThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        t1.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        t2.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        t1.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        t2.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // wait until t1 and t2 waits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        while (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
               Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
           } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
               throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
2934
7b3a71a2fc3c 6535104: TEST_BUG: FindDeadlocks.java fails intermittently.
mchung
parents: 2
diff changeset
    66
7b3a71a2fc3c 6535104: TEST_BUG: FindDeadlocks.java fails intermittently.
mchung
parents: 2
diff changeset
    67
        Utils.waitForBlockWaitingState(t1);
7b3a71a2fc3c 6535104: TEST_BUG: FindDeadlocks.java fails intermittently.
mchung
parents: 2
diff changeset
    68
        Utils.waitForBlockWaitingState(t2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static long[] getThreadIds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return new long[] {t1.getId(), t2.getId()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static void checkLocks(ThreadInfo[] tinfos) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        int matches = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        for (ThreadInfo info : tinfos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (info.getThreadId() == t1.getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                t1.checkLocks(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (info.getThreadId() == t2.getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                t2.checkLocks(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                matches++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (matches != 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException("MonitorInfo missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static class Lock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        Lock(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    final String threadName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    Lock   waitingLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    int    numOwnedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    Map<String, Lock[]> ownedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    Condition       waitingSync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    int             numOwnedSyncs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    Map<String, ReentrantLock[]> ownedSyncs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public SynchronizerLockingThread(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.threadName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    protected void setExpectedResult(Lock waitingLock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                     int numOwnedMonitors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                     Map<String, Lock[]> ownedMonitors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                     Condition waitingSync,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                     int numOwnedSyncs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                     Map<String, ReentrantLock[]> ownedSyncs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.waitingLock = waitingLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.numOwnedMonitors = numOwnedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.ownedMonitors = ownedMonitors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.waitingSync = waitingSync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.numOwnedSyncs = numOwnedSyncs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.ownedSyncs = ownedSyncs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    void checkLocks(ThreadInfo info) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        checkThreadInfo(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        MonitorInfo[] monitors = info.getLockedMonitors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (monitors.length != numOwnedMonitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ThreadDump.threadDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new RuntimeException("Number of locked monitors = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                monitors.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                " not matched. Expected: " + numOwnedMonitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // check if each monitor returned in the list is the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        for (MonitorInfo m : monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            StackTraceElement ste = m.getLockedStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            int depth = m.getLockedStackDepth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            checkStackFrame(info, ste, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            checkMonitor(m, ste.getMethodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        // check if each expected monitor is included in the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        for (Map.Entry<String, Lock[]> e : ownedMonitors.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            for (Lock l : e.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                checkMonitor(e.getKey(), l, monitors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // We can only check if the length matches since we have no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // way to get the AbstractOwnableSynchronizer in ReentrantLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        LockInfo[] syncs = info.getLockedSynchronizers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (syncs.length != numOwnedSyncs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            ThreadDump.threadDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new RuntimeException("Number of locked syncs = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                syncs.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                " not matched. Expected: " + numOwnedSyncs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    void checkThreadInfo(ThreadInfo info) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (!getName().equals(info.getThreadName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new RuntimeException("Name: " + info.getThreadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                " not matched. Expected: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        LockInfo l = info.getLockInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if ((waitingLock != null || waitingSync != null) && l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new RuntimeException("LockInfo: " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                " not matched. Expected: non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (waitingLock == null && waitingSync == null && l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new RuntimeException("LockInfo: " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                " not matched. Expected: null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        String waitingLockName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int hcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (waitingLock != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            waitingLockName = waitingLock.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            hcode = System.identityHashCode(waitingLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            waitingLockName = waitingSync.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            hcode = System.identityHashCode(waitingSync);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (!waitingLockName.equals(l.getClassName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new RuntimeException("LockInfo : " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                " class name not matched. Expected: " + waitingLockName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (hcode != l.getIdentityHashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new RuntimeException("LockInfo: " + l +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                " IdentityHashCode not matched. Expected: " + hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        String lockName = info.getLockName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        String[] s = lockName.split("@");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (!waitingLockName.equals(s[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new RuntimeException("LockName: " + lockName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                " class name not matched. Expected: " + waitingLockName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int i = Integer.parseInt(s[1], 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (hcode != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throw new RuntimeException("LockName: " + lockName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                " IdentityHashCode not matched. Expected: " + hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    void checkStackFrame(ThreadInfo info, StackTraceElement ste, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        StackTraceElement[] stacktrace = info.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (!ste.equals(stacktrace[depth])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            System.out.println("LockedStackFrame:- " + ste);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            System.out.println("StackTrace at " + depth + " :-" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                stacktrace[depth]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new RuntimeException("LockedStackFrame does not match " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                "stack frame in ThreadInfo.getStackTrace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    void checkMonitor(MonitorInfo m, String methodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        for (Map.Entry<String, Lock[]> e : ownedMonitors.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (methodName.equals(e.getKey())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                for (Lock l : e.getValue()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    String className = l.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    int hcode = System.identityHashCode(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    if (className.equals(m.getClassName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        hcode == m.getIdentityHashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        // monitor matched the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        throw new RuntimeException("Monitor not expected" + m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    void checkMonitor(String methodName, Lock l, MonitorInfo[] monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        String className = l.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int hcode = System.identityHashCode(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        for (MonitorInfo m : monitors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (className.equals(m.getClassName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                hcode == m.getIdentityHashCode() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                methodName.equals(m.getLockedStackFrame().getMethodName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        throw new RuntimeException("Monitor not found in the returned list" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            " Method: " + methodName + " Lock: " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    static class Thread1 extends SynchronizerLockingThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        public Thread1() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            super("t1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            initExpectedResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            A();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        void A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            lock1.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                lock2.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    lock3.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        B();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        lock3.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    lock2.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                lock1.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        void B() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            lock4.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                synchronized(lock5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    C();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                lock4.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        void C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            synchronized(lock6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        void D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            synchronized(lock7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    // signal to about to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    lock7.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Map<String, Lock[]> LOCKED_MONITORS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        Map<String, ReentrantLock[]> LOCKED_SYNCS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        Lock WAITING_LOCK = lock7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        int OWNED_MONITORS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int OWNED_SYNCS = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        void initExpectedResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            LOCKED_MONITORS = new HashMap<String, Lock[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            LOCKED_MONITORS.put("D", new Lock[0]); // no monitored locked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            LOCKED_MONITORS.put("C", new Lock[] {lock6});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            LOCKED_MONITORS.put("B", new Lock[] {lock5});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            LOCKED_MONITORS.put("A", new Lock[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            LOCKED_SYNCS = new HashMap<String, ReentrantLock[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            LOCKED_SYNCS.put("D", new ReentrantLock[0]); // no sync locked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            LOCKED_SYNCS.put("C", new ReentrantLock[0]); // no sync locked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            LOCKED_SYNCS.put("B", new ReentrantLock[] {lock4});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            LOCKED_SYNCS.put("A", new ReentrantLock[] {lock3, lock2, lock1});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            this.setExpectedResult(WAITING_LOCK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                   OWNED_MONITORS, LOCKED_MONITORS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                   null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                   OWNED_SYNCS, LOCKED_SYNCS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    static class Thread2 extends SynchronizerLockingThread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        Map<String, Lock[]> LOCKED_MONITORS = new HashMap<String, Lock[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        Map<String, ReentrantLock[]> LOCKED_SYNCS = new HashMap<String, ReentrantLock[]>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        Condition c = lock8.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        Condition WAITING_LOCK = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        int OWNED_MONITORS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        int OWNED_SYNCS = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        public Thread2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            super("t2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            this.setExpectedResult(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                   OWNED_MONITORS, LOCKED_MONITORS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                   WAITING_LOCK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                   OWNED_SYNCS, LOCKED_SYNCS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            lock8.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                synchronized(lock7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                c.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                lock8.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            throw new RuntimeException("should not reach here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
}