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 package nsk.jvmti.scenarios.sampling.SP05;
25 
26 import java.io.PrintStream;
27 
28 import nsk.share.*;
29 import nsk.share.jvmti.*;
30 
31 public class sp05t003 extends DebugeeClass {
32 
33     // load native library if required
34     static {
35         System.loadLibrary("sp05t003");
36     }
37 
38     // run test from command line
main(String argv[])39     public static void main(String argv[]) {
40         argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
41 
42         // JCK-compatible exit
43         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
44     }
45 
46     // run test from JCK-compatible environment
run(String argv[], PrintStream out)47     public static int run(String argv[], PrintStream out) {
48         return new sp05t003().runIt(argv, out);
49     }
50 
51     /* =================================================================== */
52 
53     // scaffold objects
54     ArgumentHandler argHandler = null;
55     Log log = null;
56     int status = Consts.TEST_PASSED;
57 
58     // monitors for threads synchronization
59     static Object endingMonitor = new Object();
60 
61     // tested threads list
62     static sp05t003Thread threads[] = null;
63 
64     // run debuggee
runIt(String argv[], PrintStream out)65     public int runIt(String argv[], PrintStream out) {
66         argHandler = new ArgumentHandler(argv);
67         log = new Log(out, argHandler);
68 
69         // create threads list
70         threads = new sp05t003Thread[] {
71             new sp05t003ThreadRunningJava(),
72             new sp05t003ThreadRunningNative()
73         };
74 
75         // sync for prepare data
76         log.display("Sync: threads created");
77         status = checkStatus(status);
78 
79         // run threads
80         try {
81 
82             // start threads
83             log.display("Starting tested threads");
84             for (int i = 0; i < threads.length; i++) {
85                 threads[i].start();
86             }
87 
88             // check threads started
89             for (int i = 0; i < threads.length; i++) {
90                 if (!threads[i].checkStarted()) {
91                     throw new Failure("Unable to prepare thread #" + i + ": " + threads[i]);
92                 }
93             }
94 
95             // sync after thread started
96             log.display("Sync: threads started");
97             status = checkStatus(status);
98 
99             // let threads to finish
100         } finally {
101             for (int i = 0; i < threads.length; i++) {
102                 threads[i].letFinish();
103             }
104         }
105 
106         // wait for all threads to finish
107         log.display("Finishing tested threads");
108         try {
109             for (int i = 0; i < threads.length; i++) {
110                 threads[i].join();
111             }
112         } catch (InterruptedException e) {
113             throw new Failure(e);
114         }
115 
116         // sync after thread finished
117         log.display("Sync: threads finished");
118         status = checkStatus(status);
119 
120         return status;
121     }
122 }
123 
124 /* =================================================================== */
125 
126 // basic class for tested threads
127 abstract class sp05t003Thread extends Thread {
128     // check if thread started
checkStarted()129     public abstract boolean checkStarted();
130 
131     // let thread to finish
letFinish()132     public abstract void letFinish();
133 }
134 
135 /* =================================================================== */
136 
137 class sp05t003ThreadRunningJava extends sp05t003Thread {
138     private volatile boolean hasStarted = false;
139     private volatile boolean shouldFinish = false;
140 
run()141     public void run() {
142         hasStarted = true;
143 
144         // run in a loop
145         int i = 0;
146         int n = 1000;
147         while (!shouldFinish) {
148             if (n <= 0) {
149                 n = 1000;
150             }
151             if (i > n) {
152                 i = 0;
153                 n = n - 1;
154             }
155             i = i + 1;
156         }
157     }
158 
checkStarted()159     public boolean checkStarted() {
160         try {
161             while(!hasStarted) {
162                 sleep(1000);
163             }
164         } catch (InterruptedException e) {
165             throw new Failure("Interrupted while waiting for thread started:\n\t" + e);
166         }
167         return hasStarted;
168     }
169 
letFinish()170     public void letFinish() {
171         shouldFinish = true;
172     }
173 }
174 
175 class sp05t003ThreadRunningNative extends sp05t003Thread {
run()176     public native void run();
checkStarted()177     public native boolean checkStarted();
letFinish()178     public native void letFinish();
179 }
180