1 /*
 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.
 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 gc.shenandoah;
25 
26 /* @test id=satb-100
27  * @requires vm.gc.Shenandoah
28  * @library /test/lib
29  * @build jdk.test.whitebox.WhiteBox
30  * @modules java.base
31  * @run main jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
32  * @run main/othervm
33  *      -Xbootclasspath/a:.
34  *      -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
35  *      -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=satb -XX:ShenandoahGarbageThreshold=100 -Xmx100m
36  *      gc.shenandoah.TestReferenceShortcutCycle
37  */
38 
39 import java.lang.ref.PhantomReference;
40 import java.lang.ref.Reference;
41 import java.lang.ref.ReferenceQueue;
42 import java.lang.ref.WeakReference;
43 import jdk.test.whitebox.WhiteBox;
44 
45 public class TestReferenceShortcutCycle {
46     private static final int NUM_ITEMS = 100000;
47 
48     private static final WhiteBox WB = WhiteBox.getWhiteBox();
49 
50     private static final class TestObject {
51         public final int value;
52 
53         public TestObject(int value) {
54             this.value = value;
55         }
56     }
57 
58     private static WeakReference[] refs;
59 
60     private static void setup() {
61         refs = new WeakReference[NUM_ITEMS];
62         for (int i = 0; i < NUM_ITEMS; i++) {
63             refs[i] = new WeakReference<>(new TestObject(i));
64         }
65     }
66 
67     private static void fail(String msg) throws Exception {
68         throw new RuntimeException(msg);
69     }
70 
71     private static void testConcurrentCollection() throws Exception {
72         setup();
73         WB.concurrentGCAcquireControl();
74         try {
75             WB.concurrentGCRunToIdle();
76             WB.concurrentGCRunTo(WB.AFTER_CONCURRENT_REFERENCE_PROCESSING_STARTED);
77             for (int i = 0; i < NUM_ITEMS; i++) {
78                 if (refs[i].get() != null) {
79                     fail("resurrected referent");
80                 }
81             }
82         } finally {
83             WB.concurrentGCReleaseControl();
84         }
85     }
86     public static void main(String[] args) throws Exception {
87         testConcurrentCollection();
88     }
89 }