Author: markt
Date: Tue Aug 12 08:10:23 2008
New Revision: 685177

URL: http://svn.apache.org/viewvc?rev=685177&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=45608
Use AtomicInteger for countAllocated to prevent race conditions. 

Modified:
    tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java

Modified: tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java?rev=685177&r1=685176&r2=685177&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java (original)
+++ tomcat/trunk/java/org/apache/catalina/core/StandardWrapper.java Tue Aug 12 
08:10:23 2008
@@ -25,6 +25,7 @@
 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 @@
      * 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 int getCountAllocated() {
 
-        return (this.countAllocated);
+        return (this.countAllocated.get());
 
     }
 
@@ -784,7 +785,7 @@
                             // condition with unload. Bug 43683, test case #3
                             if (!singleThreadModel) {
                                 newInstance = true;
-                                countAllocated++;
+                                countAllocated.incrementAndGet();
                             }
                         } catch (ServletException e) {
                             throw e;
@@ -802,7 +803,7 @@
                 // For new instances, count will have been incremented at the
                 // time of creation
                 if (!newInstance) {
-                    countAllocated++;
+                    countAllocated.incrementAndGet();
                 }
                 return (instance);
             }
@@ -810,7 +811,7 @@
 
         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 @@
             }
             if (log.isTraceEnabled())
                 log.trace("  Returning allocated STM instance");
-            countAllocated++;
+            countAllocated.incrementAndGet();
             return (Servlet) instancePool.pop();
 
         }
@@ -853,13 +854,13 @@
 
         // 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 @@
 
         // 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);



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to