1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. 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 HandshakeWalkSuspendExitTest
  27  * @summary This test tries to stress the handshakes with new and exiting threads while suspending them.
  28  * @library /testlibrary /test/lib
  29  * @build HandshakeWalkSuspendExitTest
  30  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI HandshakeWalkSuspendExitTest
  33  */
  34 
  35 import jdk.test.lib.Asserts;
  36 import sun.hotspot.WhiteBox;
  37 
  38 public class HandshakeWalkSuspendExitTest  implements Runnable {
  39 
  40     static final int _test_threads = 8;
  41     static final int _test_exit_threads = 128;
  42     static Thread[] _threads = new Thread[_test_threads];
  43     static volatile boolean exit_now = false;
  44     static java.util.concurrent.Semaphore _sem = new java.util.concurrent.Semaphore(0);
  45 
  46     @Override
  47     public void run() {
  48         WhiteBox wb = WhiteBox.getWhiteBox();
  49         while(!exit_now) {
  50             _sem.release();
  51             for (int i = 0; i < _threads.length; i+=2) {
  52                 wb.handshakeWalkStack(null, true);
  53                 if (Thread.currentThread() != _threads[i]) {
  54                     _threads[i].suspend();
  55                     _threads[i].resume();
  56                 }
  57             }
  58             for (int i = 0; i < _threads.length; i+=2) {
  59                 wb.handshakeWalkStack(_threads[i], false);
  60                 if (Thread.currentThread() != _threads[i]) {
  61                     _threads[i].suspend();
  62                     _threads[i].resume();
  63                 }
  64             }
  65         }
  66     }
  67 
  68     public static void main(String... args) throws Exception {
  69         HandshakeWalkSuspendExitTest test = new HandshakeWalkSuspendExitTest();
  70 
  71         for (int i = 0; i < _threads.length; i++) {
  72             _threads[i] = new Thread(test);
  73             _threads[i].start();
  74         }
  75         for (int i = 0; i < _test_threads; i++) {
  76             _sem.acquire();
  77         }
  78         for (int i = 0; i < _test_exit_threads; i++) {
  79             new Thread(new Runnable() { public void run() {} }).start();
  80         }
  81         exit_now = true;
  82         for (int i = 0; i < _threads.length; i++) {
  83             _threads[i].join();
  84         }
  85     }
  86 }