1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 /*
  25  * @test TestVerifyJCStress
  26  * @summary Tests that we pass at least one jcstress-like test with all verification turned on
  27  * @key gc
  28  *
  29  * @run main/othervm  -Xmx1g -Xms1g -XX:+IgnoreUnrecognizedVMOptions
  30  *                    -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+UseShenandoahGC
  31  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyObjectEquals
  32  *                    -XX:ShenandoahGCHeuristics=passive -XX:+ShenandoahDegeneratedGC
  33  *                    TestVerifyJCStress
  34  *
  35  * @run main/othervm  -Xmx1g -Xms1g -XX:+IgnoreUnrecognizedVMOptions
  36  *                    -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+UseShenandoahGC
  37  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyObjectEquals
  38  *                    -XX:ShenandoahGCHeuristics=passive -XX:-ShenandoahDegeneratedGC
  39  *                    TestVerifyJCStress
  40  *
  41  * @run main/othervm  -Xmx1g -Xms1g -XX:+IgnoreUnrecognizedVMOptions
  42  *                    -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+UseShenandoahGC
  43  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyObjectEquals -XX:+ShenandoahVerifyOptoBarriers
  44  *                    -XX:ShenandoahGCHeuristics=adaptive
  45  *                    TestVerifyJCStress
  46  *
  47  * @run main/othervm  -Xmx1g -Xms1g -XX:+IgnoreUnrecognizedVMOptions
  48  *                    -XX:+UnlockExperimentalVMOptions -XX:+UnlockDiagnosticVMOptions -XX:+UseShenandoahGC
  49  *                    -XX:+ShenandoahStoreCheck -XX:+ShenandoahVerify -XX:+VerifyObjectEquals -XX:+ShenandoahVerifyOptoBarriers
  50  *                    -XX:ShenandoahGCHeuristics=static
  51  *                    TestVerifyJCStress
  52  */
  53 
  54 import java.util.*;
  55 import java.util.concurrent.*;
  56 import java.util.concurrent.locks.*;
  57 
  58 public class TestVerifyJCStress {
  59 
  60     public static void main(String[] args) throws Exception {
  61         ExecutorService service = Executors.newFixedThreadPool(
  62                 2,
  63                 r -> {
  64                     Thread t = new Thread(r);
  65                     t.setDaemon(true);
  66                     return t;
  67                 }
  68         );
  69 
  70         for (int c = 0; c < 10000; c++) {
  71             final Test[] tests = new Test[10000];
  72             for (int t = 0; t < tests.length; t++) {
  73                 tests[t] = new Test();
  74             }
  75 
  76             Future<?> f1 = service.submit(() -> {
  77                 IntResult2 r = new IntResult2();
  78                 for (Test test : tests) {
  79                     test.RL_Us(r);
  80                 }
  81             });
  82             Future<?> f2 = service.submit(() -> {
  83                 for (Test test : tests) {
  84                     test.WLI_Us();
  85                 }
  86             });
  87 
  88             f1.get();
  89             f2.get();
  90         }
  91     }
  92 
  93     public static class IntResult2 {
  94         int r1, r2;
  95     }
  96 
  97     public static class Test {
  98         final StampedLock lock = new StampedLock();
  99 
 100         int x, y;
 101 
 102         public void RL_Us(IntResult2 r) {
 103             StampedLock lock = this.lock;
 104             long stamp = lock.readLock();
 105             r.r1 = x;
 106             r.r2 = y;
 107             lock.unlock(stamp);
 108         }
 109 
 110         public void WLI_Us() {
 111             try {
 112                 StampedLock lock = this.lock;
 113                 long stamp = lock.writeLockInterruptibly();
 114                 x = 1;
 115                 y = 2;
 116                 lock.unlock(stamp);
 117             } catch (InterruptedException e) {
 118                 throw new RuntimeException(e);
 119             }
 120         }
 121     }
 122 
 123 }