1 /*
  2  * Copyright (c) 2017, 2020, Red Hat, Inc. 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 
 25 /*
 26  * @test id=passive
 27  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 28  * @requires vm.gc.Shenandoah
 29  * @modules java.base/jdk.internal.misc
 30  *          java.management
 31  *
 32  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 33  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 34  *      -XX:+ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 35  *      TestVerifyJCStress
 36  *
 37  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 38  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
 39  *      -XX:-ShenandoahDegeneratedGC -XX:+ShenandoahVerify
 40  *      TestVerifyJCStress
 41  */
 42 
 43 /*
 44  * @test id=default
 45  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 46  * @requires vm.gc.Shenandoah
 47  * @requires !vm.debug
 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 
 62 /*
 63  * @test id=iu-debug
 64  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 65  * @requires vm.gc.Shenandoah
 66  * @requires vm.debug
 67  * @modules java.base/jdk.internal.misc
 68  *          java.management
 69  *
 70  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 71  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 72  *      -XX:+ShenandoahVerify -XX:+ShenandoahVerifyOptoBarriers
 73  *      TestVerifyJCStress
 74  */
 75 
 76 /*
 77  * @test id=iu
 78  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 79  * @requires vm.gc.Shenandoah
 80  * @requires !vm.debug
 81  * @modules java.base/jdk.internal.misc
 82  *          java.management
 83  *
 84  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 85  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 86  *      -XX:+ShenandoahVerify
 87  *      TestVerifyJCStress
 88  */
 89 
 90 /*
 91  * @test id=iu-c1
 92  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 93  * @requires vm.gc.Shenandoah
 94  * @modules java.base/jdk.internal.misc
 95  *          java.management
 96  *
 97  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 98  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 99  *      -XX:+ShenandoahVerify -XX:TieredStopAtLevel=1
100  *      TestVerifyJCStress
101  */
102 
103 
104 import java.util.*;
105 import java.util.concurrent.*;
106 import java.util.concurrent.locks.*;
107 
108 public class TestVerifyJCStress {
109 
110     public static void main(String[] args) throws Exception {
111         ExecutorService service = Executors.newFixedThreadPool(
112                 2,
113                 r -> {
114                     Thread t = new Thread(r);
115                     t.setDaemon(true);
116                     return t;
117                 }
118         );
119 
120         for (int c = 0; c < 10000; c++) {
121             final Test[] tests = new Test[10000];
122             for (int t = 0; t < tests.length; t++) {
123                 tests[t] = new Test();
124             }
125 
126             Future<?> f1 = service.submit(() -> {
127                 IntResult2 r = new IntResult2();
128                 for (Test test : tests) {
129                     test.RL_Us(r);
130                 }
131             });
132             Future<?> f2 = service.submit(() -> {
133                 for (Test test : tests) {
134                     test.WLI_Us();
135                 }
136             });
137 
138             f1.get();
139             f2.get();
140         }
141     }
142 
143     public static class IntResult2 {
144         int r1, r2;
145     }
146 
147     public static class Test {
148         final StampedLock lock = new StampedLock();
149 
150         int x, y;
151 
152         public void RL_Us(IntResult2 r) {
153             StampedLock lock = this.lock;
154             long stamp = lock.readLock();
155             r.r1 = x;
156             r.r2 = y;
157             lock.unlock(stamp);
158         }
159 
160         public void WLI_Us() {
161             try {
162                 StampedLock lock = this.lock;
163                 long stamp = lock.writeLockInterruptibly();
164                 x = 1;
165                 y = 2;
166                 lock.unlock(stamp);
167             } catch (InterruptedException e) {
168                 throw new RuntimeException(e);
169             }
170         }
171     }
172 
173 }