Share code between retain(...) and release(...) implementations.
Motivation: We can share the code in retain() and retain(...) and also in release() and release(...). Modifications: Share code. Result: Less duplicated code.
This commit is contained in:
parent
30fe2e868f
commit
3103f0551c
@ -21,6 +21,8 @@ import io.netty.util.internal.PlatformDependent;
|
|||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
||||||
|
|
||||||
|
import static io.netty.util.internal.ObjectUtil.checkPositive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for {@link ByteBuf} implementations that count references.
|
* Abstract base class for {@link ByteBuf} implementations that count references.
|
||||||
*/
|
*/
|
||||||
@ -57,27 +59,15 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf retain() {
|
public ByteBuf retain() {
|
||||||
for (;;) {
|
return retain0(1);
|
||||||
int refCnt = this.refCnt;
|
|
||||||
final int nextCnt = refCnt + 1;
|
|
||||||
|
|
||||||
// Ensure we not resurrect (which means the refCnt was 0) and also that we encountered an overflow.
|
|
||||||
if (nextCnt <= 1) {
|
|
||||||
throw new IllegalReferenceCountException(refCnt, 1);
|
|
||||||
}
|
|
||||||
if (refCntUpdater.compareAndSet(this, refCnt, nextCnt)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ByteBuf retain(int increment) {
|
public ByteBuf retain(int increment) {
|
||||||
if (increment <= 0) {
|
return retain0(checkPositive(increment, "increment"));
|
||||||
throw new IllegalArgumentException("increment: " + increment + " (expected: > 0)");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
private ByteBuf retain0(int increment) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int refCnt = this.refCnt;
|
int refCnt = this.refCnt;
|
||||||
final int nextCnt = refCnt + increment;
|
final int nextCnt = refCnt + increment;
|
||||||
@ -105,28 +95,15 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean release() {
|
public boolean release() {
|
||||||
for (;;) {
|
return release0(1);
|
||||||
int refCnt = this.refCnt;
|
|
||||||
if (refCnt == 0) {
|
|
||||||
throw new IllegalReferenceCountException(0, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (refCntUpdater.compareAndSet(this, refCnt, refCnt - 1)) {
|
|
||||||
if (refCnt == 1) {
|
|
||||||
deallocate();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean release(int decrement) {
|
public boolean release(int decrement) {
|
||||||
if (decrement <= 0) {
|
return release0(checkPositive(decrement, "decrement"));
|
||||||
throw new IllegalArgumentException("decrement: " + decrement + " (expected: > 0)");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
private boolean release0(int decrement) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int refCnt = this.refCnt;
|
int refCnt = this.refCnt;
|
||||||
if (refCnt < decrement) {
|
if (refCnt < decrement) {
|
||||||
@ -142,7 +119,6 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called once {@link #refCnt()} is equals 0.
|
* Called once {@link #refCnt()} is equals 0.
|
||||||
*/
|
*/
|
||||||
|
@ -19,6 +19,8 @@ import io.netty.util.internal.PlatformDependent;
|
|||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
||||||
|
|
||||||
|
import static io.netty.util.internal.ObjectUtil.checkPositive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for classes wants to implement {@link ReferenceCounted}.
|
* Abstract base class for classes wants to implement {@link ReferenceCounted}.
|
||||||
*/
|
*/
|
||||||
@ -51,27 +53,15 @@ public abstract class AbstractReferenceCounted implements ReferenceCounted {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReferenceCounted retain() {
|
public ReferenceCounted retain() {
|
||||||
for (;;) {
|
return retain0(1);
|
||||||
int refCnt = this.refCnt;
|
|
||||||
final int nextCnt = refCnt + 1;
|
|
||||||
|
|
||||||
// Ensure we not resurrect (which means the refCnt was 0) and also that we encountered an overflow.
|
|
||||||
if (nextCnt <= 1) {
|
|
||||||
throw new IllegalReferenceCountException(refCnt, 1);
|
|
||||||
}
|
|
||||||
if (refCntUpdater.compareAndSet(this, refCnt, nextCnt)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReferenceCounted retain(int increment) {
|
public ReferenceCounted retain(int increment) {
|
||||||
if (increment <= 0) {
|
return retain0(checkPositive(increment, "increment"));
|
||||||
throw new IllegalArgumentException("increment: " + increment + " (expected: > 0)");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
private ReferenceCounted retain0(int increment) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int refCnt = this.refCnt;
|
int refCnt = this.refCnt;
|
||||||
final int nextCnt = refCnt + increment;
|
final int nextCnt = refCnt + increment;
|
||||||
@ -94,28 +84,15 @@ public abstract class AbstractReferenceCounted implements ReferenceCounted {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean release() {
|
public boolean release() {
|
||||||
for (;;) {
|
return release0(1);
|
||||||
int refCnt = this.refCnt;
|
|
||||||
if (refCnt == 0) {
|
|
||||||
throw new IllegalReferenceCountException(0, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (refCntUpdater.compareAndSet(this, refCnt, refCnt - 1)) {
|
|
||||||
if (refCnt == 1) {
|
|
||||||
deallocate();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean release(int decrement) {
|
public boolean release(int decrement) {
|
||||||
if (decrement <= 0) {
|
return release0(checkPositive(decrement, "decrement"));
|
||||||
throw new IllegalArgumentException("decrement: " + decrement + " (expected: > 0)");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
private boolean release0(int decrement) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int refCnt = this.refCnt;
|
int refCnt = this.refCnt;
|
||||||
if (refCnt < decrement) {
|
if (refCnt < decrement) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user