Replace synchronized blocks + assert with synchronized method (#9538)

Motivation:

Following up on discussion with @normanmaurer with suggestion to improve code clarity.

Modification:

Method is synchronized, no need for assert or verbose sync blocks around calls.

Result:

Reduce verbosity and more idiomatic use of keyword. Also rename the method to better describe what it's for.
This commit is contained in:
Nitsan Wakart 2019-09-05 09:12:16 +02:00 committed by Norman Maurer
parent 2123fbe495
commit d446765b84

View File

@ -457,7 +457,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
assert ctx != head && ctx != tail;
synchronized (this) {
remove0(ctx);
atomicRemoveFromHandlerList(ctx);
// If the registered is false it means that the channel was not registered on an eventloop yet.
// In this case we remove the context from the pipeline and add a task that will call
@ -482,8 +482,10 @@ public class DefaultChannelPipeline implements ChannelPipeline {
return ctx;
}
private void remove0(AbstractChannelHandlerContext ctx) {
assert Thread.holdsLock(this);
/**
* Method is synchronized to make the handler removal from the double linked list atomic.
*/
private synchronized void atomicRemoveFromHandlerList(AbstractChannelHandlerContext ctx) {
AbstractChannelHandlerContext prev = ctx.prev;
AbstractChannelHandlerContext next = ctx.next;
prev.next = next;
@ -612,9 +614,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
} catch (Throwable t) {
boolean removed = false;
try {
synchronized (this) {
remove0(ctx);
}
atomicRemoveFromHandlerList(ctx);
ctx.callHandlerRemoved();
removed = true;
} catch (Throwable t2) {
@ -884,9 +884,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
final EventExecutor executor = ctx.executor();
if (inEventLoop || executor.inEventLoop(currentThread)) {
synchronized (this) {
remove0(ctx);
}
atomicRemoveFromHandlerList(ctx);
callHandlerRemoved0(ctx);
} else {
final AbstractChannelHandlerContext finalCtx = ctx;
@ -1484,9 +1482,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
"Can't invoke handlerAdded() as the EventExecutor {} rejected it, removing handler {}.",
executor, ctx.name(), e);
}
synchronized (this) {
remove0(ctx);
}
atomicRemoveFromHandlerList(ctx);
ctx.setRemoved();
}
}