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 import java.util.*;
 63 import java.util.concurrent.*;
 64 import java.util.concurrent.locks.*;
 65 
 66 public class TestVerifyJCStress {
 67 
 68     public static void main(String[] args) throws Exception {
 69         ExecutorService service = Executors.newFixedThreadPool(
 70                 2,
 71                 r -> {
 72                     Thread t = new Thread(r);
 73                     t.setDaemon(true);
 74                     return t;
 75                 }
 76         );
 77 
 78         for (int c = 0; c < 10000; c++) {
 79             final Test[] tests = new Test[10000];
 80             for (int t = 0; t < tests.length; t++) {
 81                 tests[t] = new Test();
 82             }
 83 
 84             Future<?> f1 = service.submit(() -> {
 85                 IntResult2 r = new IntResult2();
 86                 for (Test test : tests) {
 87                     test.RL_Us(r);
 88                 }
 89             });
 90             Future<?> f2 = service.submit(() -> {
 91                 for (Test test : tests) {
 92                     test.WLI_Us();
 93                 }
 94             });
 95 
 96             f1.get();
 97             f2.get();
 98         }
 99     }
100 
101     public static class IntResult2 {
102         int r1, r2;
103     }
104 
105     public static class Test {
106         final StampedLock lock = new StampedLock();
107 
108         int x, y;
109 
110         public void RL_Us(IntResult2 r) {
111             StampedLock lock = this.lock;
112             long stamp = lock.readLock();
113             r.r1 = x;
114             r.r2 = y;
115             lock.unlock(stamp);
116         }
117 
118         public void WLI_Us() {
119             try {
120                 StampedLock lock = this.lock;
121                 long stamp = lock.writeLockInterruptibly();
122                 x = 1;
123                 y = 2;
124                 lock.unlock(stamp);
125             } catch (InterruptedException e) {
126                 throw new RuntimeException(e);
127             }
128         }
129     }
130 
131 }