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