diff --git a/common/src/main/java/io/netty/util/Recycler.java b/common/src/main/java/io/netty/util/Recycler.java index 3d6322c28c..e84df6755a 100644 --- a/common/src/main/java/io/netty/util/Recycler.java +++ b/common/src/main/java/io/netty/util/Recycler.java @@ -197,14 +197,14 @@ public abstract class Recycler { public interface Handle extends ObjectPool.Handle { } - static final class DefaultHandle implements Handle { - private int lastRecycledId; - private int recycleId; + private static final class DefaultHandle implements Handle { + int lastRecycledId; + int recycleId; boolean hasBeenRecycled; - private Stack stack; - private Object value; + Stack stack; + Object value; DefaultHandle(Stack stack) { this.stack = stack; @@ -235,21 +235,21 @@ public abstract class Recycler { // a queue that makes only moderate guarantees about visibility: items are seen in the correct order, // but we aren't absolutely guaranteed to ever see anything at all, thereby keeping the queue cheap to maintain - private static final class WeakOrderQueue { + private static final class WeakOrderQueue extends WeakReference { static final WeakOrderQueue DUMMY = new WeakOrderQueue(); // Let Link extend AtomicInteger for intrinsics. The Link itself will be used as writerIndex. @SuppressWarnings("serial") static final class Link extends AtomicInteger { - private final DefaultHandle[] elements = new DefaultHandle[LINK_CAPACITY]; + final DefaultHandle[] elements = new DefaultHandle[LINK_CAPACITY]; - private int readIndex; + int readIndex; Link next; } // Its important this does not hold any reference to either Stack or WeakOrderQueue. - static final class Head { + private static final class Head { private final AtomicInteger availableSharedCapacity; Link link; @@ -305,15 +305,15 @@ public abstract class Recycler { private Link tail; // pointer to another queue of delayed items for the same stack private WeakOrderQueue next; - private final WeakReference owner; private final int id = ID_GENERATOR.getAndIncrement(); private WeakOrderQueue() { - owner = null; + super(null); head = new Head(null); } private WeakOrderQueue(Stack stack, Thread thread) { + super(thread); tail = new Link(); // Its important that we not store the Stack itself in the WeakOrderQueue as the Stack also is used in @@ -321,10 +321,9 @@ public abstract class Recycler { // Stack itself GCed. head = new Head(stack.availableSharedCapacity); head.link = tail; - owner = new WeakReference(thread); } - static WeakOrderQueue newQueue(Stack stack, Thread thread) { + private static WeakOrderQueue newQueue(Stack stack, Thread thread) { final WeakOrderQueue queue = new WeakOrderQueue(stack, thread); // Done outside of the constructor to ensure WeakOrderQueue.this does not escape the constructor and so // may be accessed while its still constructed. @@ -333,7 +332,11 @@ public abstract class Recycler { return queue; } - private void setNext(WeakOrderQueue next) { + WeakOrderQueue getNext() { + return next; + } + + void setNext(WeakOrderQueue next) { assert next != this; this.next = next; } @@ -414,7 +417,7 @@ public abstract class Recycler { final DefaultHandle[] dstElems = dst.elements; int newDstSize = dstSize; for (int i = srcStart; i < srcEnd; i++) { - DefaultHandle element = srcElems[i]; + DefaultHandle element = srcElems[i]; if (element.recycleId == 0) { element.recycleId = element.lastRecycledId; } else if (element.recycleId != element.lastRecycledId) { @@ -449,7 +452,7 @@ public abstract class Recycler { } } - static final class Stack { + private static final class Stack { // we keep a queue of per-thread queues, which is appended to once only, each time a new thread other // than the stack owner recycles: when we run out of items in our stack we iterate this collection @@ -465,12 +468,12 @@ public abstract class Recycler { // it in a timely manner). final WeakReference threadRef; final AtomicInteger availableSharedCapacity; - final int maxDelayedQueues; + private final int maxDelayedQueues; private final int maxCapacity; private final int ratioMask; - private DefaultHandle[] elements; - private int size; + DefaultHandle[] elements; + int size; private int handleRecycleCount = -1; // Start with -1 so the first one will be recycled. private WeakOrderQueue cursor, prev; private volatile WeakOrderQueue head; @@ -536,7 +539,7 @@ public abstract class Recycler { return ret; } - boolean scavenge() { + private boolean scavenge() { // continue an existing scavenge, if any if (scavengeSome()) { return true; @@ -548,7 +551,7 @@ public abstract class Recycler { return false; } - boolean scavengeSome() { + private boolean scavengeSome() { WeakOrderQueue prev; WeakOrderQueue cursor = this.cursor; if (cursor == null) { @@ -567,8 +570,8 @@ public abstract class Recycler { success = true; break; } - WeakOrderQueue next = cursor.next; - if (cursor.owner.get() == null) { + WeakOrderQueue next = cursor.getNext(); + if (cursor.get() == null) { // If the thread associated with the queue is gone, unlink it, after // performing a volatile read to confirm there is no data left to collect. // We never unlink the first queue, as we don't want to synchronize on updating the head.