Author: remm
Date: Sat Mar  4 11:46:44 2017
New Revision: 1785467

URL: http://svn.apache.org/viewvc?rev=1785467&view=rev
Log:
Continue some casual experimentation with HTTP/2. Apparently there is a 
"deadlock" with reads in some cases [like when shutting down and there's a 
blocked read, it's visible with the testsuite; I'll work on it].

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.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=1785467&r1=1785466&r2=1785467&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java Sat Mar  4 
11:46:44 2017
@@ -31,8 +31,6 @@ import java.nio.channels.ClosedChannelEx
 import java.nio.channels.CompletionHandler;
 import java.nio.channels.FileChannel;
 import java.nio.channels.NetworkChannel;
-import java.nio.channels.ReadPendingException;
-import java.nio.channels.WritePendingException;
 import java.nio.file.StandardOpenOption;
 import java.util.ArrayList;
 import java.util.concurrent.ExecutionException;
@@ -834,7 +832,7 @@ public class Nio2Endpoint extends Abstra
 
         @Override
         public boolean hasAsyncIO() {
-            return false;
+            return true;
         }
 
         /**
@@ -975,20 +973,30 @@ public class Nio2Endpoint extends Abstra
         public <A> CompletionState read(ByteBuffer[] dsts, int offset, int 
length,
                 boolean block, long timeout, TimeUnit unit, A attachment,
                 CompletionCheck check, CompletionHandler<Long, ? super A> 
handler) {
-            OperationState<A> state = new OperationState<>(dsts, offset, 
length, timeout, unit, attachment, check, handler);
-            try {
-                if ((!block && readPending.tryAcquire()) || (block && 
readPending.tryAcquire(timeout, unit))) {
-                    Nio2Endpoint.startInline();
-                    getSocket().read(dsts, offset, length, timeout, unit, 
state, new ScatterReadCompletionHandler<>());
-                    Nio2Endpoint.endInline();
-                } else {
-                    throw new ReadPendingException();
+            if (block) {
+                try {
+                    if (!readPending.tryAcquire(timeout, unit)) {
+                        handler.failed(new SocketTimeoutException(), 
attachment);
+                        return CompletionState.ERROR;
+                    }
+                } catch (InterruptedException e) {
+                    handler.failed(e, attachment);
+                    return CompletionState.ERROR;
                 }
-                if (block && state.state == CompletionState.PENDING && 
readPending.tryAcquire(timeout, unit)) {
-                    readPending.release();
+            } else {
+                if (!readPending.tryAcquire()) {
+                    return CompletionState.NOT_DONE;
+                }
+            }
+            OperationState<A> state = new OperationState<>(dsts, offset, 
length, timeout, unit, attachment, check, handler);
+            Nio2Endpoint.startInline();
+            getSocket().read(dsts, offset, length, timeout, unit, state, new 
ScatterReadCompletionHandler<>());
+            Nio2Endpoint.endInline();
+            if (block && state.state == CompletionState.PENDING) {
+                if (!awaitReadComplete(timeout, unit)) {
+                    handler.failed(new SocketTimeoutException(), attachment);
+                    return CompletionState.ERROR;
                 }
-            } catch (InterruptedException e) {
-                handler.failed(e, attachment);
             }
             return state.state;
         }
@@ -1001,24 +1009,28 @@ public class Nio2Endpoint extends Abstra
         }
 
         @Override
-        public <A> CompletionState write(ByteBuffer[] srcs, int offset, int 
length,
+        public  <A> CompletionState write(ByteBuffer[] srcs, int offset, int 
length,
                 boolean block, long timeout, TimeUnit unit, A attachment,
                 CompletionCheck check, CompletionHandler<Long, ? super A> 
handler) {
-            OperationState<A> state = new OperationState<>(srcs, offset, 
length, timeout, unit, attachment, check, handler);
-            try {
-                if ((!block && writePending.tryAcquire()) || (block && 
writePending.tryAcquire(timeout, unit))) {
-                    Nio2Endpoint.startInline();
-                    getSocket().write(srcs, offset, length, timeout, unit, 
state, new GatherWriteCompletionHandler<>());
-                    Nio2Endpoint.endInline();
-                } else {
-                    throw new WritePendingException();
+            if (block) {
+                try {
+                    if (!writePending.tryAcquire(timeout, unit)) {
+                        handler.failed(new SocketTimeoutException(), 
attachment);
+                        return CompletionState.ERROR;
+                    }
+                } catch (InterruptedException e) {
+                    handler.failed(e, attachment);
+                    return CompletionState.ERROR;
                 }
-                if (block && state.state == CompletionState.PENDING && 
writePending.tryAcquire(timeout, unit)) {
-                    writePending.release();
+            } else {
+                if (!writePending.tryAcquire()) {
+                    return CompletionState.NOT_DONE;
                 }
-            } catch (InterruptedException e) {
-                handler.failed(e, attachment);
             }
+            OperationState<A> state = new OperationState<>(srcs, offset, 
length, timeout, unit, attachment, check, handler);
+            Nio2Endpoint.startInline();
+            getSocket().write(srcs, offset, length, timeout, unit, state, new 
GatherWriteCompletionHandler<>());
+            Nio2Endpoint.endInline();
             return state.state;
         }
 

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.java?rev=1785467&r1=1785466&r2=1785467&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.java Sat Mar 
 4 11:46:44 2017
@@ -770,6 +770,10 @@ public abstract class SocketWrapperBase<
          */
         PENDING,
         /**
+         * Operation was pending and non blocking.
+         */
+        NOT_DONE,
+        /**
          * The operation completed inline.
          */
         INLINE,



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

Reply via email to