43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.core.test;
|
|
26 |
|
|
27 |
import java.lang.ref.Reference;
|
|
28 |
import java.lang.ref.ReferenceQueue;
|
|
29 |
import java.lang.ref.WeakReference;
|
|
30 |
|
|
31 |
import org.junit.Test;
|
|
32 |
import org.graalvm.compiler.graph.Node;
|
|
33 |
import org.graalvm.compiler.loop.LoopEx;
|
|
34 |
import org.graalvm.compiler.loop.LoopsData;
|
|
35 |
import org.graalvm.compiler.nodes.FieldLocationIdentity;
|
|
36 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
37 |
import org.graalvm.compiler.nodes.memory.Access;
|
49873
|
38 |
import jdk.internal.vm.compiler.word.LocationIdentity;
|
43972
|
39 |
|
|
40 |
import jdk.vm.ci.meta.ResolvedJavaField;
|
|
41 |
|
|
42 |
public class ReferenceGetLoopTest extends GraalCompilerTest {
|
|
43 |
|
|
44 |
@Override
|
55509
|
45 |
protected void checkMidTierGraph(StructuredGraph graph) {
|
43972
|
46 |
final LoopsData loops = new LoopsData(graph);
|
|
47 |
boolean found = false;
|
|
48 |
for (LoopEx loop : loops.loops()) {
|
|
49 |
for (Node node : loop.inside().nodes()) {
|
|
50 |
if (node instanceof Access) {
|
|
51 |
Access access = (Access) node;
|
|
52 |
LocationIdentity location = access.getLocationIdentity();
|
|
53 |
if (location instanceof FieldLocationIdentity) {
|
|
54 |
ResolvedJavaField field = ((FieldLocationIdentity) location).getField();
|
|
55 |
if (field.getName().equals("referent") && field.getDeclaringClass().equals(getMetaAccess().lookupJavaType(Reference.class))) {
|
|
56 |
found = true;
|
|
57 |
}
|
|
58 |
}
|
|
59 |
}
|
|
60 |
}
|
|
61 |
}
|
|
62 |
if (!found) {
|
|
63 |
assertTrue(false, "Reference.referent not found in loop: " + getCanonicalGraphString(graph, true, false));
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
public volatile Object referent;
|
|
68 |
public final FinalWeakReference<Object> ref;
|
|
69 |
public final ReferenceQueue<Object> refQueue;
|
|
70 |
|
|
71 |
/*
|
|
72 |
* Ensure that the Reference.get invoke is statically bindable.
|
|
73 |
*/
|
|
74 |
public static final class FinalWeakReference<T> extends WeakReference<T> {
|
|
75 |
public FinalWeakReference(T referent, ReferenceQueue<? super T> q) {
|
|
76 |
super(referent, q);
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
public ReferenceGetLoopTest() {
|
|
81 |
referent = new Object();
|
|
82 |
refQueue = new ReferenceQueue<>();
|
|
83 |
ref = new FinalWeakReference<>(referent, refQueue);
|
|
84 |
}
|
|
85 |
|
|
86 |
@Test
|
|
87 |
public void test() {
|
|
88 |
getCode(getMetaAccess().lookupJavaMethod(getMethod("testSnippet")));
|
|
89 |
}
|
|
90 |
|
|
91 |
public void testSnippet() {
|
|
92 |
while (ref.get() != null) {
|
|
93 |
// burn!
|
|
94 |
}
|
|
95 |
}
|
|
96 |
}
|