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  * @modules java.base/jdk.internal.misc
 48  *          java.management
 49  *
 50  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 51  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
 52  *      -XX:+ShenandoahVerify
 53  *      TestVerifyJCStress
 54  *
 55  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 56  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
 57  *      -XX:+ShenandoahVerify
 58  *      TestVerifyJCStress
 59  */
 60 
 61 /*
 62  * @test id=iu
 63  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 64  * @requires vm.gc.Shenandoah
 65  * @modules java.base/jdk.internal.misc
 66  *          java.management
 67  *
 68  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 69  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 70  *      -XX:+ShenandoahVerify
 71  *      TestVerifyJCStress
 72  */
 73 
 74 /*
 75  * @test id=iu-c1
 76  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
 77  * @requires vm.gc.Shenandoah
 78  * @modules java.base/jdk.internal.misc
 79  *          java.management
 80  *
 81  * @run main/othervm -Xmx1g -Xms1g -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions
 82  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
 83  *      -XX:+ShenandoahVerify -XX:TieredStopAtLevel=1
 84  *      TestVerifyJCStress
 85  */
 86 
 87 
 88 import java.util.*;
 89 import java.util.concurrent.*;
 90 import java.util.concurrent.locks.*;
 91 
 92 public class TestVerifyJCStress {
 93 
 94     public static void main(String[] args) throws Exception {
 95         ExecutorService service = Executors.newFixedThreadPool(
 96                 2,
 97                 r -> {
 98                     Thread t = new Thread(r);
 99                     t.setDaemon(true);
100                     return t;
101                 }
102         );
103 
104         for (int c = 0; c < 10000; c++) {
105             final Test[] tests = new Test[10000];
106             for (int t = 0; t < tests.length; t++) {
107                 tests[t] = new Test();
108             }
109 
110             Future<?> f1 = service.submit(() -> {
111                 IntResult2 r = new IntResult2();
112                 for (Test test : tests) {
113                     test.RL_Us(r);
114                 }
115             });
116             Future<?> f2 = service.submit(() -> {
117                 for (Test test : tests) {
118                     test.WLI_Us();
119                 }
120             });
121 
122             f1.get();
123             f2.get();
124         }
125     }
126 
127     public static class IntResult2 {
128         int r1, r2;
129     }
130 
131     public static class Test {
132         final StampedLock lock = new StampedLock();
133 
134         int x, y;
135 
136         public void RL_Us(IntResult2 r) {
137             StampedLock lock = this.lock;
138             long stamp = lock.readLock();
139             r.r1 = x;
140             r.r2 = y;
141             lock.unlock(stamp);
142         }
143 
144         public void WLI_Us() {
145             try {
146                 StampedLock lock = this.lock;
147                 long stamp = lock.writeLockInterruptibly();
148                 x = 1;
149                 y = 2;
150                 lock.unlock(stamp);
151             } catch (InterruptedException e) {
152                 throw new RuntimeException(e);
153             }
154         }
155     }
156 
157 }