Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetCurrentContendedMonitor/contmon001.java
40948 views
1
/*
2
* Copyright (c) 2003, 2018, 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
package nsk.jvmti.GetCurrentContendedMonitor;
25
26
import java.io.PrintStream;
27
28
public class contmon001 {
29
30
native static void checkMon(int point, Thread thr, Object mon);
31
native static int getRes();
32
33
static {
34
try {
35
System.loadLibrary("contmon001");
36
} catch (UnsatisfiedLinkError ule) {
37
System.err.println("Could not load contmon001 library");
38
System.err.println("java.library.path:"
39
+ System.getProperty("java.library.path"));
40
throw ule;
41
}
42
}
43
44
public static volatile boolean startingBarrier = true;
45
public static volatile boolean waitingBarrier = true;
46
static Object lockFld = new Object();
47
48
static boolean DEBUG_MODE = false;
49
static PrintStream out;
50
51
public static void main(String[] args) {
52
args = nsk.share.jvmti.JVMTITest.commonInit(args);
53
54
System.exit(run(args, System.out) + 95/*STATUS_TEMP*/);
55
}
56
57
public static void doSleep() {
58
try {
59
Thread.sleep(10);
60
} catch (Exception e) {
61
throw new Error("Unexpected " + e);
62
}
63
}
64
65
public static int run(String argv[], PrintStream ref) {
66
out = ref;
67
for (int i = 0; i < argv.length; i++) {
68
if (argv[i].equals("-v")) // verbose mode
69
DEBUG_MODE = true;
70
}
71
72
Object lock = new Object();
73
Thread currThr = Thread.currentThread();
74
75
if (DEBUG_MODE)
76
out.println("\nCheck #1: verifying a contended monitor of current thread \""
77
+ currThr.getName() + "\" ...");
78
synchronized (lock) {
79
checkMon(1, currThr, null);
80
}
81
if (DEBUG_MODE)
82
out.println("Check #1 done");
83
84
contmon001a thr = new contmon001a();
85
86
thr.start();
87
if (DEBUG_MODE)
88
out.println("\nWaiting for auxiliary thread ...");
89
while (startingBarrier) {
90
doSleep();
91
}
92
if (DEBUG_MODE)
93
out.println("Auxiliary thread is ready");
94
95
if (DEBUG_MODE)
96
out.println("\nCheck #3: verifying a contended monitor of auxiliary thread ...");
97
checkMon(3, thr, null);
98
if (DEBUG_MODE)
99
out.println("Check #3 done");
100
101
thr.letItGo();
102
103
while (waitingBarrier) {
104
doSleep();
105
}
106
synchronized (lockFld) {
107
if (DEBUG_MODE)
108
out.println("\nMain thread entered lockFld's monitor"
109
+ "\n\tand calling lockFld.notifyAll() to awake auxiliary thread");
110
lockFld.notifyAll();
111
if (DEBUG_MODE)
112
out.println("\nCheck #4: verifying a contended monitor of auxiliary thread ...");
113
checkMon(4, thr, lockFld);
114
if (DEBUG_MODE)
115
out.println("Check #4 done");
116
}
117
118
if (DEBUG_MODE)
119
out.println("\nMain thread released lockFld's monitor"
120
+ "\n\tand waiting for auxiliary thread death ...");
121
122
try {
123
thr.join();
124
} catch (InterruptedException e) {
125
throw new Error("Unexpected " + e);
126
}
127
if (DEBUG_MODE)
128
out.println("\nCheck #5: verifying a contended monitor of dead auxiliary thread ...");
129
checkMon(5, thr, null);
130
if (DEBUG_MODE)
131
out.println("Check #5 done");
132
133
return getRes();
134
}
135
}
136
137
138
class contmon001a extends Thread {
139
private volatile boolean flag = true;
140
141
public void run() {
142
if (contmon001.DEBUG_MODE)
143
contmon001.out.println("check #2: verifying a contended monitor of current auxiliary thread ...");
144
contmon001.checkMon(2, currentThread(), null);
145
if (contmon001.DEBUG_MODE)
146
contmon001.out.println("check #2 done");
147
148
if (contmon001.DEBUG_MODE)
149
contmon001.out.println("notifying main thread");
150
contmon001.startingBarrier = false;
151
152
if (contmon001.DEBUG_MODE)
153
contmon001.out.println("thread is going to loop while <flag> is true ...");
154
int i = 0;
155
int n = 1000;
156
while (flag) {
157
if (n <= 0) {
158
n = 1000;
159
}
160
if (i > n) {
161
i = 0;
162
n--;
163
}
164
i++;
165
}
166
if (contmon001.DEBUG_MODE)
167
contmon001.out.println("looping is done: <flag> is false");
168
169
synchronized (contmon001.lockFld) {
170
contmon001.waitingBarrier = false;
171
if (contmon001.DEBUG_MODE)
172
contmon001.out.println("\nthread entered lockFld's monitor"
173
+ "\n\tand releasing it through the lockFld.wait() call");
174
try {
175
contmon001.lockFld.wait();
176
} catch (InterruptedException e) {
177
throw new Error("Unexpected " + e);
178
}
179
}
180
181
if (contmon001.DEBUG_MODE)
182
contmon001.out.println("thread exiting");
183
}
184
185
public void letItGo() {
186
flag = false;
187
}
188
}
189
190