Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=45608
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 12 Aug 2008 15:10:23 +0000 (15:10 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 12 Aug 2008 15:10:23 +0000 (15:10 +0000)
Use AtomicInteger for countAllocated to prevent race conditions.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@685177 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/core/StandardWrapper.java

index b7e58a9..41c5b73 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Stack;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.management.ListenerNotFoundException;
 import javax.management.MBeanNotificationInfo;
@@ -114,7 +115,7 @@ public class StandardWrapper
      * The count of allocations that are currently active (even if they
      * are for the same instance, as will be true on a non-STM servlet).
      */
-    protected int countAllocated = 0;
+    protected AtomicInteger countAllocated = new AtomicInteger(0);
 
 
     /**
@@ -314,7 +315,7 @@ public class StandardWrapper
      */
     public int getCountAllocated() {
 
-        return (this.countAllocated);
+        return (this.countAllocated.get());
 
     }
 
@@ -784,7 +785,7 @@ public class StandardWrapper
                             // condition with unload. Bug 43683, test case #3
                             if (!singleThreadModel) {
                                 newInstance = true;
-                                countAllocated++;
+                                countAllocated.incrementAndGet();
                             }
                         } catch (ServletException e) {
                             throw e;
@@ -802,7 +803,7 @@ public class StandardWrapper
                 // For new instances, count will have been incremented at the
                 // time of creation
                 if (!newInstance) {
-                    countAllocated++;
+                    countAllocated.incrementAndGet();
                 }
                 return (instance);
             }
@@ -810,7 +811,7 @@ public class StandardWrapper
 
         synchronized (instancePool) {
 
-            while (countAllocated >= nInstances) {
+            while (countAllocated.get() >= nInstances) {
                 // Allocate a new instance if possible, or else wait
                 if (nInstances < maxInstances) {
                     try {
@@ -832,7 +833,7 @@ public class StandardWrapper
             }
             if (log.isTraceEnabled())
                 log.trace("  Returning allocated STM instance");
-            countAllocated++;
+            countAllocated.incrementAndGet();
             return (Servlet) instancePool.pop();
 
         }
@@ -853,13 +854,13 @@ public class StandardWrapper
 
         // If not SingleThreadModel, no action is required
         if (!singleThreadModel) {
-            countAllocated--;
+            countAllocated.decrementAndGet();
             return;
         }
 
         // Unlock and free this instance
         synchronized (instancePool) {
-            countAllocated--;
+            countAllocated.decrementAndGet();
             instancePool.push(servlet);
             instancePool.notify();
         }
@@ -1250,13 +1251,13 @@ public class StandardWrapper
 
         // Loaf a while if the current instance is allocated
         // (possibly more than once if non-STM)
-        if (countAllocated > 0) {
+        if (countAllocated.get() > 0) {
             int nRetries = 0;
             long delay = unloadDelay / 20;
-            while ((nRetries < 21) && (countAllocated > 0)) {
+            while ((nRetries < 21) && (countAllocated.get() > 0)) {
                 if ((nRetries % 10) == 0) {
                     log.info(sm.getString("standardWrapper.waiting",
-                                          new Integer(countAllocated)));
+                                          countAllocated.toString()));
                 }
                 try {
                     Thread.sleep(delay);