Skip to content

Commit

Permalink
Ensure WeakOrderQueue can be collected fast enough
Browse files Browse the repository at this point in the history
Motivation:

Commit afafadd introduced a change which stored the Stack in the WeakOrderQueue as field. This unfortunally had the effect that it was not removed from the WeakHashMap anymore as the Stack also is used as key.

Modifications:

Do not store a reference to the Stack in WeakOrderQueue.

Result:

WeakOrderQueue can be collected correctly again.
  • Loading branch information
normanmaurer committed Jul 22, 2016
1 parent 3ebbd96 commit 94d7557
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions common/src/main/java/io/netty/util/Recycler.java
Expand Up @@ -223,7 +223,7 @@ private static final class Link extends AtomicInteger {
private WeakOrderQueue next;
private final WeakReference<Thread> owner;
private final int id = ID_GENERATOR.getAndIncrement();
private final Stack<?> stack;
private final AtomicInteger availableSharedCapacity;

WeakOrderQueue(Stack<?> stack, Thread thread) {
head = tail = new Link();
Expand All @@ -232,19 +232,42 @@ private static final class Link extends AtomicInteger {
next = stack.head;
stack.head = this;
}
this.stack = stack;

// Its important that we not store the Stack itself in the WeakOrderQueue as the Stack also is used in
// the WeakHashMap as key. So just store the enclosed AtomicInteger which should allow to have the
// Stack itself GCed.
availableSharedCapacity = stack.availableSharedCapacity;

// We allocated a Link so reserve the space
boolean reserved = stack.reserveSpace(LINK_CAPACITY);
boolean reserved = reserveSpace(LINK_CAPACITY);
assert reserved;
}

private boolean reserveSpace(int space) {
assert space >= 0;
for (;;) {
int available = availableSharedCapacity.get();
if (available < space) {
return false;
}
if (availableSharedCapacity.compareAndSet(available, available - space)) {
return true;
}
}
}

private void reclaimSpace(int space) {
assert space >= 0;
availableSharedCapacity.addAndGet(space);
}

void add(DefaultHandle<?> handle) {
handle.lastRecycledId = id;

Link tail = this.tail;
int writeIndex;
if ((writeIndex = tail.get()) == LINK_CAPACITY) {
if (!stack.reserveSpace(LINK_CAPACITY)) {
if (!reserveSpace(LINK_CAPACITY)) {
// Drop it.
return;
}
Expand Down Expand Up @@ -314,7 +337,7 @@ boolean transfer(Stack<?> dst) {

if (srcEnd == LINK_CAPACITY && head.next != null) {
// Add capacity back as the Link is GCed.
stack.reclaimSpace(LINK_CAPACITY);
reclaimSpace(LINK_CAPACITY);

this.head = head.next;
}
Expand All @@ -339,7 +362,7 @@ static final class Stack<T> {
private DefaultHandle<?>[] elements;
private final int maxCapacity;
private int size;
private final AtomicInteger availableSharedCapacity;
final AtomicInteger availableSharedCapacity;

private volatile WeakOrderQueue head;
private WeakOrderQueue cursor, prev;
Expand All @@ -352,24 +375,6 @@ static final class Stack<T> {
elements = new DefaultHandle[min(INITIAL_CAPACITY, maxCapacity)];
}

boolean reserveSpace(int space) {
assert space >= 0;
for (;;) {
int available = availableSharedCapacity.get();
if (available < space) {
return false;
}
if (availableSharedCapacity.compareAndSet(available, available - space)) {
return true;
}
}
}

void reclaimSpace(int space) {
assert space >= 0;
availableSharedCapacity.addAndGet(space);
}

int increaseCapacity(int expectedCapacity) {
int newCapacity = elements.length;
int maxCapacity = this.maxCapacity;
Expand Down

0 comments on commit 94d7557

Please sign in to comment.