2
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
27
* @requires vm.gc != "Shenandoah" | vm.opt.ShenandoahGCMode != "iu"
29
* @build jdk.test.whitebox.WhiteBox
30
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
33
* -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
34
* gc.TestReferenceRefersToDuringConcMark
37
import java.lang.ref.Reference;
38
import java.lang.ref.WeakReference;
39
import jdk.test.whitebox.WhiteBox;
41
public class TestReferenceRefersToDuringConcMark {
42
private static final WhiteBox WB = WhiteBox.getWhiteBox();
44
private static volatile Object testObject = null;
46
private static WeakReference<Object> testWeak = null;
48
private static void setup() {
49
testObject = new Object();
50
testWeak = new WeakReference<Object>(testObject);
53
private static void gcUntilOld(Object o) throws Exception {
54
if (!WB.isObjectInOldGen(o)) {
56
if (!WB.isObjectInOldGen(o)) {
57
fail("object not promoted by full gc");
62
private static void gcUntilOld() throws Exception {
63
gcUntilOld(testObject);
67
private static void fail(String msg) throws Exception {
68
throw new RuntimeException(msg);
71
private static void expectNotCleared(Reference<Object> ref,
72
String which) throws Exception {
73
if (ref.refersTo(null)) {
74
fail("expected " + which + " to not be cleared");
78
private static void expectValue(Reference<Object> ref,
80
String which) throws Exception {
81
expectNotCleared(ref, which);
82
if (!ref.refersTo(value)) {
83
fail(which + " doesn't refer to expected value");
87
private static void checkInitialStates() throws Exception {
88
expectValue(testWeak, testObject, "testWeak");
91
private static void discardStrongReferences() {
95
private static void testConcurrentCollection() throws Exception {
99
WB.concurrentGCAcquireControl();
101
checkInitialStates();
103
discardStrongReferences();
105
WB.concurrentGCRunTo(WB.BEFORE_MARKING_COMPLETED);
107
// For most collectors - the configurations tested here -,
108
// calling get() will keep testObject alive.
109
if (testWeak.get() == null) {
110
fail("testWeak unexpectedly == null");
113
WB.concurrentGCRunToIdle();
115
expectNotCleared(testWeak, "testWeak");
117
WB.concurrentGCReleaseControl();
121
public static void main(String[] args) throws Exception {
122
if (WB.supportsConcurrentGCBreakpoints()) {
123
testConcurrentCollection();