2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.jdi;
|
|
27 |
|
45714
|
28 |
import java.util.LinkedList;
|
|
29 |
|
|
30 |
import com.sun.jdi.VMDisconnectedException;
|
|
31 |
import com.sun.jdi.VirtualMachine;
|
2
|
32 |
import com.sun.jdi.event.EventQueue;
|
|
33 |
import com.sun.jdi.event.EventSet;
|
|
34 |
|
|
35 |
public class EventQueueImpl extends MirrorImpl implements EventQueue {
|
|
36 |
|
|
37 |
/*
|
|
38 |
* Note this is not a synchronized list. Iteration/update should be
|
|
39 |
* protected through the 'this' monitor.
|
|
40 |
*/
|
45714
|
41 |
LinkedList<EventSet> eventSets = new LinkedList<>();
|
2
|
42 |
|
|
43 |
TargetVM target;
|
|
44 |
boolean closed = false;
|
|
45 |
|
|
46 |
EventQueueImpl(VirtualMachine vm, TargetVM target) {
|
|
47 |
super(vm);
|
|
48 |
this.target = target;
|
|
49 |
target.addEventQueue(this);
|
|
50 |
}
|
|
51 |
|
|
52 |
/*
|
|
53 |
* Override superclass back to default equality
|
|
54 |
*/
|
|
55 |
public boolean equals(Object obj) {
|
|
56 |
return this == obj;
|
|
57 |
}
|
|
58 |
|
|
59 |
public int hashCode() {
|
|
60 |
return System.identityHashCode(this);
|
|
61 |
}
|
|
62 |
|
|
63 |
synchronized void enqueue(EventSet eventSet) {
|
|
64 |
eventSets.add(eventSet);
|
|
65 |
notifyAll();
|
|
66 |
}
|
|
67 |
|
|
68 |
synchronized int size() {
|
|
69 |
return eventSets.size();
|
|
70 |
}
|
|
71 |
|
|
72 |
synchronized void close() {
|
|
73 |
if (!closed) {
|
|
74 |
closed = true; // OK for this the be first since synchronized
|
|
75 |
|
|
76 |
// place VMDisconnectEvent into queue
|
|
77 |
enqueue(new EventSetImpl(vm,
|
|
78 |
(byte)JDWP.EventKind.VM_DISCONNECTED));
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
public EventSet remove() throws InterruptedException {
|
|
83 |
return remove(0);
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Filter out events not for user's eyes.
|
|
88 |
* Then filter out empty sets.
|
|
89 |
*/
|
|
90 |
public EventSet remove(long timeout) throws InterruptedException {
|
|
91 |
if (timeout < 0) {
|
|
92 |
throw new IllegalArgumentException("Timeout cannot be negative");
|
|
93 |
}
|
|
94 |
|
|
95 |
EventSet eventSet;
|
|
96 |
while (true) {
|
|
97 |
EventSetImpl fullEventSet = removeUnfiltered(timeout);
|
|
98 |
if (fullEventSet == null) {
|
|
99 |
eventSet = null; // timeout
|
|
100 |
break;
|
|
101 |
}
|
|
102 |
/*
|
|
103 |
* Remove events from the event set for which
|
|
104 |
* there is no corresponding enabled request (
|
|
105 |
* this includes our internally requested events.)
|
|
106 |
* This never returns null
|
|
107 |
*/
|
|
108 |
eventSet = fullEventSet.userFilter();
|
|
109 |
if (!eventSet.isEmpty()) {
|
|
110 |
break;
|
|
111 |
}
|
|
112 |
}
|
|
113 |
|
|
114 |
if ((eventSet != null) && (eventSet.suspendPolicy() == JDWP.SuspendPolicy.ALL)) {
|
|
115 |
vm.notifySuspend();
|
|
116 |
}
|
|
117 |
|
|
118 |
return eventSet;
|
|
119 |
}
|
|
120 |
|
|
121 |
EventSet removeInternal() throws InterruptedException {
|
|
122 |
EventSet eventSet;
|
|
123 |
do {
|
|
124 |
// Waiting forever, so removeUnfiltered() is never null
|
|
125 |
eventSet = removeUnfiltered(0).internalFilter();
|
|
126 |
} while (eventSet == null || eventSet.isEmpty());
|
|
127 |
|
|
128 |
/*
|
|
129 |
* Currently, no internal events are requested with a suspend
|
|
130 |
* policy other than none, so we don't check for notifySuspend()
|
|
131 |
* here. If this changes in the future, there is much
|
|
132 |
* infrastructure that needs to be updated.
|
|
133 |
*/
|
|
134 |
|
|
135 |
return eventSet;
|
|
136 |
}
|
|
137 |
|
|
138 |
private TimerThread startTimerThread(long timeout) {
|
|
139 |
TimerThread thread = new TimerThread(timeout);
|
|
140 |
thread.setDaemon(true);
|
|
141 |
thread.start();
|
|
142 |
return thread;
|
|
143 |
}
|
|
144 |
|
|
145 |
private boolean shouldWait(TimerThread timerThread) {
|
|
146 |
return !closed && eventSets.isEmpty() &&
|
|
147 |
((timerThread == null) ? true : !timerThread.timedOut());
|
|
148 |
}
|
|
149 |
|
|
150 |
private EventSetImpl removeUnfiltered(long timeout)
|
|
151 |
throws InterruptedException {
|
|
152 |
EventSetImpl eventSet = null;
|
|
153 |
|
|
154 |
/*
|
|
155 |
* Make sure the VM has completed initialization before
|
|
156 |
* trying to build events.
|
|
157 |
*/
|
|
158 |
vm.waitInitCompletion();
|
|
159 |
|
|
160 |
synchronized(this) {
|
|
161 |
if (!eventSets.isEmpty()) {
|
|
162 |
/*
|
|
163 |
* If there's already something there, no need
|
|
164 |
* for anything elaborate.
|
|
165 |
*/
|
|
166 |
eventSet = (EventSetImpl)eventSets.removeFirst();
|
|
167 |
} else {
|
|
168 |
/*
|
|
169 |
* If a timeout was specified, create a thread to
|
|
170 |
* notify this one when a timeout
|
|
171 |
* occurs. We can't use the timed version of wait()
|
|
172 |
* because it is possible for multiple enqueue() calls
|
|
173 |
* before we see something in the eventSet queue
|
|
174 |
* (this is possible when multiple threads call
|
|
175 |
* remove() concurrently -- not a great idea, but
|
|
176 |
* it should be supported). Even if enqueue() did a
|
|
177 |
* notify() instead of notifyAll() we are not able to
|
|
178 |
* use a timed wait because there's no way to distinguish
|
|
179 |
* a timeout from a notify. That limitation implies a
|
|
180 |
* possible race condition between a timed out thread
|
|
181 |
* and a notified thread.
|
|
182 |
*/
|
|
183 |
TimerThread timerThread = null;
|
|
184 |
try {
|
|
185 |
if (timeout > 0) {
|
|
186 |
timerThread = startTimerThread(timeout);
|
|
187 |
}
|
|
188 |
|
|
189 |
while (shouldWait(timerThread)) {
|
|
190 |
this.wait();
|
|
191 |
}
|
|
192 |
} finally {
|
|
193 |
if ((timerThread != null) && !timerThread.timedOut()) {
|
|
194 |
timerThread.interrupt();
|
|
195 |
}
|
|
196 |
}
|
|
197 |
|
|
198 |
if (eventSets.isEmpty()) {
|
|
199 |
if (closed) {
|
|
200 |
throw new VMDisconnectedException();
|
|
201 |
}
|
|
202 |
} else {
|
|
203 |
eventSet = (EventSetImpl)eventSets.removeFirst();
|
|
204 |
}
|
|
205 |
}
|
|
206 |
}
|
|
207 |
|
|
208 |
// The build is synchronized on the event set, don't hold
|
|
209 |
// the queue lock.
|
|
210 |
if (eventSet != null) {
|
|
211 |
target.notifyDequeueEventSet();
|
|
212 |
eventSet.build();
|
|
213 |
}
|
|
214 |
return eventSet;
|
|
215 |
}
|
|
216 |
|
|
217 |
private class TimerThread extends Thread {
|
|
218 |
private boolean timedOut = false;
|
|
219 |
private long timeout;
|
|
220 |
|
|
221 |
TimerThread(long timeout) {
|
|
222 |
super(vm.threadGroupForJDI(), "JDI Event Queue Timer");
|
|
223 |
this.timeout = timeout;
|
|
224 |
}
|
|
225 |
|
|
226 |
boolean timedOut() {
|
|
227 |
return timedOut;
|
|
228 |
}
|
|
229 |
|
|
230 |
public void run() {
|
|
231 |
try {
|
|
232 |
Thread.sleep(timeout);
|
|
233 |
EventQueueImpl queue = EventQueueImpl.this;
|
|
234 |
synchronized(queue) {
|
|
235 |
timedOut = true;
|
|
236 |
queue.notifyAll();
|
|
237 |
}
|
|
238 |
} catch (InterruptedException e) {
|
|
239 |
// Exit without notifying
|
|
240 |
}
|
|
241 |
}
|
|
242 |
}
|
|
243 |
}
|