1 /*
  2  * Copyright (c) 2004, 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 /*
 27  * @test
 28  *
 29  * @summary converted from VM Testbase nsk/jvmti/GetThreadState/thrstat002.
 30  * VM Testbase keywords: [quick, jpda, jvmti, onload_only_logic, noras, quarantine]
 31  * VM Testbase comments: 6260469
 32  * VM Testbase readme:
 33  * DESCRIPTION
 34  *     The test exercises JVMDI function GetThreadState.  Java program launches
 35  *     a thread and for various thread states calls Thread.suspend()/resume()
 36  *     methods or JVMDI functions SuspendThread/ResumeThread. Then native method
 37  *     checkStatus is invoked. This method calls GetThreadState and checks if
 38  *     the returned values are correct and JVMTI_THREAD_STATE_SUSPENDED bit
 39  *     is set (or clear after resume).
 40  *     The thread statuses are:
 41  *       - JVMTI_THREAD_STATE_RUNNABLE
 42  *               if thread is runnable
 43  *       - JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER
 44  *               if thread is waiting to enter sync. block
 45  *       - JVMTI_THREAD_STATE_IN_OBJECT_WAIT
 46  *               if thread is waiting by object.wait()
 47  *     Failing criteria for the test are:
 48  *       - values returned by GetThreadState are not the same as expected;
 49  *       - failure of used JVMTI functions.
 50  * COMMENTS
 51  *     Converted the test to use GetThreadState instead of GetThreadStatus.
 52  *     Fixed according to 4387521 and 4427103 bugs.
 53  *     Fixed according to 4463667 bug.
 54  *     Fixed according to 4669812 bug.
 55  *     Ported from JVMDI.
 56  *     Fixed according to 4925857 bug:
 57  *       - rearranged synchronization of tested thread
 58  *       - enhanced descripton
 59  *
 60  * @library /test/lib
 61  * @run main/othervm/native -agentlib:thrstat02 thrstat02 5
 62  */
 63 
 64 import java.io.PrintStream;
 65 import java.util.concurrent.CountDownLatch;
 66 
 67 public class thrstat02 {
 68 
 69     public static final int STATUS_RUNNING = 0;
 70     public static final int STATUS_MONITOR = 1;
 71     public static final int STATUS_WAIT    = 2;
 72 
 73     native static void init(int waitTime);
 74     native static boolean checkStatus0(int statInd, boolean suspended);
 75     static void checkStatus(int statInd, boolean suspended) {
 76         if (!checkStatus0(statInd, suspended)) {
 77             throw new RuntimeException("checkStatus failed");
 78         }
 79     }
 80 
 81     public static void main(String[] args) {
 82         int waitTime = 2;
 83         if (args.length > 0) {
 84             try {
 85                 int i  = Integer.parseInt(args[0]);
 86                 waitTime = i;
 87             } catch (NumberFormatException ex) {
 88                 System.out.println("# Wrong argument \"" + args[0]
 89                     + "\", the default value is used");
 90             }
 91         }
 92         System.out.println("# Waiting time = " + waitTime + " mins");
 93         init(waitTime);
 94         try {
 95             new thrstat02().meth();
 96         } catch (InterruptedException e) {
 97             e.printStackTrace();
 98             throw new RuntimeException(e);
 99         }
100     }
101 
102     public static CountDownLatch startingBarrier;
103     public static CountDownLatch runningBarrier;
104     public static Object blockingMonitor = new Object();
105     public static Lock endingMonitor = new Lock();
106 
107     static volatile boolean targetAboutToLock = false;
108 
109     void meth() throws InterruptedException {
110         thrstat02a thr = new thrstat02a("tested_thread_thr1");
111         startingBarrier = new CountDownLatch(1);
112         runningBarrier = new CountDownLatch(1);
113 
114         synchronized (blockingMonitor) {
115             thr.start();
116             System.out.println("thrstat02.meth after thr.start()");
117 
118             startingBarrier.await();
119             System.out.println("thrstat02.meth after thr.startingBarrier.waitFor()");
120 
121             waitForThreadBlocked(thr);
122 
123             checkStatus(STATUS_MONITOR, false);
124             System.out.println("thrstat02.meth after checkStatus(STATUS_MONITOR,false)");
125 
126             thr.suspend();
127             System.out.println("thrstat02.meth after thr.suspend()");
128             checkStatus(STATUS_MONITOR, true);
129             System.out.println("thrstat02.meth after checkStatus(STATUS_MONITOR,true)");
130 
131             thr.resume();
132             System.out.println("thrstat02.meth after thr.resume()");
133             checkStatus(STATUS_MONITOR, false);
134             System.out.println("thrstat02.meth after checkStatus(STATUS_MONITOR,false)");
135         }
136 
137         runningBarrier.await();
138         checkStatus(STATUS_RUNNING, false);
139         thr.suspend();
140         checkStatus(STATUS_RUNNING, true);
141         thr.resume();
142         checkStatus(STATUS_RUNNING, false);
143         thr.letItGo();
144 
145         synchronized (endingMonitor) {
146             checkStatus(STATUS_WAIT, false);
147             thr.suspend();
148             checkStatus(STATUS_WAIT, true);
149             thr.resume();
150             checkStatus(STATUS_WAIT, false);
151             endingMonitor.val++;
152             endingMonitor.notifyAll();
153         }
154 
155         try {
156             thr.join();
157         } catch (InterruptedException e) {
158             throw new Error("Unexpected: " + e);
159         }
160     }
161 
162     private static void waitForThreadBlocked(Thread t) {
163         // Ensure that the thread is blocked on the right monitor
164         while (!targetAboutToLock || t.getState() != Thread.State.BLOCKED) {
165             try {
166                 Thread.sleep(100);
167             } catch (InterruptedException ex) {
168                 System.out.println("thrstat02.waitForThreadBlocked was interrupted: " + ex.getMessage());
169             }
170         }
171     }
172 
173     static class Lock {
174         public int val = 0;
175     }
176 }
177 
178 class thrstat02a extends Thread {
179     private volatile boolean flag = true;
180 
181     public thrstat02a(String name) {
182         super(name);
183     }
184 
185     public void run() {
186         synchronized (thrstat02.endingMonitor) {
187             System.out.println("thrstat02a.run before startingBarrier.unlock");
188             thrstat02.startingBarrier.countDown();
189 
190             System.out.println("thrstat02a.run after  startingBarrier.unlock");
191 
192             System.out.println("thrstat02a.run before blockingMonitor lock");
193 
194             thrstat02.targetAboutToLock = true;
195 
196             synchronized (thrstat02.blockingMonitor) {
197                System.out.println("thrstat02a.run blockingMonitor locked");
198             }
199             System.out.println("thrstat02a.run after blockingMonitor lock");
200 
201             thrstat02.runningBarrier.countDown();
202             System.out.println("thrstat02a.run after runningBarrier unlock");
203             int i = 0;
204             int n = 1000;
205             while (flag) {
206                 if (n <= 0) {
207                     n = 1000;
208                 }
209                 if (i > n) {
210                     i = 0;
211                     n--;
212                 }
213                 i++;
214             }
215 
216             thrstat02.endingMonitor.val = 0;
217             while (thrstat02.endingMonitor.val == 0) {
218                 try {
219                     thrstat02.endingMonitor.wait();
220                 } catch (InterruptedException e) {
221                     throw new Error("Unexpected: " + e);
222                 }
223             }
224             System.out.println("thrstat02a.run before endingMonitor unlock");
225        }
226     }
227 
228     public void letItGo() {
229         flag = false;
230     }
231 }