Skip to content

Commit

Permalink
Pull up writeBufferFlipped flag
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1648897 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Jan 1, 2015
1 parent eb2bd57 commit 205d787
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 40 deletions.
2 changes: 2 additions & 0 deletions java/org/apache/coyote/http11/AbstractOutputBuffer.java
Expand Up @@ -101,6 +101,7 @@ public abstract class AbstractOutputBuffer<S> implements OutputBuffer {
protected long byteCount = 0; protected long byteCount = 0;


protected ByteBuffer socketWriteBuffer; protected ByteBuffer socketWriteBuffer;
protected volatile boolean writeBufferFlipped;


/** /**
* For "non-blocking" writes use an external set of buffers. Although the * For "non-blocking" writes use an external set of buffers. Although the
Expand Down Expand Up @@ -314,6 +315,7 @@ public void recycle() {
// Sub-classes may wish to do more than this. // Sub-classes may wish to do more than this.
nextRequest(); nextRequest();
bufferedWrites.clear(); bufferedWrites.clear();
writeBufferFlipped = false;
} }


/** /**
Expand Down
20 changes: 6 additions & 14 deletions java/org/apache/coyote/http11/InternalAprOutputBuffer.java
Expand Up @@ -71,13 +71,6 @@ public InternalAprOutputBuffer(Response response, int headerBufferSize) {
private SocketWrapperBase<Long> wrapper; private SocketWrapperBase<Long> wrapper;




/**
* <code>false</code> if socketWriteBuffer is ready to be written to and
* <code>true</code> is ready to be read from.
*/
private volatile boolean flipped = false;


private AbstractEndpoint<Long> endpoint; private AbstractEndpoint<Long> endpoint;




Expand All @@ -103,7 +96,6 @@ public void init(SocketWrapperBase<Long> socketWrapper,
public void recycle() { public void recycle() {
super.recycle(); super.recycle();
socketWriteBuffer.clear(); socketWriteBuffer.clear();
flipped = false;
socket = 0; socket = 0;
wrapper = null; wrapper = null;
} }
Expand Down Expand Up @@ -153,7 +145,7 @@ private synchronized void addToBB(byte[] buf, int offset, int length)


// If bbuf is currently being used for writes, add this data to the // If bbuf is currently being used for writes, add this data to the
// write buffer // write buffer
if (flipped) { if (writeBufferFlipped) {
addToBuffers(buf, offset, length); addToBuffers(buf, offset, length);
return; return;
} }
Expand Down Expand Up @@ -264,8 +256,8 @@ private synchronized void writeToSocket(boolean block) throws IOException {
} }


private synchronized void writeToSocket() throws IOException { private synchronized void writeToSocket() throws IOException {
if (!flipped) { if (!writeBufferFlipped) {
flipped = true; writeBufferFlipped = true;
socketWriteBuffer.flip(); socketWriteBuffer.flip();
} }


Expand All @@ -283,7 +275,7 @@ private synchronized void writeToSocket() throws IOException {


if (socketWriteBuffer.remaining() == 0) { if (socketWriteBuffer.remaining() == 0) {
socketWriteBuffer.clear(); socketWriteBuffer.clear();
flipped = false; writeBufferFlipped = false;
} }
// If there is data left in the buffer the socket will be registered for // If there is data left in the buffer the socket will be registered for
// write further up the stack. This is to ensure the socket is only // write further up the stack. This is to ensure the socket is only
Expand All @@ -305,8 +297,8 @@ private void transfer(ByteBuffer from, ByteBuffer to) {


@Override @Override
protected synchronized boolean hasMoreDataToFlush() { protected synchronized boolean hasMoreDataToFlush() {
return (flipped && socketWriteBuffer.remaining() > 0) || return (writeBufferFlipped && socketWriteBuffer.remaining() > 0) ||
(!flipped && socketWriteBuffer.position() > 0); (!writeBufferFlipped && socketWriteBuffer.position() > 0);
} }




Expand Down
24 changes: 9 additions & 15 deletions java/org/apache/coyote/http11/InternalNio2OutputBuffer.java
Expand Up @@ -66,11 +66,6 @@ public InternalNio2OutputBuffer(Response response, int headerBufferSize) {
*/ */
protected volatile boolean interest = false; protected volatile boolean interest = false;


/**
* Track if the byte buffer is flipped
*/
protected volatile boolean flipped = false;

/** /**
* The completion handler used for asynchronous write operations * The completion handler used for asynchronous write operations
*/ */
Expand Down Expand Up @@ -226,7 +221,6 @@ public void recycle() {
super.recycle(); super.recycle();
socket = null; socket = null;
e = null; e = null;
flipped = false;
interest = false; interest = false;
if (writePending.availablePermits() != 1) { if (writePending.availablePermits() != 1) {
writePending.drainPermits(); writePending.drainPermits();
Expand All @@ -239,8 +233,8 @@ public void recycle() {
@Override @Override
public void nextRequest() { public void nextRequest() {
super.nextRequest(); super.nextRequest();
flipped = false;
interest = false; interest = false;
writeBufferFlipped = false;
} }


// ------------------------------------------------ HTTP/1.1 Output Methods // ------------------------------------------------ HTTP/1.1 Output Methods
Expand Down Expand Up @@ -386,9 +380,9 @@ private boolean flushBufferInternal(boolean block, boolean hasPermit) throws IOE
} }
bufferedWrites.clear(); bufferedWrites.clear();
} }
if (!flipped) { if (!writeBufferFlipped) {
socketWriteBuffer.flip(); socketWriteBuffer.flip();
flipped = true; writeBufferFlipped = true;
} }
while (socketWriteBuffer.hasRemaining()) { while (socketWriteBuffer.hasRemaining()) {
if (socket.getSocket().write(socketWriteBuffer).get(socket.getTimeout(), TimeUnit.MILLISECONDS).intValue() < 0) { if (socket.getSocket().write(socketWriteBuffer).get(socket.getTimeout(), TimeUnit.MILLISECONDS).intValue() < 0) {
Expand All @@ -407,14 +401,14 @@ private boolean flushBufferInternal(boolean block, boolean hasPermit) throws IOE
throw new SocketTimeoutException(); throw new SocketTimeoutException();
} }
socketWriteBuffer.clear(); socketWriteBuffer.clear();
flipped = false; writeBufferFlipped = false;
return false; return false;
} else { } else {
synchronized (completionHandler) { synchronized (completionHandler) {
if (hasPermit || writePending.tryAcquire()) { if (hasPermit || writePending.tryAcquire()) {
if (!flipped) { if (!writeBufferFlipped) {
socketWriteBuffer.flip(); socketWriteBuffer.flip();
flipped = true; writeBufferFlipped = true;
} }
Nio2Endpoint.startInline(); Nio2Endpoint.startInline();
if (bufferedWrites.size() > 0) { if (bufferedWrites.size() > 0) {
Expand Down Expand Up @@ -443,7 +437,7 @@ private boolean flushBufferInternal(boolean block, boolean hasPermit) throws IOE
if (writePending.availablePermits() > 0) { if (writePending.availablePermits() > 0) {
if (socketWriteBuffer.remaining() == 0) { if (socketWriteBuffer.remaining() == 0) {
socketWriteBuffer.clear(); socketWriteBuffer.clear();
flipped = false; writeBufferFlipped = false;
} }
} }
} }
Expand All @@ -462,8 +456,8 @@ public boolean hasDataToWrite() {


@Override @Override
protected boolean hasMoreDataToFlush() { protected boolean hasMoreDataToFlush() {
return (flipped && socketWriteBuffer.remaining() > 0) || return (writeBufferFlipped && socketWriteBuffer.remaining() > 0) ||
(!flipped && socketWriteBuffer.position() > 0); (!writeBufferFlipped && socketWriteBuffer.position() > 0);
} }


@Override @Override
Expand Down
16 changes: 5 additions & 11 deletions java/org/apache/coyote/http11/InternalNioOutputBuffer.java
Expand Up @@ -63,11 +63,6 @@ public InternalNioOutputBuffer(Response response, int headerBufferSize) {
*/ */
private NioSelectorPool pool; private NioSelectorPool pool;


/**
* Track if the byte buffer is flipped
*/
protected volatile boolean flipped = false;



// --------------------------------------------------------- Public Methods // --------------------------------------------------------- Public Methods


Expand All @@ -89,7 +84,6 @@ public void init(SocketWrapperBase<NioChannel> socketWrapper,
public void recycle() { public void recycle() {
super.recycle(); super.recycle();
socketWriteBuffer.clear(); socketWriteBuffer.clear();
flipped = false;
socket = null; socket = null;
} }


Expand Down Expand Up @@ -120,7 +114,7 @@ public void sendAck() throws IOException {
private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean block, boolean flip) throws IOException { private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean block, boolean flip) throws IOException {
if ( flip ) { if ( flip ) {
bytebuffer.flip(); bytebuffer.flip();
flipped = true; writeBufferFlipped = true;
} }


int written = 0; int written = 0;
Expand All @@ -146,7 +140,7 @@ private synchronized int writeToSocket(ByteBuffer bytebuffer, boolean block, boo
//blocking writes must empty the buffer //blocking writes must empty the buffer
//and if remaining==0 then we did empty it //and if remaining==0 then we did empty it
bytebuffer.clear(); bytebuffer.clear();
flipped = false; writeBufferFlipped = false;
} }
// If there is data left in the buffer the socket will be registered for // If there is data left in the buffer the socket will be registered for
// write further up the stack. This is to ensure the socket is only // write further up the stack. This is to ensure the socket is only
Expand Down Expand Up @@ -238,7 +232,7 @@ protected boolean flushBuffer(boolean block) throws IOException {


//write to the socket, if there is anything to write //write to the socket, if there is anything to write
if (dataLeft) { if (dataLeft) {
writeToSocket(socketWriteBuffer, block, !flipped); writeToSocket(socketWriteBuffer, block, !writeBufferFlipped);
} }


dataLeft = hasMoreDataToFlush(); dataLeft = hasMoreDataToFlush();
Expand All @@ -265,8 +259,8 @@ protected boolean flushBuffer(boolean block) throws IOException {


@Override @Override
protected boolean hasMoreDataToFlush() { protected boolean hasMoreDataToFlush() {
return (flipped && socketWriteBuffer.remaining() > 0) || return (writeBufferFlipped && socketWriteBuffer.remaining() > 0) ||
(!flipped && socketWriteBuffer.position() > 0); (!writeBufferFlipped && socketWriteBuffer.position() > 0);
} }




Expand Down

0 comments on commit 205d787

Please sign in to comment.