You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/04/07 08:04:21 UTC

cvs commit: jakarta-avalon-excalibur/pool/src/java/org/apache/avalon/excalibur/pool Mutex.java AbstractPool.java

donaldp     02/04/06 22:04:21

  Modified:    pool     build.xml
               pool/src/java/org/apache/avalon/excalibur/pool
                        AbstractPool.java
  Added:       pool/src/java/org/apache/avalon/excalibur/pool Mutex.java
  Log:
  Copy a file from concurrent into pool so that is decoupled from concurrent.
  
  Revision  Changes    Path
  1.4       +0 -1      jakarta-avalon-excalibur/pool/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/pool/build.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- build.xml	6 Apr 2002 20:48:46 -0000	1.3
  +++ build.xml	7 Apr 2002 06:04:21 -0000	1.4
  @@ -64,7 +64,6 @@
           <pathelement location="${logkit.jar}"/>
           <pathelement location="${avalon-framework.jar}"/>
           <pathelement location="${excalibur-collections.jar}"/>
  -        <pathelement location="${excalibur-concurrent.jar}"/>
           <pathelement location="${junit.jar}"/>
           <pathelement location="${checkstyle.jar}"/>
       </path>
  
  
  
  1.2       +1 -2      jakarta-avalon-excalibur/pool/src/java/org/apache/avalon/excalibur/pool/AbstractPool.java
  
  Index: AbstractPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/pool/src/java/org/apache/avalon/excalibur/pool/AbstractPool.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractPool.java	4 Apr 2002 05:09:04 -0000	1.1
  +++ AbstractPool.java	7 Apr 2002 06:04:21 -0000	1.2
  @@ -11,7 +11,6 @@
   import java.util.List;
   import org.apache.avalon.excalibur.collections.Buffer;
   import org.apache.avalon.excalibur.collections.VariableSizeBuffer;
  -import org.apache.avalon.excalibur.concurrent.Mutex;
   import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.logger.LogKitLogger;
  @@ -22,7 +21,7 @@
    * This is an <code>Pool</code> that caches Poolable objects for reuse.
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/04/04 05:09:04 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/04/07 06:04:21 $
    * @since 4.0
    */
   public abstract class AbstractPool
  
  
  
  1.1                  jakarta-avalon-excalibur/pool/src/java/org/apache/avalon/excalibur/pool/Mutex.java
  
  Index: Mutex.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.pool;
  
  /**
   * This class implements a counting semaphore, also known as a
   * Dijkstra semaphore.  A semaphore is used to control access to
   * resources.  A counting semaphore has a count associated with it and
   * each acquire() call reduces the count.  A thread that tries to
   * acquire() a semaphore with a zero count blocks until someone else
   * calls release(), which increases the count.
   *
   * @version CVS $Revision: 1.1 $ $Date: 2002/04/07 06:04:21 $
   * @since 4.0
   */
  class Mutex
  {
      private long m_tokens;
  
      /**
       * Creates a semaphore with the specified number of tokens, which
       * determines the maximum number of acquisitions to allow.
       *
       * @param tokens the maximum number of acquisitions to allow
       */
      public Mutex()
      {
          m_tokens = 1;
      }
  
      public synchronized void acquire()
          throws InterruptedException
      {
          //TODO: check for interuption outside sync block?
          if( Thread.interrupted() ) throw new InterruptedException();
  
          //While there is no more tokens left wait
          while( 0 >= m_tokens )
          {
              wait();
          }
          m_tokens--;
      }
  
      public synchronized void release()
      {
          m_tokens++;
          notify();
      }
  
      public synchronized boolean attempt( final long msecs )
          throws InterruptedException
      {
          if( Thread.interrupted() ) throw new InterruptedException();
  
          if( m_tokens > 0 )
          {
              m_tokens--;
              return true;
          }
          else
          {
              final long start = System.currentTimeMillis();
              long wait = msecs;
  
              while( wait > 0 )
              {
                  wait( wait );
  
                  if( m_tokens > 0 )
                  {
                      m_tokens--;
                      return true;
                  }
                  else
                  {
                      wait = msecs - ( System.currentTimeMillis() - start );
                  }
              }
  
              return false;
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>