jdk/test/java/lang/management/ThreadMXBean/MyOwnSynchronizer.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 23010 6dadb192ad81
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 23010
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     5086470 6358247
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic Test for ThreadMXBean.dumpAllThreads(false, true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          and getThreadInfo of customized JSR-166 synchronizers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 23010
diff changeset
    31
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @build Barrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @build ThreadDump
19814
77d0468c8874 8004179: Few of test/java/lang/management/ThreadMXBean/* tests don't clean up the created threads
jbachorik
parents: 5506
diff changeset
    34
 * @run main/othervm MyOwnSynchronizer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.concurrent.TimeUnit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class MyOwnSynchronizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static Mutex mutex = new Mutex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static MyThread thread = new MyThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (!mbean.isSynchronizerUsageSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            System.out.println("Monitoring of synchronizer usage not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        thread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // wait until myThread acquires mutex
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        while (!mutex.isLocked()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
               Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
           } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
               throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ThreadDump.threadDump();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Test dumpAllThreads with locked synchronizers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ThreadInfo[] tinfos = mbean.dumpAllThreads(false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        for (ThreadInfo ti : tinfos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
           MonitorInfo[] monitors = ti.getLockedMonitors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
           if (monitors.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
               throw new RuntimeException("Name: " + ti.getThreadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                   " has non-empty locked monitors = " + monitors.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
           LockInfo[] syncs = ti.getLockedSynchronizers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
           if (ti.getThreadId() == thread.getId()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
               thread.checkLockedSyncs(ti, syncs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Test getThreadInfo with locked synchronizers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        tinfos = mbean.getThreadInfo(new long[] {thread.getId()}, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (tinfos.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new RuntimeException("getThreadInfo() returns " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                tinfos.length + " ThreadInfo objects. Expected 0.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        ThreadInfo ti = tinfos[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (ti.getLockedMonitors().length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException("Name: " + ti.getThreadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
               " has non-empty locked monitors = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
               ti.getLockedMonitors().length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        thread.checkLockedSyncs(ti, ti.getLockedSynchronizers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static class Mutex implements Lock, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // Our internal helper class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        class Sync extends AbstractQueuedSynchronizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // Report whether in locked state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            protected boolean isHeldExclusively() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return getState() == 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            // Acquire the lock if state is zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            public boolean tryAcquire(int acquires) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                assert acquires == 1; // Otherwise unused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (compareAndSetState(0, 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                  setExclusiveOwnerThread(Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                  return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // Release the lock by setting state to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            protected boolean tryRelease(int releases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                assert releases == 1; // Otherwise unused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (getState() == 0) throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                setExclusiveOwnerThread(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                setState(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // Provide a Condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            Condition newCondition() { return new ConditionObject(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // Deserialize properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                setState(0); // reset to unlocked state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // The sync object does all the hard work. We just forward to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        private final Sync sync = new Sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        public void lock()                { sync.acquire(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        public boolean tryLock()          { return sync.tryAcquire(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public void unlock()              { sync.release(1); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        public Condition newCondition()   { return sync.newCondition(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        public boolean isLocked()         { return sync.isHeldExclusively(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        public void lockInterruptibly() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            sync.acquireInterruptibly(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return sync.tryAcquireNanos(1, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public AbstractOwnableSynchronizer getSync() { return sync; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    static class MyThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public MyThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            super("MyThread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            mutex.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            Object o = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            synchronized(o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    o.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int OWNED_SYNCS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        void checkLockedSyncs(ThreadInfo info, LockInfo[] syncs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (!getName().equals(info.getThreadName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                throw new RuntimeException("Name: " + info.getThreadName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    " not matched. Expected: " + getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (syncs.length != OWNED_SYNCS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new RuntimeException("Number of locked syncs = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    syncs.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    " not matched. Expected: " + OWNED_SYNCS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            AbstractOwnableSynchronizer s = mutex.getSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            String lockName = s.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            int hcode = System.identityHashCode(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (!lockName.equals(syncs[0].getClassName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                throw new RuntimeException("LockInfo : " + syncs[0] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    " class name not matched. Expected: " + lockName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (hcode != syncs[0].getIdentityHashCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                throw new RuntimeException("LockInfo: " + syncs[0] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    " IdentityHashCode not matched. Expected: " + hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
}