Suppress system calls when a user attempts to write an empty buffer

This commit is contained in:
Trustin Lee 2010-06-16 03:10:19 +00:00
parent 4b0219ee9b
commit f2d490ef9f
2 changed files with 24 additions and 15 deletions

View File

@ -530,7 +530,7 @@ class NioDatagramWorker implements Runnable {
long localWrittenBytes = 0; long localWrittenBytes = 0;
SocketAddress raddr = evt.getRemoteAddress(); SocketAddress raddr = evt.getRemoteAddress();
if (raddr == null) { if (raddr == null) {
for (int i = writeSpinCount; i > 0; i --) { for (int i = writeSpinCount; i > 0 && !buf.finished(); i --) {
localWrittenBytes = buf.transferTo(ch); localWrittenBytes = buf.transferTo(ch);
if (localWrittenBytes != 0) { if (localWrittenBytes != 0) {
writtenBytes += localWrittenBytes; writtenBytes += localWrittenBytes;
@ -538,7 +538,7 @@ class NioDatagramWorker implements Runnable {
} }
} }
} else { } else {
for (int i = writeSpinCount; i > 0; i --) { for (int i = writeSpinCount; i > 0 && !buf.finished(); i --) {
localWrittenBytes = buf.transferTo(ch, raddr); localWrittenBytes = buf.transferTo(ch, raddr);
if (localWrittenBytes != 0) { if (localWrittenBytes != 0) {
writtenBytes += localWrittenBytes; writtenBytes += localWrittenBytes;

View File

@ -15,21 +15,18 @@
*/ */
package org.jboss.netty.channel.socket.nio; package org.jboss.netty.channel.socket.nio;
import org.jboss.netty.buffer.ChannelBuffer; import static org.jboss.netty.channel.Channels.*;
import org.jboss.netty.buffer.ChannelBufferFactory;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.SocketSendBufferPool.SendBuffer;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.ThreadRenamingRunnable;
import org.jboss.netty.util.internal.IoWorkerRunnable;
import org.jboss.netty.util.internal.LinkedTransferQueue;
import java.io.IOException; import java.io.IOException;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.*; import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.NotYetConnectedException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator; import java.util.Iterator;
import java.util.Queue; import java.util.Queue;
import java.util.Set; import java.util.Set;
@ -39,7 +36,19 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
import static org.jboss.netty.channel.Channels.*; import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferFactory;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelException;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.ReceiveBufferSizePredictor;
import org.jboss.netty.channel.socket.nio.SocketSendBufferPool.SendBuffer;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.util.ThreadRenamingRunnable;
import org.jboss.netty.util.internal.IoWorkerRunnable;
import org.jboss.netty.util.internal.LinkedTransferQueue;
/** /**
* *
@ -456,7 +465,7 @@ class NioWorker implements Runnable {
ChannelFuture future = evt.getFuture(); ChannelFuture future = evt.getFuture();
try { try {
long localWrittenBytes = 0; long localWrittenBytes = 0;
for (int i = writeSpinCount; i > 0; i --) { for (int i = writeSpinCount; i > 0 && !buf.finished(); i --) {
localWrittenBytes = buf.transferTo(ch); localWrittenBytes = buf.transferTo(ch);
if (localWrittenBytes != 0) { if (localWrittenBytes != 0) {
writtenBytes += localWrittenBytes; writtenBytes += localWrittenBytes;