geirm       01/03/30 21:25:22

  Modified:    src/java/org/apache/velocity/util SimplePool.java
  Log:
  A few fixes to fix what I am sure was a synchronization problem, and
  further, I am pretty convinced there was a hole at the bottom of the
  array, which made life really unpleasant when we ran out of parsers.
  
  I will review again tomorrow as I am pretty beat right now...
  
  Revision  Changes    Path
  1.2       +84 -42    
jakarta-velocity/src/java/org/apache/velocity/util/SimplePool.java
  
  Index: SimplePool.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/SimplePool.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SimplePool.java   2000/11/12 16:20:43     1.1
  +++ SimplePool.java   2001/03/31 05:25:22     1.2
  @@ -1,15 +1,15 @@
   package org.apache.velocity.util;
   
   /*
  - * $Header: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/SimplePool.java,v 1.1 
2000/11/12 16:20:43 jvanzyl Exp $
  - * $Revision: 1.1 $
  - * $Date: 2000/11/12 16:20:43 $
  + * $Header: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/SimplePool.java,v 1.2 
2001/03/31 05:25:22 geirm Exp $
  + * $Revision: 1.2 $
  + * $Date: 2001/03/31 05:25:22 $
    *
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -31,7 +31,7 @@
    *    Alternately, this acknowlegement may appear in the software itself,
    *    if and wherever such third-party acknowlegements normally appear.
    *
  - * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  + * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
    *    Foundation" must not be used to endorse or promote products derived
    *    from this software without prior written permission. For written
    *    permission, please contact [EMAIL PROTECTED]
  @@ -63,11 +63,6 @@
    *
    */
   
  -import java.util.zip.*;
  -import java.net.*;
  -import java.util.*;
  -import java.io.*;
  -
   /**
    * Simple object pool. Based on ThreadPool and few other classes
    *
  @@ -75,57 +70,104 @@
    *
    * @author Gal Shachor
    * @author Costin
  + * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  + * @version $Id: SimplePool.java,v 1.2 2001/03/31 05:25:22 geirm Exp $
    */
  -public final class SimplePool  {
  +public final class SimplePool  
  +{
       /*
  -     * Where the threads are held.
  +     * Where the objects are held.
        */
       private Object pool[];
  -
  -    private int max;
  -    private int minSpare;
  -    private int maxSpare;
   
  -    private int current=-1;
  -
  -    Object lock;
  +    /**
  +     *  max amount of objects to be managed
  +     *  set via CTOR
  +     */
  +    private int max;    
       
  -    public SimplePool(int max) {
  -     this.max=max;
  -     pool=new Object[max];
  -     lock=new Object();
  -    }
  +    /**
  +     *  index of previous to next
  +     *  free slot
  +     */
  +    private int current=-1;
  +       
  +    public SimplePool(int max) 
  +    {
  +        this.max = max;
  +        pool = new Object[max];
  +    } 
   
       /**
        * 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( idx > 0 ) 
  -         pool[idx]=o;
  +    public void put(Object o) 
  +    {
  +        int idx=-1;
  +     
  +        synchronized( this ) 
  +        {
  +            /*
  +             *  if we aren't full
  +             */
  +
  +            if( current < max - 1 )
  +            {
  +                /*
  +                 *  then increment the 
  +                 *  current index.
  +                 */
  +                idx = ++current;
  +            }
  +
  +            if( idx >= 0 ) 
  +            {
  +                pool[idx] = o;
  +            }
  +        }
       }
   
       /**
        * Get an object from the pool, null if the pool is empty.
        */
  -    public  Object get() {
  -     int idx=-1;
  -     synchronized( lock ) {
  -         if( current >= 0 )
  -             idx=current--;
  -     }
  -     if( idx >= 0  ) 
  -         return pool[idx];
  -     return null;
  +    public  Object get() 
  +    {
  +        int idx = -1;
  +        
  +        synchronized( this ) 
  +        {
  +            /*
  +             *  if we have any in the pool
  +             */
  +            if( current >= 0 )
  +            {
  +                /*
  +                 *  take one out, so to speak -
  +                 *  separate the two operations
  +                 *  to make it clear that you
  +                 *  don't want idx = --current; :)
  +                 */
  +
  +                idx = current;
  +                current--;
  +               
  +                /*
  +                 *  and since current was >= 0
  +                 *  to get in here, idx must be as well
  +                 *  so save the if() opration
  +                 */
  +
  +                return pool[idx];
  +            }
  +        }
  +        
  +        return null;
       }
   
       /** Return the size of the pool
        */
  -    public int getMax() {
  -     return max;
  +    public int getMax() 
  +    {
  +        return max;
       }
   }
  
  
  

Reply via email to