From 84cf41890c16903fcd4fc3089b380c99ba6fdfcf Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 28 Jan 2010 13:16:46 +0000 Subject: [PATCH] DirectBufferPool should be static because it uses thread local now --- .../channel/socket/nio/DirectBufferPool.java | 20 +++++++++---------- .../channel/socket/nio/NioDatagramWorker.java | 10 ++++------ .../netty/channel/socket/nio/NioWorker.java | 13 ++++++------ 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java b/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java index 324ea2acf9..9fef92fe90 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java @@ -29,7 +29,7 @@ final class DirectBufferPool { private static final int POOL_SIZE = 4; - private final ThreadLocal[]> pool = + private static final ThreadLocal[]> pool = new ThreadLocal[]>() { @Override @SuppressWarnings("unchecked") @@ -40,19 +40,15 @@ final class DirectBufferPool { }; - DirectBufferPool() { - super(); - } - - final ByteBuffer acquire(ChannelBuffer src) { + static final ByteBuffer acquire(ChannelBuffer src) { ByteBuffer dst = acquire(src.readableBytes()); src.getBytes(src.readerIndex(), dst); dst.rewind(); return dst; } - final ByteBuffer acquire(int size) { - final SoftReference[] pool = this.pool.get(); + static final ByteBuffer acquire(int size) { + final SoftReference[] pool = DirectBufferPool.pool.get(); for (int i = 0; i < POOL_SIZE; i ++) { SoftReference ref = pool[i]; if (ref == null) { @@ -81,8 +77,8 @@ final class DirectBufferPool { return buf; } - final void release(ByteBuffer buffer) { - final SoftReference[] pool = this.pool.get(); + static final void release(ByteBuffer buffer) { + final SoftReference[] pool = DirectBufferPool.pool.get(); for (int i = 0; i < POOL_SIZE; i ++) { SoftReference ref = pool[i]; if (ref == null || ref.get() == null) { @@ -114,4 +110,8 @@ final class DirectBufferPool { // but it becomes 8192 to keep the calculation simplistic. return (capacity & 0xfffff000) + 0x1000; } + + private DirectBufferPool() { + super(); + } } diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java index a8c4dee554..72194c20dd 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioDatagramWorker.java @@ -127,8 +127,6 @@ class NioDatagramWorker implements Runnable { private volatile int cancelledKeys; // should use AtomicInteger but we just need approximation - private final DirectBufferPool directBufferPool = new DirectBufferPool(); - /** * Sole constructor. * @@ -476,7 +474,7 @@ class NioDatagramWorker implements Runnable { channel.currentWriteBuffer = buf = origBuf.toByteBuffer(); channel.currentWriteBufferIsPooled = false; } else { - channel.currentWriteBuffer = buf = directBufferPool.acquire(origBuf); + channel.currentWriteBuffer = buf = DirectBufferPool.acquire(origBuf); channel.currentWriteBufferIsPooled = true; } } else { @@ -508,7 +506,7 @@ class NioDatagramWorker implements Runnable { if (localWrittenBytes > 0) { // Successful write - proceed to the next message. if (channel.currentWriteBufferIsPooled) { - directBufferPool.release(buf); + DirectBufferPool.release(buf); } ChannelFuture future = evt.getFuture(); @@ -526,7 +524,7 @@ class NioDatagramWorker implements Runnable { // Doesn't need a user attention - ignore. } catch (final Throwable t) { if (channel.currentWriteBufferIsPooled) { - directBufferPool.release(buf); + DirectBufferPool.release(buf); } ChannelFuture future = evt.getFuture(); channel.currentWriteEvent = null; @@ -636,7 +634,7 @@ class NioDatagramWorker implements Runnable { cause = new ClosedChannelException(); } if (channel.currentWriteBufferIsPooled) { - directBufferPool.release(buf); + DirectBufferPool.release(buf); } ChannelFuture future = evt.getFuture(); diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java index d9888d04a2..902486cb38 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java @@ -78,7 +78,6 @@ class NioWorker implements Runnable { private final Queue registerTaskQueue = new LinkedTransferQueue(); private final Queue writeTaskQueue = new LinkedTransferQueue(); private volatile int cancelledKeys; // should use AtomicInteger but we just need approximation - private final DirectBufferPool directBufferPool = new DirectBufferPool(); NioWorker(int bossId, int id, Executor executor) { this.bossId = bossId; @@ -317,7 +316,7 @@ class NioWorker implements Runnable { final ByteBuffer directBuffer; final boolean fromPool = !buffer.isDirect(); if (fromPool) { - directBuffer = directBufferPool.acquire(buffer.writableBytes()); + directBuffer = DirectBufferPool.acquire(buffer.writableBytes()); } else { directBuffer = buffer.toByteBuffer(); } @@ -341,7 +340,7 @@ class NioWorker implements Runnable { if (fromPool) { directBuffer.flip(); buffer.writeBytes(directBuffer); - directBufferPool.release(directBuffer); + DirectBufferPool.release(directBuffer); } else { // no need to copy: directBuffer is just a view to buffer. buffer.writerIndex(buffer.writerIndex() + readBytes); @@ -408,7 +407,7 @@ class NioWorker implements Runnable { channel.currentWriteBuffer = buf = origBuf.toByteBuffer(); channel.currentWriteBufferIsPooled = false; } else { - channel.currentWriteBuffer = buf = directBufferPool.acquire(origBuf); + channel.currentWriteBuffer = buf = DirectBufferPool.acquire(origBuf); channel.currentWriteBufferIsPooled = true; } } else { @@ -427,7 +426,7 @@ class NioWorker implements Runnable { if (!buf.hasRemaining()) { // Successful write - proceed to the next message. if (channel.currentWriteBufferIsPooled) { - directBufferPool.release(buf); + DirectBufferPool.release(buf); } ChannelFuture future = evt.getFuture(); @@ -445,7 +444,7 @@ class NioWorker implements Runnable { // Doesn't need a user attention - ignore. } catch (Throwable t) { if (channel.currentWriteBufferIsPooled) { - directBufferPool.release(buf); + DirectBufferPool.release(buf); } ChannelFuture future = evt.getFuture(); channel.currentWriteEvent = null; @@ -563,7 +562,7 @@ class NioWorker implements Runnable { } if (channel.currentWriteBufferIsPooled) { - directBufferPool.release(buf); + DirectBufferPool.release(buf); } ChannelFuture future = evt.getFuture();