Mercurial > hg > graal-compiler
comparison test/compiler/testlibrary/rtm/BusyLock.java @ 17871:665bbe93823f
8039499: Add all common classes used by tests on RTM support to testlibrary
Reviewed-by: kvn, iignatyev
Contributed-by: filipp.zhinkin@oracle.com
author | iignatyev |
---|---|
date | Mon, 14 Apr 2014 19:29:34 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
17870:a0eb3f61d34a | 17871:665bbe93823f |
---|---|
1 /* | |
2 * Copyright (c) 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 * | |
23 */ | |
24 | |
25 package rtm; | |
26 | |
27 import com.oracle.java.testlibrary.Utils; | |
28 import sun.misc.Unsafe; | |
29 | |
30 import java.util.concurrent.BrokenBarrierException; | |
31 import java.util.concurrent.CyclicBarrier; | |
32 | |
33 /** | |
34 * Test case for busy lock scenario. | |
35 * One thread enters the monitor and sleep for a while. | |
36 * Another thread is blocked on the same monitor. | |
37 */ | |
38 public class BusyLock implements CompilableTest, Runnable { | |
39 private static final int DEFAULT_TIMEOUT = 1000; | |
40 private final CyclicBarrier barrier; | |
41 | |
42 // Following field have to be static in order to avoid escape analysis. | |
43 @SuppressWarnings("UnsuedDeclaration") | |
44 private static int field = 0; | |
45 private static final Unsafe UNSAFE = Utils.getUnsafe(); | |
46 protected final Object monitor; | |
47 protected final int timeout; | |
48 | |
49 public BusyLock() { | |
50 this(BusyLock.DEFAULT_TIMEOUT); | |
51 } | |
52 | |
53 public BusyLock(int timeout) { | |
54 this.timeout = timeout; | |
55 this.monitor = new Object(); | |
56 this.barrier = new CyclicBarrier(2); | |
57 } | |
58 | |
59 @Override | |
60 public void run() { | |
61 try { | |
62 // wait until forceAbort leave monitor | |
63 barrier.await(); | |
64 if (UNSAFE.tryMonitorEnter(monitor)) { | |
65 try { | |
66 barrier.await(); | |
67 Thread.sleep(timeout); | |
68 } finally { | |
69 UNSAFE.monitorExit(monitor); | |
70 } | |
71 } else { | |
72 throw new RuntimeException("Monitor should be entered by " + | |
73 "::run() first."); | |
74 } | |
75 } catch (InterruptedException | BrokenBarrierException e) { | |
76 throw new RuntimeException("Synchronization error happened.", e); | |
77 } | |
78 } | |
79 | |
80 public void test() { | |
81 try { | |
82 barrier.await(); | |
83 // wait until monitor is locked by a ::run method | |
84 barrier.await(); | |
85 } catch (InterruptedException | BrokenBarrierException e) { | |
86 throw new RuntimeException("Synchronization error happened.", e); | |
87 } | |
88 synchronized(monitor) { | |
89 BusyLock.field++; | |
90 } | |
91 } | |
92 | |
93 @Override | |
94 public String getMethodWithLockName() { | |
95 return this.getClass().getName() + "::test"; | |
96 } | |
97 | |
98 @Override | |
99 public String[] getMethodsToCompileNames() { | |
100 return new String[] { getMethodWithLockName() }; | |
101 } | |
102 | |
103 /** | |
104 * Usage: | |
105 * BusyLock [ <inflate monitor> [ <timeout> ] ] | |
106 * | |
107 * Default values are: | |
108 * <ul> | |
109 * <li>inflate monitor = {@code true}</li> | |
110 * <li>timeout = {@code BusyLock.DEFAULT_TIMEOUT}</li> | |
111 * </ul> | |
112 */ | |
113 public static void main(String args[]) throws Exception { | |
114 int timeoutValue = BusyLock.DEFAULT_TIMEOUT; | |
115 boolean inflateMonitor = true; | |
116 | |
117 if (args.length > 0 ) { | |
118 inflateMonitor = Boolean.valueOf(args[0]); | |
119 | |
120 if (args.length > 1) { | |
121 timeoutValue = Integer.valueOf(args[1]); | |
122 } | |
123 } | |
124 | |
125 BusyLock busyLock = new BusyLock(timeoutValue); | |
126 | |
127 if (inflateMonitor) { | |
128 AbortProvoker.inflateMonitor(busyLock.monitor); | |
129 } | |
130 | |
131 Thread t = new Thread(busyLock); | |
132 t.start(); | |
133 busyLock.test(); | |
134 t.join(); | |
135 } | |
136 } |