1 /*
  2  * Copyright (c) 2017, 2020, Red Hat, Inc. All rights reserved.
  3  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 /*
 27  * @test id=passive
 28  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 29  * @requires vm.gc.Shenandoah
 30  * @modules java.base/jdk.internal.misc
 31  *          java.management
 32  *
 33  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 34  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 35  *      -XX:+ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 36  *      TestVerifyJCStress
 37  *
 38  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 39  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 40  *      -XX:-ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 41  *      TestVerifyJCStress
 42  */
 43 
 44 /*
 45  * @test id=default
 46  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 47  * @requires vm.gc.Shenandoah
 48  * @modules java.base/jdk.internal.misc
 49  *          java.management
 50  *
 51  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 52  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
 53  *      -XX:+ShenandoahVerify
 54  *      TestVerifyJCStress
 55  *
 56  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 57  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
 58  *      -XX:+ShenandoahVerify
 59  *      TestVerifyJCStress
 60  *
 61  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 62  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive -XX:ShenandoahGCMode=generational
 63  *      -XX:+ShenandoahVerify
 64  *      TestVerifyJCStress
 65  */
 66 
 67 /*
 68  * @test id=iu
 69  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 70  * @requires vm.gc.Shenandoah
 71  * @modules java.base/jdk.internal.misc
 72  *          java.management
 73  *
 74  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 75  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 76  *      -XX:+ShenandoahVerify
 77  *      TestVerifyJCStress
 78  */
 79 
 80 /*
 81  * @test id=iu-c1
 82  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 83  * @requires vm.gc.Shenandoah
 84  * @modules java.base/jdk.internal.misc
 85  *          java.management
 86  *
 87  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 88  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 89  *      -XX:+ShenandoahVerify -XX:TieredStopAtLevel=1
 90  *      TestVerifyJCStress
 91  */
 92 
 93 
 94 import java.util.*;
 95 import java.util.concurrent.*;
 96 import java.util.concurrent.locks.*;
 97 
 98 public class TestVerifyJCStress {
 99 
100     public static void main(String[] args) throws Exception {
101         ExecutorService service = Executors.newFixedThreadPool(
102                 2,
103                 r -> {
104                     Thread t = new Thread(r);
105                     t.setDaemon(true);
106                     return t;
107                 }
108         );
109 
110         for (int c = 0; c < 10000; c++) {
111             final Test[] tests = new Test[10000];
112             for (int t = 0; t < tests.length; t++) {
113                 tests[t] = new Test();
114             }
115 
116             Future<?> f1 = service.submit(() -> {
117                 IntResult2 r = new IntResult2();
118                 for (Test test : tests) {
119                     test.RL_Us(r);
120                 }
121             });
122             Future<?> f2 = service.submit(() -> {
123                 for (Test test : tests) {
124                     test.WLI_Us();
125                 }
126             });
127 
128             f1.get();
129             f2.get();
130         }
131     }
132 
133     public static class IntResult2 {
134         int r1, r2;
135     }
136 
137     public static class Test {
138         final StampedLock lock = new StampedLock();
139 
140         int x, y;
141 
142         public void RL_Us(IntResult2 r) {
143             StampedLock lock = this.lock;
144             long stamp = lock.readLock();
145             r.r1 = x;
146             r.r2 = y;
147             lock.unlock(stamp);
148         }
149 
150         public void WLI_Us() {
151             try {
152                 StampedLock lock = this.lock;
153                 long stamp = lock.writeLockInterruptibly();
154                 x = 1;
155                 y = 2;
156                 lock.unlock(stamp);
157             } catch (InterruptedException e) {
158                 throw new RuntimeException(e);
159             }
160         }
161     }
162 
163 }