Remove atomic usage in DefaultChannelHandlerContext as all pipeline operations are on the EventLoop (#9794)

Motivation:

In netty 5.x we changed to have all pipeline operations be executed on the EventLoop so there is no need to have an atomic operation involved anymore to update the handler state.

Modifications:

Remove atomic usage to handle the handler state

Result:

Simpler code and less overhead
This commit is contained in:
Norman Maurer 2019-11-22 10:12:21 +01:00 committed by GitHub
parent e8d72fda5f
commit b7b6156505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -32,7 +32,6 @@ import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.net.SocketAddress;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import static io.netty.channel.ChannelHandlerMask.*;
@ -40,9 +39,6 @@ final class DefaultChannelHandlerContext implements ChannelHandlerContext, Resou
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultChannelHandlerContext.class);
private static final AtomicIntegerFieldUpdater<DefaultChannelHandlerContext> HANDLER_STATE_UPDATER =
AtomicIntegerFieldUpdater.newUpdater(DefaultChannelHandlerContext.class, "handlerState");
/**
* Neither {@link ChannelHandler#handlerAdded(ChannelHandlerContext)}
* nor {@link ChannelHandler#handlerRemoved(ChannelHandlerContext)} was called.
@ -70,7 +66,7 @@ final class DefaultChannelHandlerContext implements ChannelHandlerContext, Resou
DefaultChannelHandlerContext next;
DefaultChannelHandlerContext prev;
private volatile int handlerState = INIT;
private int handlerState = INIT;
// Keeps track of processing different events
private short outboundOperations;
@ -1100,7 +1096,11 @@ final class DefaultChannelHandlerContext implements ChannelHandlerContext, Resou
// Ensure we never update when the handlerState is REMOVE_COMPLETE already.
// oldState is usually ADD_PENDING but can also be REMOVE_COMPLETE when an EventExecutor is used that is not
// exposing ordering guarantees.
return HANDLER_STATE_UPDATER.getAndSet(this, ADD_COMPLETE) != REMOVE_COMPLETE;
if (handlerState != REMOVE_COMPLETE) {
handlerState = ADD_COMPLETE;
return true;
}
return false;
}
void callHandlerAdded() throws Exception {