Remove unnecessary inEventLoop() checks in Channel.Unsafe

.. because HeadHandler in the pipeline always ensures those methods are always invoked from the correct I/O thread
This commit is contained in:
Trustin Lee 2013-05-17 19:20:46 +09:00
parent 41f5d5650d
commit fd1d31e7d8
8 changed files with 287 additions and 410 deletions

View File

@ -15,17 +15,16 @@
*/ */
package io.netty.channel.rxtx; package io.netty.channel.rxtx;
import static io.netty.channel.rxtx.RxtxChannelOption.*; import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import io.netty.channel.ChannelPromise; import io.netty.channel.ChannelPromise;
import io.netty.channel.oio.OioByteStreamChannel; import io.netty.channel.oio.OioByteStreamChannel;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import gnu.io.CommPort; import static io.netty.channel.rxtx.RxtxChannelOption.*;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
/** /**
* A channel to a serial device using the RXTX library. * A channel to a serial device using the RXTX library.
@ -134,7 +133,6 @@ public class RxtxChannel extends OioByteStreamChannel {
public void connect( public void connect(
final SocketAddress remoteAddress, final SocketAddress remoteAddress,
final SocketAddress localAddress, final ChannelPromise promise) { final SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) { if (!ensureOpen(promise)) {
return; return;
} }
@ -171,14 +169,6 @@ public class RxtxChannel extends OioByteStreamChannel {
promise.setFailure(t); promise.setFailure(t);
closeIfClosed(); closeIfClosed();
} }
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
connect(remoteAddress, localAddress, promise);
}
});
}
} }
} }
} }

View File

@ -476,13 +476,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
} }
} }
private final Runnable beginReadTask = new Runnable() {
@Override
public void run() {
beginRead();
}
};
private final Runnable flushLaterTask = new Runnable() { private final Runnable flushLaterTask = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -495,8 +488,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override @Override
public final void sendFile(final FileRegion region, final ChannelPromise promise) { public final void sendFile(final FileRegion region, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (outboundBufSize() > 0) { if (outboundBufSize() > 0) {
flushNotifier(newPromise()).addListener(new ChannelFutureListener() { flushNotifier(newPromise()).addListener(new ChannelFutureListener() {
@Override @Override
@ -508,14 +499,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
// nothing pending try to send the fileRegion now! // nothing pending try to send the fileRegion now!
sendFile0(region, promise); sendFile0(region, promise);
} }
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
sendFile(region, promise);
}
});
}
} }
private void sendFile0(FileRegion region, ChannelPromise promise) { private void sendFile0(FileRegion region, ChannelPromise promise) {
@ -631,7 +614,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override @Override
public final void bind(final SocketAddress localAddress, final ChannelPromise promise) { public final void bind(final SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) { if (!ensureOpen(promise)) {
return; return;
} }
@ -661,19 +643,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
promise.setFailure(t); promise.setFailure(t);
closeIfClosed(); closeIfClosed();
} }
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
bind(localAddress, promise);
}
});
}
} }
@Override @Override
public final void disconnect(final ChannelPromise promise) { public final void disconnect(final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
try { try {
boolean wasActive = isActive(); boolean wasActive = isActive();
doDisconnect(); doDisconnect();
@ -690,19 +663,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
promise.setFailure(t); promise.setFailure(t);
closeIfClosed(); closeIfClosed();
} }
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
disconnect(promise);
}
});
}
} }
@Override @Override
public final void close(final ChannelPromise promise) { public final void close(final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
boolean wasActive = isActive(); boolean wasActive = isActive();
if (closeFuture.setClosed()) { if (closeFuture.setClosed()) {
try { try {
@ -732,14 +696,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
// Closed already. // Closed already.
promise.setSuccess(); promise.setSuccess();
} }
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
close(promise);
}
});
}
} }
@Override @Override
@ -753,7 +709,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override @Override
public final void deregister(final ChannelPromise promise) { public final void deregister(final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!registered) { if (!registered) {
promise.setSuccess(); promise.setSuccess();
return; return;
@ -785,14 +740,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
postTask.run(); postTask.run();
} }
} }
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
deregister(promise);
}
});
}
} }
@Override @Override
@ -801,7 +748,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
return; return;
} }
if (eventLoop().inEventLoop()) {
try { try {
doBeginRead(); doBeginRead();
} catch (final Exception e) { } catch (final Exception e) {
@ -813,16 +759,14 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
}); });
close(unsafe().voidFuture()); close(unsafe().voidFuture());
} }
} else {
eventLoop().execute(beginReadTask);
}
} }
@Override @Override
public void flush(final ChannelPromise promise) { public void flush(final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
FlushTask task = flushTaskInProgress; FlushTask task = flushTaskInProgress;
if (task != null) { if (task == null) {
flushNotifierAndFlush(promise);
} else {
// loop over the tasks to find the last one // loop over the tasks to find the last one
for (;;) { for (;;) {
FlushTask t = task.next; FlushTask t = task.next;
@ -832,17 +776,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
task = t.next; task = t.next;
} }
task.next = new FlushTask(null, promise); task.next = new FlushTask(null, promise);
return;
}
flushNotifierAndFlush(promise);
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
flush(promise);
}
});
} }
} }

View File

@ -87,32 +87,14 @@ public abstract class AbstractServerChannel extends AbstractChannel implements S
private final class DefaultServerUnsafe extends AbstractUnsafe { private final class DefaultServerUnsafe extends AbstractUnsafe {
@Override @Override
public void flush(final ChannelPromise future) { public void flush(final ChannelPromise future) {
if (eventLoop().inEventLoop()) {
reject(future); reject(future);
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
flush(future);
}
});
}
} }
@Override @Override
public void connect( public void connect(
final SocketAddress remoteAddress, final SocketAddress localAddress, final SocketAddress remoteAddress, final SocketAddress localAddress,
final ChannelPromise future) { final ChannelPromise future) {
if (eventLoop().inEventLoop()) {
reject(future); reject(future);
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
connect(remoteAddress, localAddress, future);
}
});
}
} }
private void reject(ChannelPromise future) { private void reject(ChannelPromise future) {

View File

@ -150,7 +150,7 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, ChannelPr
* {@code null} if this channel is not connected. * {@code null} if this channel is not connected.
* If this channel is not connected but it can receive messages * If this channel is not connected but it can receive messages
* from arbitrary remote addresses (e.g. {@link DatagramChannel}, * from arbitrary remote addresses (e.g. {@link DatagramChannel},
* use {@link DatagramPacket#remoteAddress()} to determine * use {@link DatagramPacket#recipient()} to determine
* the origination of the received message as this method will * the origination of the received message as this method will
* return {@code null}. * return {@code null}.
*/ */
@ -163,13 +163,22 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, ChannelPr
ChannelFuture closeFuture(); ChannelFuture closeFuture();
/** /**
* <strong>Caution</strong> for transport implementations use only! * Returns an <em>internal-use-only</em> object that provides unsafe operations.
*/ */
Unsafe unsafe(); Unsafe unsafe();
/** /**
* <strong>Unsafe</strong> operations that should <strong>never</strong> be called * <em>Unsafe</em> operations that should <em>never</em> be called from user-code. These methods
* from user-code. These methods are only provided to implement the actual transport. * are only provided to implement the actual transport, and must be invoked from an I/O thread except for the
* following methods:
* <ul>
* <li>{@link #headContext()}</li>
* <li>{@link #voidFuture()}</li>
* <li>{@link #localAddress()}</li>
* <li>{@link #remoteAddress()}</li>
* <li>{@link #closeForcibly()}</li>
* <li>{@link #register(EventLoop, ChannelPromise)}</li>
* </ul>
*/ */
interface Unsafe { interface Unsafe {
/** /**

View File

@ -94,7 +94,6 @@ public abstract class AbstractAioChannel extends AbstractChannel {
@Override @Override
public void connect(final SocketAddress remoteAddress, public void connect(final SocketAddress remoteAddress,
final SocketAddress localAddress, final ChannelPromise promise) { final SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) { if (!ensureOpen(promise)) {
return; return;
} }
@ -132,14 +131,6 @@ public abstract class AbstractAioChannel extends AbstractChannel {
promise.setFailure(t); promise.setFailure(t);
closeIfClosed(); closeIfClosed();
} }
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
connect(remoteAddress, localAddress, promise);
}
});
}
} }
public void connectFailed(Throwable t) { public void connectFailed(Throwable t) {

View File

@ -277,7 +277,6 @@ public class LocalChannel extends AbstractChannel {
@Override @Override
public void connect(final SocketAddress remoteAddress, public void connect(final SocketAddress remoteAddress,
SocketAddress localAddress, final ChannelPromise promise) { SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) { if (!ensureOpen(promise)) {
return; return;
} }
@ -323,15 +322,6 @@ public class LocalChannel extends AbstractChannel {
LocalServerChannel serverChannel = (LocalServerChannel) boundChannel; LocalServerChannel serverChannel = (LocalServerChannel) boundChannel;
peer = serverChannel.serve(LocalChannel.this); peer = serverChannel.serve(LocalChannel.this);
} else {
final SocketAddress localAddress0 = localAddress;
eventLoop().execute(new Runnable() {
@Override
public void run() {
connect(remoteAddress, localAddress0, promise);
}
});
}
} }
} }
} }

View File

@ -157,7 +157,6 @@ public abstract class AbstractNioChannel extends AbstractChannel {
@Override @Override
public void connect( public void connect(
final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) { final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) { if (!ensureOpen(promise)) {
return; return;
} }
@ -202,14 +201,6 @@ public abstract class AbstractNioChannel extends AbstractChannel {
promise.setFailure(t); promise.setFailure(t);
closeIfClosed(); closeIfClosed();
} }
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
connect(remoteAddress, localAddress, promise);
}
});
}
} }
@Override @Override

View File

@ -58,7 +58,6 @@ public abstract class AbstractOioChannel extends AbstractChannel {
public void connect( public void connect(
final SocketAddress remoteAddress, final SocketAddress remoteAddress,
final SocketAddress localAddress, final ChannelPromise promise) { final SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) { if (!ensureOpen(promise)) {
return; return;
} }
@ -79,14 +78,6 @@ public abstract class AbstractOioChannel extends AbstractChannel {
promise.setFailure(t); promise.setFailure(t);
closeIfClosed(); closeIfClosed();
} }
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
connect(remoteAddress, localAddress, promise);
}
});
}
} }
} }