2008-08-08 02:37:18 +02:00
|
|
|
/*
|
2008-08-08 03:27:24 +02:00
|
|
|
* JBoss, Home of Professional Open Source
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
|
|
|
|
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
|
|
|
* full listing of individual contributors.
|
2008-08-08 02:37:18 +02:00
|
|
|
*
|
2008-08-08 03:27:24 +02:00
|
|
|
* This is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful,
|
2008-08-08 02:37:18 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-08 03:27:24 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2008-08-08 02:37:18 +02:00
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2008-08-08 03:27:24 +02:00
|
|
|
* License along with this software; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
2008-08-08 02:37:18 +02:00
|
|
|
*/
|
2008-08-08 03:40:10 +02:00
|
|
|
package org.jboss.netty.channel;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2008-11-07 04:00:19 +01:00
|
|
|
import static java.util.concurrent.TimeUnit.*;
|
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2008-08-09 16:52:19 +02:00
|
|
|
import org.jboss.netty.logging.InternalLogger;
|
2008-08-09 17:05:53 +02:00
|
|
|
import org.jboss.netty.logging.InternalLoggerFactory;
|
2009-04-06 09:09:11 +02:00
|
|
|
import org.jboss.netty.util.internal.IoWorkerRunnable;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
2008-08-11 09:33:19 +02:00
|
|
|
/**
|
2008-09-02 09:13:20 +02:00
|
|
|
* The default {@link ChannelFuture} implementation. It is recommended to
|
|
|
|
* use {@link Channels#future(Channel)} and {@link Channels#future(Channel, boolean)}
|
|
|
|
* to create a new {@link ChannelFuture} rather than calling the constructor
|
|
|
|
* explicitly.
|
2008-08-11 09:33:19 +02:00
|
|
|
*
|
|
|
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
|
|
* @author Trustin Lee (tlee@redhat.com)
|
|
|
|
*
|
|
|
|
* @version $Rev$, $Date$
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
public class DefaultChannelFuture implements ChannelFuture {
|
|
|
|
|
2008-08-09 16:52:19 +02:00
|
|
|
private static final InternalLogger logger =
|
2008-08-09 17:05:53 +02:00
|
|
|
InternalLoggerFactory.getInstance(DefaultChannelFuture.class);
|
2008-08-11 09:33:19 +02:00
|
|
|
|
2008-08-08 02:37:18 +02:00
|
|
|
private static final Throwable CANCELLED = new Throwable();
|
|
|
|
|
|
|
|
private final Channel channel;
|
|
|
|
private final boolean cancellable;
|
|
|
|
|
2008-11-07 03:35:04 +01:00
|
|
|
private volatile ChannelFutureListener firstListener;
|
|
|
|
private volatile List<ChannelFutureListener> otherListeners;
|
2008-08-08 02:37:18 +02:00
|
|
|
private boolean done;
|
|
|
|
private Throwable cause;
|
|
|
|
private int waiters;
|
|
|
|
|
2008-08-11 09:33:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*
|
|
|
|
* @param channel
|
|
|
|
* the {@link Channel} associated with this future
|
|
|
|
* @param cancellable
|
|
|
|
* {@code true} if and only if this future can be canceled
|
|
|
|
*/
|
2008-08-08 02:37:18 +02:00
|
|
|
public DefaultChannelFuture(Channel channel, boolean cancellable) {
|
|
|
|
this.channel = channel;
|
|
|
|
this.cancellable = cancellable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Channel getChannel() {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized boolean isDone() {
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized boolean isSuccess() {
|
2009-02-26 10:28:37 +01:00
|
|
|
return done && cause == null;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized Throwable getCause() {
|
|
|
|
if (cause != CANCELLED) {
|
|
|
|
return cause;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized boolean isCancelled() {
|
|
|
|
return cause == CANCELLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addListener(ChannelFutureListener listener) {
|
|
|
|
if (listener == null) {
|
|
|
|
throw new NullPointerException("listener");
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean notifyNow = false;
|
|
|
|
synchronized (this) {
|
|
|
|
if (done) {
|
|
|
|
notifyNow = true;
|
|
|
|
} else {
|
|
|
|
if (firstListener == null) {
|
|
|
|
firstListener = listener;
|
|
|
|
} else {
|
|
|
|
if (otherListeners == null) {
|
|
|
|
otherListeners = new ArrayList<ChannelFutureListener>(1);
|
|
|
|
}
|
|
|
|
otherListeners.add(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notifyNow) {
|
|
|
|
notifyListener(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeListener(ChannelFutureListener listener) {
|
|
|
|
if (listener == null) {
|
|
|
|
throw new NullPointerException("listener");
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
if (!done) {
|
|
|
|
if (listener == firstListener) {
|
|
|
|
if (otherListeners != null && !otherListeners.isEmpty()) {
|
|
|
|
firstListener = otherListeners.remove(0);
|
|
|
|
} else {
|
|
|
|
firstListener = null;
|
|
|
|
}
|
|
|
|
} else if (otherListeners != null) {
|
|
|
|
otherListeners.remove(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelFuture await() throws InterruptedException {
|
|
|
|
synchronized (this) {
|
|
|
|
while (!done) {
|
2009-04-06 09:09:11 +02:00
|
|
|
checkDeadLock();
|
2008-08-08 02:37:18 +02:00
|
|
|
waiters++;
|
|
|
|
try {
|
2008-08-19 12:21:04 +02:00
|
|
|
this.wait();
|
2008-08-08 02:37:18 +02:00
|
|
|
} finally {
|
|
|
|
waiters--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean await(long timeout, TimeUnit unit)
|
|
|
|
throws InterruptedException {
|
2008-11-07 04:00:19 +01:00
|
|
|
return await0(unit.toNanos(timeout), true);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean await(long timeoutMillis) throws InterruptedException {
|
2008-11-07 04:00:19 +01:00
|
|
|
return await0(MILLISECONDS.toNanos(timeoutMillis), true);
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ChannelFuture awaitUninterruptibly() {
|
|
|
|
synchronized (this) {
|
|
|
|
while (!done) {
|
2009-04-06 09:09:11 +02:00
|
|
|
checkDeadLock();
|
2008-08-08 02:37:18 +02:00
|
|
|
waiters++;
|
|
|
|
try {
|
2008-08-19 12:21:04 +02:00
|
|
|
this.wait();
|
2008-08-08 02:37:18 +02:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
// Ignore.
|
|
|
|
} finally {
|
|
|
|
waiters--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean awaitUninterruptibly(long timeout, TimeUnit unit) {
|
2008-11-07 04:00:19 +01:00
|
|
|
try {
|
|
|
|
return await0(unit.toNanos(timeout), false);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new InternalError();
|
|
|
|
}
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean awaitUninterruptibly(long timeoutMillis) {
|
|
|
|
try {
|
2008-11-07 04:00:19 +01:00
|
|
|
return await0(MILLISECONDS.toNanos(timeoutMillis), false);
|
2008-08-08 02:37:18 +02:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
throw new InternalError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-07 04:00:19 +01:00
|
|
|
private boolean await0(long timeoutNanos, boolean interruptable) throws InterruptedException {
|
|
|
|
long startTime = timeoutNanos <= 0 ? 0 : System.nanoTime();
|
|
|
|
long waitTime = timeoutNanos;
|
2008-08-08 02:37:18 +02:00
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
if (done) {
|
|
|
|
return done;
|
|
|
|
} else if (waitTime <= 0) {
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2009-04-06 09:09:11 +02:00
|
|
|
checkDeadLock();
|
2008-08-08 02:37:18 +02:00
|
|
|
waiters++;
|
|
|
|
try {
|
|
|
|
for (;;) {
|
|
|
|
try {
|
2008-11-07 04:00:19 +01:00
|
|
|
this.wait(waitTime / 1000000, (int) (waitTime % 1000000));
|
2008-08-08 02:37:18 +02:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
if (interruptable) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2008-11-07 04:00:19 +01:00
|
|
|
waitTime = timeoutNanos - (System.nanoTime() - startTime);
|
2008-08-08 02:37:18 +02:00
|
|
|
if (waitTime <= 0) {
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
waiters--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-06 09:09:11 +02:00
|
|
|
private void checkDeadLock() {
|
|
|
|
if (IoWorkerRunnable.IN_IO_THREAD.get()) {
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"await*() in I/O thread causes a dead lock or " +
|
2009-04-23 09:14:27 +02:00
|
|
|
"sudden performance drop. Use addListener() instead or " +
|
|
|
|
"call await*() from a different thread.");
|
2009-04-06 09:09:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-26 10:21:00 +01:00
|
|
|
public boolean setSuccess() {
|
2008-08-08 02:37:18 +02:00
|
|
|
synchronized (this) {
|
|
|
|
// Allow only once.
|
|
|
|
if (done) {
|
2008-11-26 10:21:00 +01:00
|
|
|
return false;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
done = true;
|
|
|
|
if (waiters > 0) {
|
2008-11-26 10:21:00 +01:00
|
|
|
notifyAll();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
2008-11-26 10:21:00 +01:00
|
|
|
return true;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
2008-11-26 10:21:00 +01:00
|
|
|
public boolean setFailure(Throwable cause) {
|
2008-08-08 02:37:18 +02:00
|
|
|
synchronized (this) {
|
|
|
|
// Allow only once.
|
|
|
|
if (done) {
|
2008-11-26 10:21:00 +01:00
|
|
|
return false;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.cause = cause;
|
|
|
|
done = true;
|
|
|
|
if (waiters > 0) {
|
2008-11-26 10:21:00 +01:00
|
|
|
notifyAll();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
2008-11-26 10:21:00 +01:00
|
|
|
return true;
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean cancel() {
|
|
|
|
if (!cancellable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (this) {
|
|
|
|
// Allow only once.
|
|
|
|
if (done) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cause = CANCELLED;
|
|
|
|
done = true;
|
|
|
|
if (waiters > 0) {
|
2008-11-26 10:21:00 +01:00
|
|
|
notifyAll();
|
2008-08-08 02:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyListeners();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyListeners() {
|
|
|
|
// There won't be any visibility problem or concurrent modification
|
|
|
|
// because 'ready' flag will be checked against both addListener and
|
|
|
|
// removeListener calls.
|
|
|
|
if (firstListener != null) {
|
|
|
|
notifyListener(firstListener);
|
|
|
|
firstListener = null;
|
|
|
|
|
|
|
|
if (otherListeners != null) {
|
|
|
|
for (ChannelFutureListener l: otherListeners) {
|
|
|
|
notifyListener(l);
|
|
|
|
}
|
|
|
|
otherListeners = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyListener(ChannelFutureListener l) {
|
|
|
|
try {
|
|
|
|
l.operationComplete(this);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
logger.warn(
|
|
|
|
"An exception was thrown by " +
|
|
|
|
ChannelFutureListener.class.getSimpleName() + ".", t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|