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 /*
 25  * @test
 26  *
 27  * @summary converted from VM Testbase nsk/jvmti/ResumeThread/resumethrd001.
 28  * VM Testbase keywords: [quick, jpda, jvmti, noras]
 29  * VM Testbase readme:
 30  * DESCRIPTION
 31  *     This JVMTI test exercises JVMTI thread function ResumeThread().
 32  *     This tests checks that for thread resumed by ResumeThread()
 33  *     function GetThreadState() does not return JVMTI_THREAD_STATE_SUSPENDED.
 34  * COMMENTS
 35  *     Modified due to fix of the RFE
 36  *     5001769 TEST_RFE: remove usage of deprecated GetThreadStatus function
 37  *
 38  * @library /test/lib
 39  * @run main/othervm/native -agentlib:resumethrd01=-waittime=5 resumethrd01
 40  */
 41 
 42 import jdk.test.lib.jvmti.DebugeeClass;
 43 
 44 
 45 public class resumethrd01 extends DebugeeClass {
 46 
 47     // load native library if required
 48     static {
 49         System.loadLibrary("resumethrd01");
 50     }
 51 
 52     // run test from command line
 53     public static void main(String argv[]) throws Exception {
 54         new resumethrd01().runIt();
 55     }
 56 
 57     /* =================================================================== */
 58 
 59     long timeout = 0;
 60     int status = DebugeeClass.TEST_PASSED;
 61 
 62     // run debuggee
 63     public void runIt() throws Exception {
 64         timeout = 60 * 1000; // milliseconds
 65 
 66         // create tested thread
 67         TestedThread thread = new TestedThread("TestedThread");
 68 
 69         // run tested thread
 70         System.out.println("Staring tested thread");
 71         try {
 72             thread.start();
 73             thread.checkReady();
 74 
 75             // testing sync
 76             System.out.println("Sync: thread started");
 77             status = checkStatus(status);
 78         } finally {
 79             // let thread to finish
 80             thread.letFinish();
 81             // wait for thread to finish
 82             System.out.println("Finishing tested thread");
 83             thread.join();
 84         }
 85 
 86         // testing sync
 87         System.out.println("Sync: thread finished");
 88         status = checkStatus(status);
 89 
 90         if (status !=0) {
 91             throw new RuntimeException("status = " + status);
 92         }
 93     }
 94 }
 95 
 96 /* =================================================================== */
 97 
 98 // basic class for tested threads
 99 class TestedThread extends Thread {
100     private volatile boolean threadReady = false;
101     private volatile boolean shouldFinish = false;
102 
103     // make thread with specific name
104     public TestedThread(String name) {
105         super(name);
106     }
107 
108     // run thread continuously
109     public void run() {
110         // run in a loop
111         threadReady = true;
112         int i = 0;
113         int n = 1000;
114         while (!shouldFinish) {
115             if (n <= 0) {
116                 n = 1000;
117             }
118             if (i > n) {
119                 i = 0;
120                 n = n - 1;
121             }
122             i = i + 1;
123         }
124     }
125 
126     // check if thread is ready
127     public void checkReady() {
128         try {
129             while (!threadReady) {
130                 sleep(100);
131             }
132         } catch (InterruptedException e) {
133             throw new RuntimeException("Interruption while preparing tested thread: \n\t" + e);
134         }
135     }
136 
137     // let thread to finish
138     public void letFinish() {
139         shouldFinish = true;
140     }
141 }