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;
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.oio.OioByteStreamChannel;
import java.net.SocketAddress;
import java.util.concurrent.TimeUnit;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import static io.netty.channel.rxtx.RxtxChannelOption.*;
/**
* A channel to a serial device using the RXTX library.
@ -134,7 +133,6 @@ public class RxtxChannel extends OioByteStreamChannel {
public void connect(
final SocketAddress remoteAddress,
final SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) {
return;
}
@ -171,14 +169,6 @@ public class RxtxChannel extends OioByteStreamChannel {
promise.setFailure(t);
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() {
@Override
public void run() {
@ -495,8 +488,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void sendFile(final FileRegion region, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (outboundBufSize() > 0) {
flushNotifier(newPromise()).addListener(new ChannelFutureListener() {
@Override
@ -508,14 +499,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
// nothing pending try to send the fileRegion now!
sendFile0(region, promise);
}
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
sendFile(region, promise);
}
});
}
}
private void sendFile0(FileRegion region, ChannelPromise promise) {
@ -631,7 +614,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void bind(final SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) {
return;
}
@ -661,19 +643,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
promise.setFailure(t);
closeIfClosed();
}
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
bind(localAddress, promise);
}
});
}
}
@Override
public final void disconnect(final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
try {
boolean wasActive = isActive();
doDisconnect();
@ -690,19 +663,10 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
promise.setFailure(t);
closeIfClosed();
}
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
disconnect(promise);
}
});
}
}
@Override
public final void close(final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
boolean wasActive = isActive();
if (closeFuture.setClosed()) {
try {
@ -732,14 +696,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
// Closed already.
promise.setSuccess();
}
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
close(promise);
}
});
}
}
@Override
@ -753,7 +709,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
@Override
public final void deregister(final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!registered) {
promise.setSuccess();
return;
@ -785,14 +740,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
postTask.run();
}
}
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
deregister(promise);
}
});
}
}
@Override
@ -801,7 +748,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
return;
}
if (eventLoop().inEventLoop()) {
try {
doBeginRead();
} catch (final Exception e) {
@ -813,16 +759,14 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
});
close(unsafe().voidFuture());
}
} else {
eventLoop().execute(beginReadTask);
}
}
@Override
public void flush(final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
FlushTask task = flushTaskInProgress;
if (task != null) {
if (task == null) {
flushNotifierAndFlush(promise);
} else {
// loop over the tasks to find the last one
for (;;) {
FlushTask t = task.next;
@ -832,17 +776,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
task = t.next;
}
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 {
@Override
public void flush(final ChannelPromise future) {
if (eventLoop().inEventLoop()) {
reject(future);
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
flush(future);
}
});
}
}
@Override
public void connect(
final SocketAddress remoteAddress, final SocketAddress localAddress,
final ChannelPromise future) {
if (eventLoop().inEventLoop()) {
reject(future);
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
connect(remoteAddress, localAddress, 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.
* If this channel is not connected but it can receive messages
* 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
* return {@code null}.
*/
@ -163,13 +163,22 @@ public interface Channel extends AttributeMap, ChannelOutboundInvoker, ChannelPr
ChannelFuture closeFuture();
/**
* <strong>Caution</strong> for transport implementations use only!
* Returns an <em>internal-use-only</em> object that provides unsafe operations.
*/
Unsafe unsafe();
/**
* <strong>Unsafe</strong> operations that should <strong>never</strong> be called
* from user-code. These methods are only provided to implement the actual transport.
* <em>Unsafe</em> operations that should <em>never</em> be called from user-code. These methods
* 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 {
/**

View File

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

View File

@ -277,7 +277,6 @@ public class LocalChannel extends AbstractChannel {
@Override
public void connect(final SocketAddress remoteAddress,
SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) {
return;
}
@ -323,15 +322,6 @@ public class LocalChannel extends AbstractChannel {
LocalServerChannel serverChannel = (LocalServerChannel) boundChannel;
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
public void connect(
final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
if (eventLoop().inEventLoop()) {
if (!ensureOpen(promise)) {
return;
}
@ -202,14 +201,6 @@ public abstract class AbstractNioChannel extends AbstractChannel {
promise.setFailure(t);
closeIfClosed();
}
} else {
eventLoop().execute(new Runnable() {
@Override
public void run() {
connect(remoteAddress, localAddress, promise);
}
});
}
}
@Override

View File

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