Skip to content

Commit

Permalink
Mark Recycler.recycle(...) deprecated and update usage.
Browse files Browse the repository at this point in the history
Motivation:

Recycler.recycle(...) should not be used anymore and be replaced by Handle.recycle().

Modifications:

Mark it as deprecated and update usage.

Result:

Correctly document deprecated api.
  • Loading branch information
normanmaurer committed May 20, 2016
1 parent d41f076 commit e10dca7
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 31 deletions.
16 changes: 8 additions & 8 deletions buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ static final class ThreadLocalUnsafeDirectByteBuf extends UnpooledUnsafeDirectBy
private static final Recycler<ThreadLocalUnsafeDirectByteBuf> RECYCLER =
new Recycler<ThreadLocalUnsafeDirectByteBuf>() {
@Override
protected ThreadLocalUnsafeDirectByteBuf newObject(Handle handle) {
protected ThreadLocalUnsafeDirectByteBuf newObject(Handle<ThreadLocalUnsafeDirectByteBuf> handle) {
return new ThreadLocalUnsafeDirectByteBuf(handle);
}
};
Expand All @@ -948,9 +948,9 @@ static ThreadLocalUnsafeDirectByteBuf newInstance() {
return buf;
}

private final Handle handle;
private final Handle<ThreadLocalUnsafeDirectByteBuf> handle;

private ThreadLocalUnsafeDirectByteBuf(Handle handle) {
private ThreadLocalUnsafeDirectByteBuf(Handle<ThreadLocalUnsafeDirectByteBuf> handle) {
super(UnpooledByteBufAllocator.DEFAULT, 256, Integer.MAX_VALUE);
this.handle = handle;
}
Expand All @@ -961,7 +961,7 @@ protected void deallocate() {
super.deallocate();
} else {
clear();
RECYCLER.recycle(this, handle);
handle.recycle(this);
}
}
}
Expand All @@ -970,7 +970,7 @@ static final class ThreadLocalDirectByteBuf extends UnpooledDirectByteBuf {

private static final Recycler<ThreadLocalDirectByteBuf> RECYCLER = new Recycler<ThreadLocalDirectByteBuf>() {
@Override
protected ThreadLocalDirectByteBuf newObject(Handle handle) {
protected ThreadLocalDirectByteBuf newObject(Handle<ThreadLocalDirectByteBuf> handle) {
return new ThreadLocalDirectByteBuf(handle);
}
};
Expand All @@ -981,9 +981,9 @@ static ThreadLocalDirectByteBuf newInstance() {
return buf;
}

private final Handle handle;
private final Handle<ThreadLocalDirectByteBuf> handle;

private ThreadLocalDirectByteBuf(Handle handle) {
private ThreadLocalDirectByteBuf(Handle<ThreadLocalDirectByteBuf> handle) {
super(UnpooledByteBufAllocator.DEFAULT, 256, Integer.MAX_VALUE);
this.handle = handle;
}
Expand All @@ -994,7 +994,7 @@ protected void deallocate() {
super.deallocate();
} else {
clear();
RECYCLER.recycle(this, handle);
handle.recycle(this);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions buffer/src/main/java/io/netty/buffer/PoolThreadCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,18 +457,18 @@ private void freeEntry(Entry entry) {
}

static final class Entry<T> {
final Handle recyclerHandle;
final Handle<Entry<?>> recyclerHandle;
PoolChunk<T> chunk;
long handle = -1;

Entry(Handle recyclerHandle) {
Entry(Handle<Entry<?>> recyclerHandle) {
this.recyclerHandle = recyclerHandle;
}

void recycle() {
chunk = null;
handle = -1;
RECYCLER.recycle(this, recyclerHandle);
recyclerHandle.recycle(this);
}
}

Expand All @@ -482,8 +482,9 @@ private static Entry newEntry(PoolChunk<?> chunk, long handle) {

@SuppressWarnings("rawtypes")
private static final Recycler<Entry> RECYCLER = new Recycler<Entry>() {
@SuppressWarnings("unchecked")
@Override
protected Entry newObject(Handle handle) {
protected Entry newObject(Handle<Entry> handle) {
return new Entry(handle);
}
};
Expand Down
4 changes: 4 additions & 0 deletions common/src/main/java/io/netty/util/Recycler.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public final T get() {
return (T) handle.value;
}

/**
* @deprecated use {@link Handle#recycle(Object)}.
*/
@Deprecated
public final boolean recycle(T o, Handle<T> handle) {
if (handle == NOOP_HANDLE) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private PendingWrite(Recycler.Handle<PendingWrite> handle) {
public boolean recycle() {
msg = null;
promise = null;
return RECYCLER.recycle(this, handle);
handle.recycle(this);
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public boolean insertSinceRecycled() {
public boolean recycle() {
clear();
insertSinceRecycled = false;
return RECYCLER.recycle(this, handle);
handle.recycle(this);
return true;
}
}
24 changes: 14 additions & 10 deletions common/src/test/java/io/netty/util/RecyclerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ public static RecyclableObject newInstance() {
}

public void recycle() {
RECYCLER.recycle(this, handle);
handle.recycle(this);
}
}

static final class DisabledRecyclableObject {

private static final Recycler<DisabledRecyclableObject> RECYCLER = new Recycler<DisabledRecyclableObject>(-1) {
@Override
protected DisabledRecyclableObject newObject(Handle handle) {
protected DisabledRecyclableObject newObject(Handle<DisabledRecyclableObject> handle) {
return new DisabledRecyclableObject(handle);
}
};

private final Recycler.Handle handle;
private final Recycler.Handle<DisabledRecyclableObject> handle;

private DisabledRecyclableObject(Recycler.Handle handle) {
private DisabledRecyclableObject(Recycler.Handle<DisabledRecyclableObject> handle) {
this.handle = handle;
}

Expand All @@ -93,7 +93,7 @@ public static DisabledRecyclableObject newInstance() {
}

public void recycle() {
RECYCLER.recycle(this, handle);
handle.recycle(this);
}
}

Expand Down Expand Up @@ -125,7 +125,7 @@ protected HandledObject newObject(
}

for (int i = 0; i < objects.length; i++) {
recycler.recycle(objects[i], objects[i].handle);
objects[i].recycle();
objects[i] = null;
}

Expand All @@ -136,7 +136,7 @@ protected HandledObject newObject(
public void testRecycleAtDifferentThread() throws Exception {
final Recycler<HandledObject> recycler = new Recycler<HandledObject>(256) {
@Override
protected HandledObject newObject(Recycler.Handle handle) {
protected HandledObject newObject(Recycler.Handle<HandledObject> handle) {
return new HandledObject(handle);
}
};
Expand All @@ -145,7 +145,7 @@ protected HandledObject newObject(Recycler.Handle handle) {
final Thread thread = new Thread() {
@Override
public void run() {
recycler.recycle(o, o.handle);
o.recycle();
}
};
thread.start();
Expand Down Expand Up @@ -174,14 +174,14 @@ protected HandledObject newObject(Recycler.Handle handle) {
}

for (int i = 0; i < maxCapacity; i ++) {
recycler.recycle(array[i], array[i].handle);
array[i].recycle();
}

final Thread thread = new Thread() {
@Override
public void run() {
for (int i = maxCapacity; i < array.length; i ++) {
recycler.recycle(array[i], array[i].handle);
array[i].recycle();
}
}
};
Expand All @@ -205,5 +205,9 @@ static final class HandledObject {
HandledObject(Recycler.Handle<HandledObject> handle) {
this.handle = handle;
}

void recycle() {
handle.recycle(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ protected Entry newObject(Handle handle) {
}
};

private final Handle handle;
private final Handle<Entry> handle;
Entry next;
Object msg;
ByteBuffer[] bufs;
Expand All @@ -810,7 +810,7 @@ protected Entry newObject(Handle handle) {
int count = -1;
boolean cancelled;

private Entry(Handle handle) {
private Entry(Handle<Entry> handle) {
this.handle = handle;
}

Expand Down Expand Up @@ -853,7 +853,7 @@ void recycle() {
pendingSize = 0;
count = -1;
cancelled = false;
RECYCLER.recycle(this, handle);
handle.recycle(this);
}

Entry recycleAndGetNext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,18 @@ private static void safeFail(ChannelPromise promise, Throwable cause) {
static final class PendingWrite {
private static final Recycler<PendingWrite> RECYCLER = new Recycler<PendingWrite>() {
@Override
protected PendingWrite newObject(Handle handle) {
protected PendingWrite newObject(Handle<PendingWrite> handle) {
return new PendingWrite(handle);
}
};

private final Recycler.Handle handle;
private final Recycler.Handle<PendingWrite> handle;
private PendingWrite next;
private long size;
private ChannelPromise promise;
private Object msg;

private PendingWrite(Recycler.Handle handle) {
private PendingWrite(Recycler.Handle<PendingWrite> handle) {
this.handle = handle;
}

Expand All @@ -308,7 +308,7 @@ private void recycle() {
next = null;
msg = null;
promise = null;
RECYCLER.recycle(this, handle);
handle.recycle(this);
}
}
}

0 comments on commit e10dca7

Please sign in to comment.