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; assert ctx != head && ctx != tail;
synchronized (this) { synchronized (this) {
remove0(ctx); atomicRemoveFromHandlerList(ctx);
// If the registered is false it means that the channel was not registered on an eventloop yet. // 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 // 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; 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 prev = ctx.prev;
AbstractChannelHandlerContext next = ctx.next; AbstractChannelHandlerContext next = ctx.next;
prev.next = next; prev.next = next;
@ -612,9 +614,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
} catch (Throwable t) { } catch (Throwable t) {
boolean removed = false; boolean removed = false;
try { try {
synchronized (this) { atomicRemoveFromHandlerList(ctx);
remove0(ctx);
}
ctx.callHandlerRemoved(); ctx.callHandlerRemoved();
removed = true; removed = true;
} catch (Throwable t2) { } catch (Throwable t2) {
@ -884,9 +884,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
final EventExecutor executor = ctx.executor(); final EventExecutor executor = ctx.executor();
if (inEventLoop || executor.inEventLoop(currentThread)) { if (inEventLoop || executor.inEventLoop(currentThread)) {
synchronized (this) { atomicRemoveFromHandlerList(ctx);
remove0(ctx);
}
callHandlerRemoved0(ctx); callHandlerRemoved0(ctx);
} else { } else {
final AbstractChannelHandlerContext finalCtx = ctx; final AbstractChannelHandlerContext finalCtx = ctx;
@ -1484,9 +1482,7 @@ public class DefaultChannelPipeline implements ChannelPipeline {
"Can't invoke handlerAdded() as the EventExecutor {} rejected it, removing handler {}.", "Can't invoke handlerAdded() as the EventExecutor {} rejected it, removing handler {}.",
executor, ctx.name(), e); executor, ctx.name(), e);
} }
synchronized (this) { atomicRemoveFromHandlerList(ctx);
remove0(ctx);
}
ctx.setRemoved(); ctx.setRemoved();
} }
} }