tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1528174 - in /tomcat/tc7.0.x/trunk: ./ java/org/apache/tomcat/util/net/AprEndpoint.java
Date Tue, 01 Oct 2013 18:41:43 GMT
Author: markt
Date: Tue Oct  1 18:41:43 2013
New Revision: 1528174

URL: http://svn.apache.org/r1528174
Log:
Ensure that connectionCount remains thread-safe

Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java

Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
  Merged /tomcat/trunk:r1528166

Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java?rev=1528174&r1=1528173&r2=1528174&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java Tue Oct  1 18:41:43
2013
@@ -27,6 +27,7 @@ import java.util.concurrent.ConcurrentHa
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.Executor;
 import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
@@ -1307,10 +1308,17 @@ public class AprEndpoint extends Abstrac
 
 
         /**
-         * Amount of connections inside this poller.
+         * The number of connections currently inside this Poller. The correct
+         * operation of the Poller depends on this figure being correct. If it
+         * is not, it is possible that the Poller will enter a wait loop where
+         * it waits for the next connection to be added to the Poller before it
+         * calls poll when it should still be polling existing connections.
+         * Although not necessary at the time of writing this comment, it has
+         * been implemented as an AtomicInteger to ensure that it remains
+         * thread-safe.
          */
-        protected int connectionCount = 0;
-        public int getConnectionCount() { return connectionCount; }
+        private AtomicInteger connectionCount = new AtomicInteger(0);
+        public int getConnectionCount() { return connectionCount.get(); }
 
 
         private volatile boolean pollerRunning = true;
@@ -1366,7 +1374,7 @@ public class AprEndpoint extends Abstrac
             }
 
             desc = new long[actualPollerSize * 2];
-            connectionCount = 0;
+            connectionCount.set(0);
             addList = new SocketList(defaultPollerSize);
             closeList = new SocketList(defaultPollerSize);
         }
@@ -1425,7 +1433,7 @@ public class AprEndpoint extends Abstrac
                 }
             }
             Pool.destroy(pool);
-            connectionCount = 0;
+            connectionCount.set(0);
         }
 
 
@@ -1498,7 +1506,7 @@ public class AprEndpoint extends Abstrac
                     rv = Poll.add(pollers[i], socket, events);
                     if (rv == Status.APR_SUCCESS) {
                         pollerSpace[i]--;
-                        connectionCount++;
+                        connectionCount.incrementAndGet();
                         return true;
                     }
                 }
@@ -1532,7 +1540,7 @@ public class AprEndpoint extends Abstrac
                     rv = Poll.remove(pollers[i], socket);
                     if (rv != Status.APR_NOTFOUND) {
                         pollerSpace[i]++;
-                        connectionCount--;
+                        connectionCount.decrementAndGet();
                         break;
                     }
                 }
@@ -1614,7 +1622,7 @@ public class AprEndpoint extends Abstrac
                     }
                 }
                 // Check timeouts if the poller is empty
-                while (pollerRunning && connectionCount < 1 &&
+                while (pollerRunning && connectionCount.get() < 1 &&
                         addList.size() < 1) {
                     // Reset maintain time.
                     try {
@@ -1731,7 +1739,7 @@ public class AprEndpoint extends Abstrac
                         }
                         if (rv > 0) {
                             pollerSpace[i] += rv;
-                            connectionCount -= rv;
+                            connectionCount.addAndGet(-rv);
                             for (int n = 0; n < rv; n++) {
                                 timeouts.remove(desc[n*2+1]);
                                 AprSocketWrapper wrapper = connections.get(
@@ -1830,7 +1838,7 @@ public class AprEndpoint extends Abstrac
                             long newPoller = allocatePoller(actualPollerSize, pool, -1);
                             // Don't restore connections for now, since I have not tested
it
                             pollerSpace[i] = actualPollerSize;
-                            connectionCount -= count;
+                            connectionCount.addAndGet(-count);
                             Poll.destroy(pollers[i]);
                             pollers[i] = newPoller;
                         }



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


Mime
View raw message