Author: markt
Date: Fri Jan 16 10:53:21 2015
New Revision: 1652384

URL: http://svn.apache.org/r1652384
Log:
Streamlining
 - reduce scope of sync (in really only needs to surrond non-blocking
read and readInterest)
 - make fillReadBuffer responsible for releasing readPending semaphore
in blocking case (it already did it for non-blcoking)

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java?rev=1652384&r1=1652383&r2=1652384&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java Fri Jan 16 
10:53:21 2015
@@ -1014,38 +1014,34 @@ public class Nio2Endpoint extends Abstra
                 }
             }
 
-            synchronized (readCompletionHandler) {
-                socketBufferHandler.configureReadBufferForRead();
+            socketBufferHandler.configureReadBufferForRead();
 
-                int remaining = 
socketBufferHandler.getReadBuffer().remaining();
+            int remaining = socketBufferHandler.getReadBuffer().remaining();
 
-                // Is there enough data in the read buffer to satisfy this 
request?
-                if (remaining >= len) {
-                    socketBufferHandler.getReadBuffer().get(b, off, len);
-                    if (log.isDebugEnabled()) {
-                        log.debug("Socket: [" + this + "], Read from buffer: 
[" + len + "]");
-                    }
-                    readPending.release();
-                    return len;
+            // Is there enough data in the read buffer to satisfy this request?
+            if (remaining >= len) {
+                socketBufferHandler.getReadBuffer().get(b, off, len);
+                if (log.isDebugEnabled()) {
+                    log.debug("Socket: [" + this + "], Read from buffer: [" + 
len + "]");
                 }
+                readPending.release();
+                return len;
+            }
 
-                // Copy what data there is in the read buffer to the byte array
-                if (remaining > 0) {
-                    socketBufferHandler.getReadBuffer().get(b, off, remaining);
-                    // This may be sufficient to complete the request and we
-                    // don't want to trigger another read since if there is no
-                    // more data to read and this request takes a while to
-                    // process the read will timeout triggering an error.
-                    readPending.release();
-                    return remaining;
-                }
+            // Copy what data there is in the read buffer to the byte array
+            if (remaining > 0) {
+                socketBufferHandler.getReadBuffer().get(b, off, remaining);
+                // This may be sufficient to complete the request and we
+                // don't want to trigger another read since if there is no
+                // more data to read and this request takes a while to
+                // process the read will timeout triggering an error.
+                readPending.release();
+                return remaining;
+            }
 
+            synchronized (readCompletionHandler) {
                 // Fill the read buffer as best we can.
                 int nRead = fillReadBuffer(block);
-                if (block) {
-                    // Just did a blocking read so release the semaphore
-                    readPending.release();
-                }
 
                 // Fill as much of the remaining byte array as possible with 
the
                 // data that was just read
@@ -1091,6 +1087,9 @@ public class Nio2Endpoint extends Abstra
         /* Callers of this method must:
          * - have acquired the readPending semaphore
          * - have acquired a lock on readCompletionHandler
+         *
+         * This method will release (or arrange for the release of) the
+         * readPending semaphore once the read has completed.
          */
         private int fillReadBuffer(boolean block) throws IOException {
             socketBufferHandler.configureReadBufferForWrite();
@@ -1099,6 +1098,7 @@ public class Nio2Endpoint extends Abstra
                 try {
                     nRead = 
getSocket().read(socketBufferHandler.getReadBuffer()).get(
                             getTimeout(), TimeUnit.MILLISECONDS).intValue();
+                    readPending.release();
                 } catch (ExecutionException e) {
                     if (e.getCause() instanceof IOException) {
                         throw (IOException) e.getCause();



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to