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