1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
20 
21 import org.apache.harmony.jpda.tests.framework.DebuggeeSynchronizer;
22 import org.apache.harmony.jpda.tests.framework.LogWriter;
23 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
24 import org.apache.harmony.jpda.tests.share.SyncDebuggee;
25 
26 
27 /**
28  * The class specifies debuggee for <code>org.apache.harmony.jpda.tests.jdwp.ThreadReference.OwnedMonitorsStackDepthInfoTest</code>.
29  * This debuggee starts the tested thread <code>TESTED_THREAD</code> which
30  * synchronized with test via the <code>SGNL_READY</code> and
31  * <code>SGNL_CONTINUE</code> signals.
32  */
33 public class OwnedMonitorsStackDepthInfoDebuggee extends SyncDebuggee {
34 
35     public static final String TESTED_THREAD = "TestedThread";
36 
37     // These two objects are used
38     static Object waitForStart = new Object();
39     static Object waitForFinish = new Object();
40 
run()41     public void run() {
42         DebuggeeThread thrd = new DebuggeeThread(TESTED_THREAD,
43                 logWriter, synchronizer);
44 
45         synchronized(waitForStart){
46             thrd.start();
47             try {
48                 waitForStart.wait();
49             } catch (InterruptedException e) {
50 
51             }
52         }
53 
54         synchronized(waitForFinish){
55             logWriter.println("thread is finished");
56         }
57     }
58 
59     class DebuggeeThread extends Thread {
60 
61         LogWriter logWriter;
62         DebuggeeSynchronizer synchronizer;
63 
DebuggeeThread(String name, LogWriter logWriter, DebuggeeSynchronizer synchronizer)64         public DebuggeeThread(String name, LogWriter logWriter,
65                 DebuggeeSynchronizer synchronizer) {
66             super(name);
67             this.logWriter = logWriter;
68             this.synchronizer = synchronizer;
69         }
70 
71         // Deliberately make several stack frames with known monitor states.
72 
73         // 1. No monitors held in the outermost frame.
run()74         public void run() {
75             run1();
76         }
77 
78         // 2. One monitor ("this") held in the next frame.
run1()79         private synchronized void run1() {
80             run2();
81         }
82 
83         // 3. No monitors held in the next frame.
run2()84         private void run2() {
85             run3();
86         }
87 
88         // 4. Two monitors held in the frame after that.
run3()89         private void run3() {
90             synchronized(OwnedMonitorsStackDepthInfoDebuggee.waitForFinish){
91 
92                 synchronized(OwnedMonitorsStackDepthInfoDebuggee.waitForStart){
93 
94                     OwnedMonitorsStackDepthInfoDebuggee.waitForStart.notifyAll();
95 
96                     logWriter.println(getName() +  ": started");
97                     synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
98 
99                     logWriter.println(getName() +  ": wait for SGNL_CONTINUE");
100                     synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
101                     logWriter.println(getName() +  ": finished");
102                 }
103             }
104         }
105     }
106 
main(String [] args)107     public static void main(String [] args) {
108         runDebuggee(OwnedMonitorsStackDepthInfoDebuggee.class);
109     }
110 }
111