1 /*
  2  * Copyright (c) 2003, 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 import java.io.PrintStream;
 25 
 26 /*
 27  * @test
 28  *
 29  * @summary converted from VM Testbase nsk/jvmti/ThreadStart/threadstart001.
 30  * VM Testbase keywords: [quick, jpda, jvmti, noras, quarantine]
 31  * VM Testbase comments: 8016181
 32  * VM Testbase readme:
 33  * DESCRIPTION
 34  *     The test exercises JVMTI event callback function ThreadStart.
 35  *     The test checks if the event is ganerated by a new
 36  *     thread before its initial method executes.
 37  * COMMENTS
 38  *     Ported from JVMDI.
 39  *
 40  * @library /test/lib
 41  * @run main/othervm/native -agentlib:threadstart01 threadstart01
 42  */
 43 
 44 public class threadstart01 {
 45 
 46     final static int THREADS_LIMIT = 100;
 47     final static String NAME_PREFIX = "threadstart01-";
 48 
 49     static {
 50         try {
 51             System.loadLibrary("threadstart01");
 52         } catch (UnsatisfiedLinkError ule) {
 53             System.err.println("Could not load threadstart01 library");
 54             System.err.println("java.library.path:"
 55                 + System.getProperty("java.library.path"));
 56             throw ule;
 57         }
 58     }
 59 
 60     native static void getReady(int i, String name);
 61     native static int check();
 62 
 63     static volatile int thrCount = 0;
 64 
 65     public static void main(String args[]) {
 66         int result = run(args, System.out);
 67         if (result != 0) {
 68             throw new RuntimeException("Unexpected status: " + result);
 69         }
 70     }
 71 
 72     public static int run(String args[], PrintStream out) {
 73         TestThread t = new TestThread(NAME_PREFIX + thrCount);
 74         getReady(THREADS_LIMIT, NAME_PREFIX);
 75         t.start();
 76         try {
 77             t.join();
 78         } catch (InterruptedException e) {
 79             throw new Error("Unexpected: " + e);
 80         }
 81         return check();
 82     }
 83 
 84     static class TestThread extends Thread {
 85         public TestThread(String name) {
 86             super(name);
 87         }
 88         public void run() {
 89             thrCount++;
 90             if (thrCount < THREADS_LIMIT) {
 91                 TestThread t = new TestThread(NAME_PREFIX + thrCount);
 92                 t.start();
 93                 try {
 94                     t.join();
 95                 } catch (InterruptedException e) {
 96                     throw new Error("Unexpected: " + e);
 97                 }
 98             }
 99         }
100     }
101 }