@@ -89,7 +89,7 @@ protected ByteBuffer[] initialValue() throws Exception { |
89 | 89 | @SuppressWarnings("UnusedDeclaration") |
90 | 90 | private volatile int unwritable; |
91 | 91 |
|
92 | | - private final Runnable fireChannelWritabilityChangedTask; |
| 92 | + private volatile Runnable fireChannelWritabilityChangedTask; |
93 | 93 |
|
94 | 94 | static { |
95 | 95 | AtomicIntegerFieldUpdater<ChannelOutboundBuffer> unwritableUpdater = |
@@ -107,9 +107,8 @@ protected ByteBuffer[] initialValue() throws Exception { |
107 | 107 | TOTAL_PENDING_SIZE_UPDATER = pendingSizeUpdater; |
108 | 108 | } |
109 | 109 |
|
110 | | - ChannelOutboundBuffer(final AbstractChannel channel) { |
| 110 | + ChannelOutboundBuffer(AbstractChannel channel) { |
111 | 111 | this.channel = channel; |
112 | | - fireChannelWritabilityChangedTask = new ChannelWritabilityChangedTask(channel); |
113 | 112 | } |
114 | 113 |
|
115 | 114 | /** |
@@ -132,7 +131,7 @@ public void addMessage(Object msg, int size, ChannelPromise promise) { |
132 | 131 |
|
133 | 132 | // increment pending bytes after adding message to the unflushed arrays. |
134 | 133 | // See https://github.com/netty/netty/issues/1619 |
135 | | - incrementPendingOutboundBytes(size, true); |
| 134 | + incrementPendingOutboundBytes(size, false); |
136 | 135 | } |
137 | 136 |
|
138 | 137 | /** |
@@ -155,7 +154,7 @@ public void addFlush() { |
155 | 154 | if (!entry.promise.setUncancellable()) { |
156 | 155 | // Was cancelled so make sure we free up memory and notify about the freed bytes |
157 | 156 | int pending = entry.cancel(); |
158 | | - decrementPendingOutboundBytes(pending, true); |
| 157 | + decrementPendingOutboundBytes(pending, false, true); |
159 | 158 | } |
160 | 159 | entry = entry.next; |
161 | 160 | } while (entry != null); |
@@ -169,30 +168,38 @@ public void addFlush() { |
169 | 168 | * Increment the pending bytes which will be written at some point. |
170 | 169 | * This method is thread-safe! |
171 | 170 | */ |
172 | | - void incrementPendingOutboundBytes(long size, boolean notifyWritability) { |
| 171 | + void incrementPendingOutboundBytes(long size) { |
| 172 | + incrementPendingOutboundBytes(size, true); |
| 173 | + } |
| 174 | + |
| 175 | + private void incrementPendingOutboundBytes(long size, boolean invokeLater) { |
173 | 176 | if (size == 0) { |
174 | 177 | return; |
175 | 178 | } |
176 | 179 |
|
177 | 180 | long newWriteBufferSize = TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, size); |
178 | 181 | if (newWriteBufferSize >= channel.config().getWriteBufferHighWaterMark()) { |
179 | | - setUnwritable(notifyWritability); |
| 182 | + setUnwritable(invokeLater); |
180 | 183 | } |
181 | 184 | } |
182 | 185 |
|
183 | 186 | /** |
184 | 187 | * Decrement the pending bytes which will be written at some point. |
185 | 188 | * This method is thread-safe! |
186 | 189 | */ |
187 | | - void decrementPendingOutboundBytes(long size, boolean notifyWritability) { |
| 190 | + void decrementPendingOutboundBytes(long size) { |
| 191 | + decrementPendingOutboundBytes(size, true, true); |
| 192 | + } |
| 193 | + |
| 194 | + private void decrementPendingOutboundBytes(long size, boolean invokeLater, boolean notifyWritability) { |
188 | 195 | if (size == 0) { |
189 | 196 | return; |
190 | 197 | } |
191 | 198 |
|
192 | 199 | long newWriteBufferSize = TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, -size); |
193 | | - if (newWriteBufferSize == 0 |
194 | | - || newWriteBufferSize <= channel.config().getWriteBufferLowWaterMark()) { |
195 | | - setWritable(notifyWritability); |
| 200 | + if (notifyWritability && (newWriteBufferSize == 0 |
| 201 | + || newWriteBufferSize <= channel.config().getWriteBufferLowWaterMark())) { |
| 202 | + setWritable(invokeLater); |
196 | 203 | } |
197 | 204 | } |
198 | 205 |
|
@@ -257,7 +264,7 @@ public boolean remove() { |
257 | 264 | // only release message, notify and decrement if it was not canceled before. |
258 | 265 | ReferenceCountUtil.safeRelease(msg); |
259 | 266 | safeSuccess(promise); |
260 | | - decrementPendingOutboundBytes(size, true); |
| 267 | + decrementPendingOutboundBytes(size, false, true); |
261 | 268 | } |
262 | 269 |
|
263 | 270 | // recycle the entry |
@@ -293,7 +300,7 @@ private boolean remove0(Throwable cause, boolean notifyWritability) { |
293 | 300 | ReferenceCountUtil.safeRelease(msg); |
294 | 301 |
|
295 | 302 | safeFail(promise, cause); |
296 | | - decrementPendingOutboundBytes(size, notifyWritability); |
| 303 | + decrementPendingOutboundBytes(size, false, notifyWritability); |
297 | 304 | } |
298 | 305 |
|
299 | 306 | // recycle the entry |
@@ -515,7 +522,7 @@ private void setUserDefinedWritability(int index) { |
515 | 522 | final int newValue = oldValue & mask; |
516 | 523 | if (UNWRITABLE_UPDATER.compareAndSet(this, oldValue, newValue)) { |
517 | 524 | if (oldValue != 0 && newValue == 0) { |
518 | | - fireChannelWritabilityChanged(); |
| 525 | + fireChannelWritabilityChanged(true); |
519 | 526 | } |
520 | 527 | break; |
521 | 528 | } |
@@ -529,7 +536,7 @@ private void clearUserDefinedWritability(int index) { |
529 | 536 | final int newValue = oldValue | mask; |
530 | 537 | if (UNWRITABLE_UPDATER.compareAndSet(this, oldValue, newValue)) { |
531 | 538 | if (oldValue == 0 && newValue != 0) { |
532 | | - fireChannelWritabilityChanged(); |
| 539 | + fireChannelWritabilityChanged(true); |
533 | 540 | } |
534 | 541 | break; |
535 | 542 | } |
@@ -543,36 +550,48 @@ private static int writabilityMask(int index) { |
543 | 550 | return 1 << index; |
544 | 551 | } |
545 | 552 |
|
546 | | - private void setWritable(boolean notify) { |
| 553 | + private void setWritable(boolean invokeLater) { |
547 | 554 | for (;;) { |
548 | 555 | final int oldValue = unwritable; |
549 | 556 | final int newValue = oldValue & ~1; |
550 | 557 | if (UNWRITABLE_UPDATER.compareAndSet(this, oldValue, newValue)) { |
551 | | - if (notify && oldValue != 0 && newValue == 0) { |
552 | | - fireChannelWritabilityChanged(); |
| 558 | + if (oldValue != 0 && newValue == 0) { |
| 559 | + fireChannelWritabilityChanged(invokeLater); |
553 | 560 | } |
554 | 561 | break; |
555 | 562 | } |
556 | 563 | } |
557 | 564 | } |
558 | 565 |
|
559 | | - private void setUnwritable(boolean notify) { |
| 566 | + private void setUnwritable(boolean invokeLater) { |
560 | 567 | for (;;) { |
561 | 568 | final int oldValue = unwritable; |
562 | 569 | final int newValue = oldValue | 1; |
563 | 570 | if (UNWRITABLE_UPDATER.compareAndSet(this, oldValue, newValue)) { |
564 | | - if (notify && oldValue == 0 && newValue != 0) { |
565 | | - fireChannelWritabilityChanged(); |
| 571 | + if (oldValue == 0 && newValue != 0) { |
| 572 | + fireChannelWritabilityChanged(invokeLater); |
566 | 573 | } |
567 | 574 | break; |
568 | 575 | } |
569 | 576 | } |
570 | 577 | } |
571 | 578 |
|
572 | | - private void fireChannelWritabilityChanged() { |
573 | | - // Always invoke it later to prevent re-entrance bug. |
574 | | - // See https://github.com/netty/netty/issues/5028 |
575 | | - channel.eventLoop().execute(fireChannelWritabilityChangedTask); |
| 579 | + private void fireChannelWritabilityChanged(boolean invokeLater) { |
| 580 | + final ChannelPipeline pipeline = channel.pipeline(); |
| 581 | + if (invokeLater) { |
| 582 | + Runnable task = fireChannelWritabilityChangedTask; |
| 583 | + if (task == null) { |
| 584 | + fireChannelWritabilityChangedTask = task = new Runnable() { |
| 585 | + @Override |
| 586 | + public void run() { |
| 587 | + pipeline.fireChannelWritabilityChanged(); |
| 588 | + } |
| 589 | + }; |
| 590 | + } |
| 591 | + channel.eventLoop().execute(task); |
| 592 | + } else { |
| 593 | + pipeline.fireChannelWritabilityChanged(); |
| 594 | + } |
576 | 595 | } |
577 | 596 |
|
578 | 597 | /** |
@@ -843,25 +862,4 @@ Entry recycleAndGetNext() { |
843 | 862 | return next; |
844 | 863 | } |
845 | 864 | } |
846 | | - |
847 | | - private static final class ChannelWritabilityChangedTask implements Runnable { |
848 | | - private final Channel channel; |
849 | | - private boolean writable = true; |
850 | | - |
851 | | - ChannelWritabilityChangedTask(Channel channel) { |
852 | | - this.channel = channel; |
853 | | - } |
854 | | - |
855 | | - @Override |
856 | | - public void run() { |
857 | | - if (channel.isActive()) { |
858 | | - boolean newWritable = channel.isWritable(); |
859 | | - |
860 | | - if (writable != newWritable) { |
861 | | - writable = newWritable; |
862 | | - channel.pipeline().fireChannelWritabilityChanged(); |
863 | | - } |
864 | | - } |
865 | | - } |
866 | | - } |
867 | 865 | } |