Author: fhanik
Date: Tue Dec 21 17:40:57 2010
New Revision: 1051578

URL: http://svn.apache.org/viewvc?rev=1051578&view=rev
Log:
refactor latch usage, since its shared by all connectors

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java?rev=1051578&r1=1051577&r2=1051578&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java 
(original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/AbstractEndpoint.java Tue Dec 
21 17:40:57 2010
@@ -126,7 +126,7 @@ public abstract class AbstractEndpoint {
     /**
      * counter for nr of connections handled by an endpoint
      */
-    protected volatile CounterLatch connectionCounterLatch = null;
+    private volatile CounterLatch connectionCounterLatch = null;
 
     /**
      * Socket properties
@@ -576,6 +576,43 @@ public abstract class AbstractEndpoint {
 
     protected abstract Log getLog();
     public abstract boolean getUseSendfile();
+    
+    protected CounterLatch initializeConnectionLatch() {
+        if (connectionCounterLatch==null) {
+            connectionCounterLatch = new CounterLatch(0,getMaxConnections());
+        }
+        return connectionCounterLatch;
+    }
+    
+    protected void releaseConnectionLatch() {
+        CounterLatch latch = connectionCounterLatch;
+        if (latch!=null) latch.releaseAll();
+        connectionCounterLatch = null;
+    }
+    
+    protected void awaitConnection() throws InterruptedException {
+        CounterLatch latch = connectionCounterLatch;
+        if (latch!=null) latch.await();
+    }
+    
+    protected long countUpConnection() {
+        CounterLatch latch = connectionCounterLatch;
+        if (latch!=null) return latch.countUp();
+        else return -1;
+    }
+    
+    protected long countDownConnection() {
+        CounterLatch latch = connectionCounterLatch;
+        if (latch!=null) {
+            long result = latch.countDown();
+            if (result<0) {
+                getLog().warn("Incorrect connection count, multiple 
socket.close called on the same socket." );
+            }
+            return result;
+        } else return -1;
+    }
+    
+    
 
     // --------------------  SSL related properties --------------------
 

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java?rev=1051578&r1=1051577&r2=1051578&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java Tue Dec 21 
17:40:57 2010
@@ -40,6 +40,7 @@ import org.apache.tomcat.jni.SSLSocket;
 import org.apache.tomcat.jni.Socket;
 import org.apache.tomcat.jni.Status;
 import org.apache.tomcat.util.ExceptionUtils;
+import org.apache.tomcat.util.threads.CounterLatch;
 
 
 /**
@@ -531,6 +532,8 @@ public class AprEndpoint extends Abstrac
             if (getExecutor() == null) {
                 createExecutor();
             }
+            
+            initializeConnectionLatch();
 
             // Start poller threads
             pollers = new Poller[pollerThreadCount];
@@ -592,6 +595,7 @@ public class AprEndpoint extends Abstrac
      */
     @Override
     public void stopInternal() {
+        releaseConnectionLatch();
         if (!paused) {
             pause();
         }
@@ -885,6 +889,7 @@ public class AprEndpoint extends Abstrac
             // parent pool or acceptor socket.
             // In any case disable double free which would cause JVM core.
             Socket.destroy(socket);
+            countDownConnection();
         }
     }
 
@@ -926,8 +931,12 @@ public class AprEndpoint extends Abstrac
                     break;
                 }
                 try {
+                    //if we have reached max connections, wait
+                    awaitConnection();
                     // Accept the next incoming connection from the server 
socket
                     long socket = Socket.accept(serverSock);
+                    //increment socket count
+                    countUpConnection();
                     /*
                      * In the case of a deferred accept unlockAccept needs to
                      * send data. This data will be rubbish, so destroy the

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java?rev=1051578&r1=1051577&r2=1051578&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/JIoEndpoint.java Tue Dec 21 
17:40:57 2010
@@ -201,7 +201,7 @@ public class JIoEndpoint extends Abstrac
                 }
                 try {
                     //if we have reached max connections, wait
-                    connectionCounterLatch.await();
+                    awaitConnection();                    
                     // Accept the next incoming connection from the server 
socket
                     Socket socket = 
serverSocketFactory.acceptSocket(serverSocket);
                     
@@ -216,7 +216,7 @@ public class JIoEndpoint extends Abstrac
                                 // Ignore
                             }
                         } else {
-                            connectionCounterLatch.countUp();
+                            countUpConnection();
                         }
                     } else {
                         // Close socket right away
@@ -293,7 +293,7 @@ public class JIoEndpoint extends Abstrac
                         if (log.isTraceEnabled()) {
                             log.trace("Closing socket:"+socket);
                         }
-                        connectionCounterLatch.countDown();
+                        countDownConnection();
                         try {
                             socket.getSocket().close();
                         } catch (IOException e) {
@@ -382,7 +382,7 @@ public class JIoEndpoint extends Abstrac
                 createExecutor();
             }
             
-            connectionCounterLatch = new CounterLatch(0,getMaxConnections());
+            initializeConnectionLatch();
 
             // Start acceptor threads
             for (int i = 0; i < acceptorThreadCount; i++) {
@@ -404,8 +404,7 @@ public class JIoEndpoint extends Abstrac
 
     @Override
     public void stopInternal() {
-        connectionCounterLatch.releaseAll();
-        connectionCounterLatch = null;
+        releaseConnectionLatch();
         if (!paused) {
             pause();
         }

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=1051578&r1=1051577&r2=1051578&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Tue Dec 21 
17:40:57 2010
@@ -567,13 +567,14 @@ public class NioEndpoint extends Abstrac
         if (!running) {
             running = true;
             paused = false;
-            
-            connectionCounterLatch = new CounterLatch(0, getMaxConnections()); 
+             
             // Create worker collection
             if ( getExecutor() == null ) {
                 createExecutor();
             }
 
+            initializeConnectionLatch();
+            
             // Start poller threads
             pollers = new Poller[getPollerThreadCount()];
             for (int i=0; i<pollers.length; i++) {
@@ -600,8 +601,7 @@ public class NioEndpoint extends Abstrac
      */
     @Override
     public void stopInternal() {
-        connectionCounterLatch.releaseAll();
-        connectionCounterLatch = null;
+        releaseConnectionLatch();
         if (!paused) {
             pause();
         }
@@ -813,7 +813,7 @@ public class NioEndpoint extends Abstrac
                 }
                 try {
                     //if we have reached max connections, wait
-                    connectionCounterLatch.await();
+                    awaitConnection();
                     // Accept the next incoming connection from the server 
socket
                     SocketChannel socket = serverSock.accept();
                     // Hand this socket off to an appropriate processor
@@ -831,7 +831,7 @@ public class NioEndpoint extends Abstrac
                                     log.debug("", ix);
                             }
                         } else {
-                            connectionCounterLatch.countUp();
+                            countUpConnection();
                         }
                     }
                 } catch (SocketTimeoutException sx) {
@@ -1066,9 +1066,7 @@ public class NioEndpoint extends Abstrac
                 try {if (ka!=null && ka.getSendfileData()!=null && 
ka.getSendfileData().fchannel!=null && ka.getSendfileData().fchannel.isOpen()) 
ka.getSendfileData().fchannel.close();}catch (Exception ignore){}
                 if (ka!=null) {
                     ka.reset();
-                    if (connectionCounterLatch.countDown()<0) {
-                        log.warn("Incorrect connection count, multiple cancel 
called on the same key?" );
-                    }
+                    countDownConnection(); 
                 }
             } catch (Throwable e) {
                 ExceptionUtils.handleThrowable(e);



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

Reply via email to