You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@jakarta.apache.org by Lindsay Patten <li...@roguewave.com> on 2001/02/28 04:22:52 UTC

[PATCH] Fix for race condition in SimplePool, fixes bug#728

[The bug site is down]

I understand the real problem now and here are new diffs for Bug 728.
The problem was that in trying to minimize the amount of code in the
synchronized blocks a race condition was created.  The assignment
and reading of the array values HAS to be part of the synchronized
block or values can be read before they are written.  The resulting
code is simpler, clearer, and actually more efficient.

--- src/org/apache/tomcat/util/SimplePool.java  Mon Feb 26 13:56:08 2001
+++ /tmp/org/apache/tomcat/util/SimplePool.java Tue Feb 27 21:17:21 2001
@@ -63,11 +63,6 @@
 
 package org.apache.tomcat.util;
 
-import java.util.zip.*;
-import java.net.*;
-import java.util.*;
-import java.io.*;
-
 /**
  * Simple object pool. Based on ThreadPool and few other classes
  *
@@ -83,9 +78,6 @@
     private Object pool[];
 
     private int max;
-    private int minSpare;
-    private int maxSpare;
-
     private int current=-1;
 
     Object lock;
@@ -103,33 +95,32 @@
      * Add the object to the pool, silent nothing if the pool is full
      */
     public  void put(Object o) {
-       int idx=-1;
        synchronized( lock ) {
-           if( current < max )
-               idx=++current;
+           if( current < (max-1) ) {
+                current += 1;
+                pool[current] = o;
+            }
        }
-       if( idx > 0 ) 
-           pool[idx]=o;
     }
 
     /**
      * Get an object from the pool, null if the pool is empty.
      */
     public  Object get() {
-       int idx=-1;
+        Object item = null;
        synchronized( lock ) {
-           if( current >= 0 )
-               idx=current--;
+           if( current >= 0 ) {
+                item = pool[current];
+                current -= 1;
+            }
        }
-       if( idx >= 0  ) 
-           return pool[idx];
-       return null;
+       return item;
     }
 
-    /** Return the size of the pool
+    /**
+     * Return the size of the pool
      */
     public int getMax() {
        return max;
     }
-
 }

Re: [PATCH] Fix for race condition in SimplePool, fixes bug#728

Posted by "Pier P. Fumagalli" <pi...@betaversion.org>.
Lindsay Patten <li...@roguewave.com> wrote:
> 
> [The bug site is down]

Whops... Sorry, we were installing the new Disk Array (280 Gigs, slurp :)
and the new Backup Unit...

    Pier

-- 
----------------------------------------------------------------------------
Pier P. Fumagalli                              <ma...@betaversion.org>