Skip to content

Commit

Permalink
Share code between retain(...) and release(...) implementations.
Browse files Browse the repository at this point in the history
Motivation:

We can share the code in retain() and retain(...) and also in release() and release(...).

Modifications:

Share code.

Result:

Less duplicated code.
  • Loading branch information
normanmaurer committed Sep 2, 2016
1 parent 862983b commit 2fddfb8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import static io.netty.util.internal.ObjectUtil.checkPositive;

/**
* Abstract base class for {@link ByteBuf} implementations that count references.
*/
Expand Down Expand Up @@ -57,27 +59,15 @@ protected final void setRefCnt(int refCnt) {

@Override
public ByteBuf retain() {
for (;;) {
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;
return retain0(1);
}

@Override
public ByteBuf retain(int increment) {
if (increment <= 0) {
throw new IllegalArgumentException("increment: " + increment + " (expected: > 0)");
}
return retain0(checkPositive(increment, "increment"));
}

private ByteBuf retain0(int increment) {
for (;;) {
int refCnt = this.refCnt;
final int nextCnt = refCnt + increment;
Expand All @@ -95,28 +85,15 @@ public ByteBuf retain(int increment) {

@Override
public boolean release() {
for (;;) {
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;
}
}
return release0(1);
}

@Override
public boolean release(int decrement) {
if (decrement <= 0) {
throw new IllegalArgumentException("decrement: " + decrement + " (expected: > 0)");
}
return release0(checkPositive(decrement, "decrement"));
}

private boolean release0(int decrement) {
for (;;) {
int refCnt = this.refCnt;
if (refCnt < decrement) {
Expand All @@ -132,7 +109,6 @@ public boolean release(int decrement) {
}
}
}

/**
* Called once {@link #refCnt()} is equals 0.
*/
Expand Down
47 changes: 12 additions & 35 deletions common/src/main/java/io/netty/util/AbstractReferenceCounted.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

import static io.netty.util.internal.ObjectUtil.checkPositive;

/**
* Abstract base class for classes wants to implement {@link ReferenceCounted}.
*/
Expand Down Expand Up @@ -51,27 +53,15 @@ protected final void setRefCnt(int refCnt) {

@Override
public ReferenceCounted retain() {
for (;;) {
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;
return retain0(1);
}

@Override
public ReferenceCounted retain(int increment) {
if (increment <= 0) {
throw new IllegalArgumentException("increment: " + increment + " (expected: > 0)");
}
return retain0(checkPositive(increment, "increment"));
}

private ReferenceCounted retain0(int increment) {
for (;;) {
int refCnt = this.refCnt;
final int nextCnt = refCnt + increment;
Expand All @@ -88,29 +78,16 @@ public ReferenceCounted retain(int increment) {
}

@Override
public final boolean release() {
for (;;) {
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;
}
}
public boolean release() {
return release0(1);
}

@Override
public final boolean release(int decrement) {
if (decrement <= 0) {
throw new IllegalArgumentException("decrement: " + decrement + " (expected: > 0)");
}
public boolean release(int decrement) {
return release0(checkPositive(decrement, "decrement"));
}

private boolean release0(int decrement) {
for (;;) {
int refCnt = this.refCnt;
if (refCnt < decrement) {
Expand Down

0 comments on commit 2fddfb8

Please sign in to comment.