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