1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.core.future;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.concurrent.TimeUnit;
25
26 import org.apache.mina.core.polling.AbstractPollingIoProcessor;
27 import org.apache.mina.core.service.IoProcessor;
28 import org.apache.mina.core.session.IoSession;
29 import org.apache.mina.util.ExceptionMonitor;
30
31
32
33
34
35
36
37 public class DefaultIoFuture implements IoFuture {
38
39
40 private static final long DEAD_LOCK_CHECK_INTERVAL = 5000L;
41
42
43 private final IoSession session;
44
45
46 private final Object lock;
47
48
49
50 private IoFutureListener<?> firstListener;
51
52
53 private List<IoFutureListener<?>> otherListeners;
54
55 private Object result;
56
57
58 private boolean ready;
59
60
61 private int waiters;
62
63
64
65
66
67
68 public DefaultIoFuture(IoSession session) {
69 this.session = session;
70 this.lock = this;
71 }
72
73
74
75
76 public IoSession getSession() {
77 return session;
78 }
79
80
81
82
83 @Deprecated
84 public void join() {
85 awaitUninterruptibly();
86 }
87
88
89
90
91 @Deprecated
92 public boolean join(long timeoutMillis) {
93 return awaitUninterruptibly(timeoutMillis);
94 }
95
96
97
98
99 public IoFuture await() throws InterruptedException {
100 synchronized (lock) {
101 while (!ready) {
102 waiters++;
103
104 try {
105
106
107
108 lock.wait(DEAD_LOCK_CHECK_INTERVAL);
109 } finally {
110 waiters--;
111
112 if (!ready) {
113 checkDeadLock();
114 }
115 }
116 }
117 }
118
119 return this;
120 }
121
122
123
124
125 public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
126 return await0(unit.toMillis(timeout), true);
127 }
128
129
130
131
132 public boolean await(long timeoutMillis) throws InterruptedException {
133 return await0(timeoutMillis, true);
134 }
135
136
137
138
139 public IoFuture awaitUninterruptibly() {
140 try {
141 await0(Long.MAX_VALUE, false);
142 } catch (InterruptedException ie) {
143
144 }
145
146 return this;
147 }
148
149
150
151
152 public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
153 try {
154 return await0(unit.toMillis(timeout), false);
155 } catch (InterruptedException e) {
156 throw new InternalError();
157 }
158 }
159
160
161
162
163 public boolean awaitUninterruptibly(long timeoutMillis) {
164 try {
165 return await0(timeoutMillis, false);
166 } catch (InterruptedException e) {
167 throw new InternalError();
168 }
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184 private boolean await0(long timeoutMillis, boolean interruptable) throws InterruptedException {
185 long endTime = System.currentTimeMillis() + timeoutMillis;
186
187 if (endTime < 0) {
188 endTime = Long.MAX_VALUE;
189 }
190
191 synchronized (lock) {
192
193
194 if (ready||(timeoutMillis <= 0)) {
195 return ready;
196 }
197
198
199 waiters++;
200
201 try {
202 for (;;) {
203 try {
204 long timeOut = Math.min(timeoutMillis, DEAD_LOCK_CHECK_INTERVAL);
205
206
207
208
209 lock.wait(timeOut);
210 } catch (InterruptedException e) {
211 if (interruptable) {
212 throw e;
213 }
214 }
215
216 if (ready || (endTime < System.currentTimeMillis())) {
217 return ready;
218 } else {
219
220 checkDeadLock();
221 }
222 }
223 } finally {
224
225
226
227
228
229 waiters--;
230
231 if (!ready) {
232 checkDeadLock();
233 }
234 }
235 }
236 }
237
238
239
240
241
242 private void checkDeadLock() {
243
244 if (!(this instanceof CloseFuture || this instanceof WriteFuture || this instanceof ReadFuture || this instanceof ConnectFuture)) {
245 return;
246 }
247
248
249
250
251
252
253
254 StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
255
256
257 for (StackTraceElement stackElement : stackTrace) {
258 if (AbstractPollingIoProcessor.class.getName().equals(stackElement.getClassName())) {
259 IllegalStateException e = new IllegalStateException("t");
260 e.getStackTrace();
261 throw new IllegalStateException("DEAD LOCK: " + IoFuture.class.getSimpleName()
262 + ".await() was invoked from an I/O processor thread. " + "Please use "
263 + IoFutureListener.class.getSimpleName() + " or configure a proper thread model alternatively.");
264 }
265 }
266
267
268 for (StackTraceElement s : stackTrace) {
269 try {
270 Class<?> cls = DefaultIoFuture.class.getClassLoader().loadClass(s.getClassName());
271
272 if (IoProcessor.class.isAssignableFrom(cls)) {
273 throw new IllegalStateException("DEAD LOCK: " + IoFuture.class.getSimpleName()
274 + ".await() was invoked from an I/O processor thread. " + "Please use "
275 + IoFutureListener.class.getSimpleName()
276 + " or configure a proper thread model alternatively.");
277 }
278 } catch (ClassNotFoundException cnfe) {
279
280 }
281 }
282 }
283
284
285
286
287 public boolean isDone() {
288 synchronized (lock) {
289 return ready;
290 }
291 }
292
293
294
295
296
297
298
299
300 public boolean setValue(Object newValue) {
301 synchronized (lock) {
302
303 if (ready) {
304 return false;
305 }
306
307 result = newValue;
308 ready = true;
309
310
311 if (waiters > 0) {
312 lock.notifyAll();
313 }
314 }
315
316
317 notifyListeners();
318
319 return true;
320 }
321
322
323
324
325 protected Object getValue() {
326 synchronized (lock) {
327 return result;
328 }
329 }
330
331
332
333
334 public IoFuture addListener(IoFutureListener<?> listener) {
335 if (listener == null) {
336 throw new IllegalArgumentException("listener");
337 }
338
339 synchronized (lock) {
340 if (ready) {
341
342
343
344
345 notifyListener(listener);
346 } else {
347 if (firstListener == null) {
348 firstListener = listener;
349 } else {
350 if (otherListeners == null) {
351 otherListeners = new ArrayList<IoFutureListener<?>>(1);
352 }
353
354 otherListeners.add(listener);
355 }
356 }
357 }
358
359 return this;
360 }
361
362
363
364
365 public IoFuture removeListener(IoFutureListener<?> listener) {
366 if (listener == null) {
367 throw new IllegalArgumentException("listener");
368 }
369
370 synchronized (lock) {
371 if (!ready) {
372 if (listener == firstListener) {
373 if ((otherListeners != null) && !otherListeners.isEmpty()) {
374 firstListener = otherListeners.remove(0);
375 } else {
376 firstListener = null;
377 }
378 } else if (otherListeners != null) {
379 otherListeners.remove(listener);
380 }
381 }
382 }
383
384 return this;
385 }
386
387
388
389
390 private void notifyListeners() {
391
392
393
394 if (firstListener != null) {
395 notifyListener(firstListener);
396 firstListener = null;
397
398 if (otherListeners != null) {
399 for (IoFutureListener<?> listener : otherListeners) {
400 notifyListener(listener);
401 }
402
403 otherListeners = null;
404 }
405 }
406 }
407
408 @SuppressWarnings("unchecked")
409 private void notifyListener(IoFutureListener listener) {
410 try {
411 listener.operationComplete(this);
412 } catch (Exception e) {
413 ExceptionMonitor.getInstance().exceptionCaught(e);
414 }
415 }
416 }