annotate test/compiler/testlibrary/rtm/BusyLock.java @ 21142:0b221b4ad707

Merge
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Tue, 28 Apr 2015 12:58:40 -0700
parents 665bbe93823f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17871
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
1 /*
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
4 *
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
7 * published by the Free Software Foundation.
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
8 *
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
13 * accompanied this code).
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
14 *
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
18 *
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
21 * questions.
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
22 *
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
23 */
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
24
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
25 package rtm;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
26
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
27 import com.oracle.java.testlibrary.Utils;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
28 import sun.misc.Unsafe;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
29
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
30 import java.util.concurrent.BrokenBarrierException;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
31 import java.util.concurrent.CyclicBarrier;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
32
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
33 /**
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
34 * Test case for busy lock scenario.
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
35 * One thread enters the monitor and sleep for a while.
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
36 * Another thread is blocked on the same monitor.
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
37 */
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
38 public class BusyLock implements CompilableTest, Runnable {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
39 private static final int DEFAULT_TIMEOUT = 1000;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
40 private final CyclicBarrier barrier;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
41
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
42 // Following field have to be static in order to avoid escape analysis.
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
43 @SuppressWarnings("UnsuedDeclaration")
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
44 private static int field = 0;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
45 private static final Unsafe UNSAFE = Utils.getUnsafe();
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
46 protected final Object monitor;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
47 protected final int timeout;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
48
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
49 public BusyLock() {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
50 this(BusyLock.DEFAULT_TIMEOUT);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
51 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
52
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
53 public BusyLock(int timeout) {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
54 this.timeout = timeout;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
55 this.monitor = new Object();
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
56 this.barrier = new CyclicBarrier(2);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
57 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
58
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
59 @Override
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
60 public void run() {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
61 try {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
62 // wait until forceAbort leave monitor
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
63 barrier.await();
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
64 if (UNSAFE.tryMonitorEnter(monitor)) {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
65 try {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
66 barrier.await();
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
67 Thread.sleep(timeout);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
68 } finally {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
69 UNSAFE.monitorExit(monitor);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
70 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
71 } else {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
72 throw new RuntimeException("Monitor should be entered by " +
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
73 "::run() first.");
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
74 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
75 } catch (InterruptedException | BrokenBarrierException e) {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
76 throw new RuntimeException("Synchronization error happened.", e);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
77 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
78 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
79
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
80 public void test() {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
81 try {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
82 barrier.await();
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
83 // wait until monitor is locked by a ::run method
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
84 barrier.await();
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
85 } catch (InterruptedException | BrokenBarrierException e) {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
86 throw new RuntimeException("Synchronization error happened.", e);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
87 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
88 synchronized(monitor) {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
89 BusyLock.field++;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
90 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
91 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
92
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
93 @Override
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
94 public String getMethodWithLockName() {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
95 return this.getClass().getName() + "::test";
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
96 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
97
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
98 @Override
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
99 public String[] getMethodsToCompileNames() {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
100 return new String[] { getMethodWithLockName() };
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
101 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
102
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
103 /**
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
104 * Usage:
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
105 * BusyLock [ &lt;inflate monitor&gt; [ &lt;timeout&gt; ] ]
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
106 *
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
107 * Default values are:
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
108 * <ul>
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
109 * <li>inflate monitor = {@code true}</li>
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
110 * <li>timeout = {@code BusyLock.DEFAULT_TIMEOUT}</li>
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
111 * </ul>
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
112 */
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
113 public static void main(String args[]) throws Exception {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
114 int timeoutValue = BusyLock.DEFAULT_TIMEOUT;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
115 boolean inflateMonitor = true;
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
116
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
117 if (args.length > 0 ) {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
118 inflateMonitor = Boolean.valueOf(args[0]);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
119
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
120 if (args.length > 1) {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
121 timeoutValue = Integer.valueOf(args[1]);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
122 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
123 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
124
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
125 BusyLock busyLock = new BusyLock(timeoutValue);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
126
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
127 if (inflateMonitor) {
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
128 AbortProvoker.inflateMonitor(busyLock.monitor);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
129 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
130
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
131 Thread t = new Thread(busyLock);
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
132 t.start();
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
133 busyLock.test();
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
134 t.join();
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
135 }
665bbe93823f 8039499: Add all common classes used by tests on RTM support to testlibrary
iignatyev
parents:
diff changeset
136 }