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 package nsk.jvmti.GetThreadState;
25 
26 import java.io.PrintStream;
27 
28 public class thrstat001 {
29 
30     public static final int STATUS_RUNNING = 0;
31     public static final int STATUS_MONITOR = 1;
32     public static final int STATUS_WAIT    = 2;
33 
checkStatus(int statInd)34     native static void checkStatus(int statInd);
getRes()35     native static int getRes();
36 
main(String[] args)37     public static void main(String[] args) {
38         args = nsk.share.jvmti.JVMTITest.commonInit(args);
39 
40         System.exit(run(args, System.out) + 95/*STATUS_TEMP*/);
41     }
42 
run(String argv[], PrintStream ref)43     public static int run(String argv[], PrintStream ref) {
44         thrstat001 t = new thrstat001();
45         t.meth();
46         return getRes();
47     }
48 
49     // barriers for testing thread status values
50     public static Lock startingMonitor = new Lock();
51     public static Object blockingMonitor = new Object();
52     public static Lock endingMonitor = new Lock();
53 
meth()54     void meth() {
55         thrstat001a thr = new thrstat001a("thr1");
56 
57         synchronized (blockingMonitor) {
58             synchronized (startingMonitor) {
59                 startingMonitor.val = 0;
60                 thr.start();
61                 while (startingMonitor.val == 0) {
62                     try {
63                         startingMonitor.wait();
64                     } catch (InterruptedException e) {
65                         throw new Error("Unexpected: " + e);
66                     }
67                 }
68             }
69             Thread.yield();
70             checkStatus(STATUS_MONITOR);
71         }
72 
73         synchronized (endingMonitor) {
74             checkStatus(STATUS_WAIT);
75             endingMonitor.val++;
76             endingMonitor.notify();
77         }
78 
79         try {
80             thr.join();
81         } catch (InterruptedException e) {
82             throw new Error("Unexpected: " + e);
83         }
84    }
85 
86     static class Lock {
87         public int val = 0;
88     }
89 }
90 
91 class thrstat001a extends Thread {
92 
thrstat001a(String name)93     public thrstat001a(String name) {
94         super(name);
95     }
96 
run()97     public void run() {
98         synchronized (thrstat001.endingMonitor) {
99             thrstat001.checkStatus(thrstat001.STATUS_RUNNING);
100             synchronized (thrstat001.startingMonitor) {
101                 thrstat001.startingMonitor.val++;
102                 thrstat001.startingMonitor.notifyAll();
103             }
104 
105             synchronized (thrstat001.blockingMonitor) {
106             }
107 
108             thrstat001.endingMonitor.val = 0;
109             while (thrstat001.endingMonitor.val == 0) {
110                 try {
111                     thrstat001.endingMonitor.wait();
112                 } catch (InterruptedException e) {
113                     throw new Error("Unexpected: " + e);
114                 }
115             }
116         }
117     }
118 }
119