Made sure NioWorker.taskQueue doesn't contain the same WriteTask

This commit is contained in:
Trustin Lee 2008-09-26 03:32:26 +00:00
parent 5f1ecc9022
commit 9f5468f7a7
2 changed files with 6 additions and 1 deletions

View File

@ -27,6 +27,7 @@ import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jboss.netty.channel.AbstractChannel;
import org.jboss.netty.channel.Channel;
@ -49,6 +50,7 @@ abstract class NioSocketChannel extends AbstractChannel
final SocketChannel socket;
private final NioSocketChannelConfig config;
final AtomicBoolean writeTaskInTaskQueue = new AtomicBoolean();
final Runnable writeTask = new WriteTask();
final Queue<MessageEvent> writeBuffer =
new ConcurrentLinkedQueue<MessageEvent>();
@ -119,6 +121,7 @@ abstract class NioSocketChannel extends AbstractChannel
}
public void run() {
writeTaskInTaskQueue.set(false);
NioWorker.write(NioSocketChannel.this, false);
}
}

View File

@ -320,7 +320,9 @@ class NioWorker implements Runnable {
if (mightNeedWakeup) {
NioWorker worker = channel.getWorker();
if (worker != null && Thread.currentThread() != worker.thread) {
worker.taskQueue.offer(channel.writeTask);
if (channel.writeTaskInTaskQueue.compareAndSet(false, true)) {
worker.taskQueue.offer(channel.writeTask);
}
if (worker.wakenUp.compareAndSet(false, true)) {
worker.selector.wakeup();
}