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.MonitorContendedEntered;
25 
26 import java.io.PrintStream;
27 
28 import nsk.share.*;
29 import nsk.share.jvmti.*;
30 
31 public class mcontentered001 extends DebugeeClass {
32 
33     // load native library if required
34     static {
35         loadLibrary("mcontentered001");
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 mcontentered001().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     long timeout = 0;
58 
59     // tested thread
60     mcontentered001Thread thread = null;
61 
62     // run debuggee
runIt(String argv[], PrintStream out)63     public int runIt(String argv[], PrintStream out) {
64         argHandler = new ArgumentHandler(argv);
65         log = new Log(out, argHandler);
66         timeout = argHandler.getWaitTime() * 60000; // milliseconds
67         log.display("Timeout = " + timeout + " msc.");
68 
69         thread = new mcontentered001Thread("Debuggee Thread");
70 
71         synchronized (thread.endingMonitor) {
72 
73             // run thread
74             try {
75                 // start thread
76                 synchronized (thread.startingMonitor) {
77                     thread.start();
78                     thread.startingMonitor.wait(timeout);
79                 }
80             } catch (InterruptedException e) {
81                 throw new Failure(e);
82             }
83 
84             int totalDelay = 0;
85             while (getEventCount() < 1 && totalDelay < timeout) {
86                 try {
87                     Thread.sleep(100);
88                 } catch (InterruptedException e) {
89                     throw new Failure(e);
90                 }
91                 totalDelay += 100;
92             }
93 
94             Thread.yield();
95             log.display("Thread started");
96         }
97 
98         // wait for thread finish
99         try {
100             thread.join(timeout);
101         } catch (InterruptedException e) {
102             throw new Failure(e);
103         }
104 
105         log.display("Sync: thread finished");
106         status = checkStatus(status);
107 
108         return status;
109     }
110 
getEventCount()111     private native int getEventCount();
112 }
113 
114 /* =================================================================== */
115 
116 class mcontentered001Thread extends Thread {
117     public Object startingMonitor = new Object();
118     public Object endingMonitor = new Object();
119 
mcontentered001Thread(String name)120     public mcontentered001Thread(String name) {
121         super(name);
122     }
123 
run()124     public void run() {
125 
126         mcontentered001.checkStatus(Consts.TEST_PASSED);
127 
128         // notify about starting
129         synchronized (startingMonitor) {
130             startingMonitor.notify();
131         }
132 
133         // wait until main thread release monitor
134         synchronized (endingMonitor) {
135             endingMonitor.notify();
136         }
137     }
138 }
139