Make use of AtomicLongFieldUpdater.addAndGet(...) for cleaner code

Motivation:

The code in ChannelOutboundBuffer can be simplified by using AtomicLongFieldUpdater.addAndGet(...)

Modification:

Replace our manual looping with AtomicLongFieldUpdater.addAndGet(...)

Result:

Cleaner code
This commit is contained in:
Norman Maurer 2014-06-17 18:02:41 +02:00
parent ad86ec798d
commit 061cb21689

View File

@ -215,16 +215,8 @@ public class ChannelOutboundBuffer {
return; return;
} }
long oldValue = totalPendingSize; long newWriteBufferSize = TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, size);
long newWriteBufferSize = oldValue + size; if (newWriteBufferSize > channel.config().getWriteBufferHighWaterMark()) {
while (!TOTAL_PENDING_SIZE_UPDATER.compareAndSet(this, oldValue, newWriteBufferSize)) {
oldValue = totalPendingSize;
newWriteBufferSize = oldValue + size;
}
int highWaterMark = channel.config().getWriteBufferHighWaterMark();
if (newWriteBufferSize > highWaterMark) {
if (WRITABLE_UPDATER.compareAndSet(this, 1, 0)) { if (WRITABLE_UPDATER.compareAndSet(this, 1, 0)) {
channel.pipeline().fireChannelWritabilityChanged(); channel.pipeline().fireChannelWritabilityChanged();
} }
@ -243,16 +235,8 @@ public class ChannelOutboundBuffer {
return; return;
} }
long oldValue = totalPendingSize; long newWriteBufferSize = TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, -size);
long newWriteBufferSize = oldValue - size; if (newWriteBufferSize == 0 || newWriteBufferSize < channel.config().getWriteBufferLowWaterMark()) {
while (!TOTAL_PENDING_SIZE_UPDATER.compareAndSet(this, oldValue, newWriteBufferSize)) {
oldValue = totalPendingSize;
newWriteBufferSize = oldValue - size;
}
int lowWaterMark = channel.config().getWriteBufferLowWaterMark();
if (newWriteBufferSize == 0 || newWriteBufferSize < lowWaterMark) {
if (WRITABLE_UPDATER.compareAndSet(this, 0, 1)) { if (WRITABLE_UPDATER.compareAndSet(this, 0, 1)) {
channel.pipeline().fireChannelWritabilityChanged(); channel.pipeline().fireChannelWritabilityChanged();
} }
@ -436,12 +420,7 @@ public class ChannelOutboundBuffer {
// Just decrease; do not trigger any events via decrementPendingOutboundBytes() // Just decrease; do not trigger any events via decrementPendingOutboundBytes()
int size = e.pendingSize; int size = e.pendingSize;
long oldValue = totalPendingSize; TOTAL_PENDING_SIZE_UPDATER.addAndGet(this, -size);
long newWriteBufferSize = oldValue - size;
while (!TOTAL_PENDING_SIZE_UPDATER.compareAndSet(this, oldValue, newWriteBufferSize)) {
oldValue = totalPendingSize;
newWriteBufferSize = oldValue - size;
}
e.pendingSize = 0; e.pendingSize = 0;
if (!e.cancelled) { if (!e.cancelled) {