[#2187] Always do a volatile read on the refCnt
This commit is contained in:
parent
35ae000c59
commit
e7b800eb82
@ -28,20 +28,7 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
|
|||||||
|
|
||||||
private static final AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> refCntUpdater;
|
private static final AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> refCntUpdater;
|
||||||
|
|
||||||
private static final long REFCNT_FIELD_OFFSET;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
long refCntFieldOffset = -1;
|
|
||||||
try {
|
|
||||||
if (PlatformDependent.hasUnsafe()) {
|
|
||||||
refCntFieldOffset = PlatformDependent.objectFieldOffset(
|
|
||||||
AbstractReferenceCountedByteBuf.class.getDeclaredField("refCnt"));
|
|
||||||
}
|
|
||||||
} catch (Throwable t) {
|
|
||||||
// Ignored
|
|
||||||
}
|
|
||||||
|
|
||||||
REFCNT_FIELD_OFFSET = refCntFieldOffset;
|
|
||||||
AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> updater =
|
AtomicIntegerFieldUpdater<AbstractReferenceCountedByteBuf> updater =
|
||||||
PlatformDependent.newAtomicIntegerFieldUpdater(AbstractReferenceCountedByteBuf.class, "refCnt");
|
PlatformDependent.newAtomicIntegerFieldUpdater(AbstractReferenceCountedByteBuf.class, "refCnt");
|
||||||
if (updater == null) {
|
if (updater == null) {
|
||||||
@ -59,13 +46,8 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final int refCnt() {
|
public final int refCnt() {
|
||||||
if (REFCNT_FIELD_OFFSET >= 0) {
|
|
||||||
// Try to do non-volatile read for performance.
|
|
||||||
return PlatformDependent.getInt(this, REFCNT_FIELD_OFFSET);
|
|
||||||
} else {
|
|
||||||
return refCnt;
|
return refCnt;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
|
* An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
|
||||||
|
@ -26,21 +26,7 @@ public abstract class AbstractReferenceCounted implements ReferenceCounted {
|
|||||||
|
|
||||||
private static final AtomicIntegerFieldUpdater<AbstractReferenceCounted> refCntUpdater;
|
private static final AtomicIntegerFieldUpdater<AbstractReferenceCounted> refCntUpdater;
|
||||||
|
|
||||||
private static final long REFCNT_FIELD_OFFSET;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
long refCntFieldOffset = -1;
|
|
||||||
try {
|
|
||||||
if (PlatformDependent.hasUnsafe()) {
|
|
||||||
refCntFieldOffset = PlatformDependent.objectFieldOffset(
|
|
||||||
AbstractReferenceCounted.class.getDeclaredField("refCnt"));
|
|
||||||
}
|
|
||||||
} catch (Throwable t) {
|
|
||||||
// Ignored
|
|
||||||
}
|
|
||||||
|
|
||||||
REFCNT_FIELD_OFFSET = refCntFieldOffset;
|
|
||||||
|
|
||||||
AtomicIntegerFieldUpdater<AbstractReferenceCounted> updater =
|
AtomicIntegerFieldUpdater<AbstractReferenceCounted> updater =
|
||||||
PlatformDependent.newAtomicIntegerFieldUpdater(AbstractReferenceCounted.class, "refCnt");
|
PlatformDependent.newAtomicIntegerFieldUpdater(AbstractReferenceCounted.class, "refCnt");
|
||||||
if (updater == null) {
|
if (updater == null) {
|
||||||
@ -54,13 +40,8 @@ public abstract class AbstractReferenceCounted implements ReferenceCounted {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final int refCnt() {
|
public final int refCnt() {
|
||||||
if (REFCNT_FIELD_OFFSET >= 0) {
|
|
||||||
// Try to do non-volatile read for performance.
|
|
||||||
return PlatformDependent.getInt(this, REFCNT_FIELD_OFFSET);
|
|
||||||
} else {
|
|
||||||
return refCnt;
|
return refCnt;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
|
* An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
|
||||||
|
Loading…
x
Reference in New Issue
Block a user