[jboss-cvs] JBossCache/src/org/jboss/cache/lock ...

Manik Surtani manik at jboss.org
Tue Jun 19 12:40:01 EDT 2007


  User: msurtani
  Date: 07/06/19 12:40:01

  Modified:    src/org/jboss/cache/lock  StripedLock.java
  Log:
  proper reentrancy for acquiring locks, as well as idempotency for releasing locks
  
  Revision  Changes    Path
  1.5       +23 -3     JBossCache/src/org/jboss/cache/lock/StripedLock.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: StripedLock.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/lock/StripedLock.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- StripedLock.java	19 Jun 2007 15:41:34 -0000	1.4
  +++ StripedLock.java	19 Jun 2007 16:40:01 -0000	1.5
  @@ -1,6 +1,8 @@
   package org.jboss.cache.lock;
   
   import net.jcip.annotations.ThreadSafe;
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
   import org.jboss.cache.Fqn;
   
   import java.util.List;
  @@ -28,6 +30,7 @@
      private int lockSegmentShift;
   
      ReentrantReadWriteLock[] sharedLocks;
  +   private Log log = LogFactory.getLog(StripedLock.class);
   
      /**
       * This constructor just calls {@link #StripedLock(int)} with a default concurrency value of 20.
  @@ -70,13 +73,16 @@
         ReentrantReadWriteLock lock = getLock(fqn);
   
         if (exclusive)
  +      {
  +         // allow for reentrancy
            lock.writeLock().lock();
  +      }
         else
            lock.readLock().lock();
      }
   
      /**
  -    * Releases a lock the caller may be holding.
  +    * Releases a lock the caller may be holding. This method is idempotent.
       *
       * @param fqn the Fqn to release
       */
  @@ -84,10 +90,24 @@
      {
         ReentrantReadWriteLock lock = getLock(fqn);
         if (lock.isWriteLockedByCurrentThread())
  +      {
            lock.writeLock().unlock();
  +         // check that we still don't have a stale WL
  +         if (lock.isWriteLockedByCurrentThread() && log.isWarnEnabled())
  +            log.warn("Write lock still exists on Fqn " + fqn + " for current thread.  Perhaps this was write-locked more than once?");
  +      }
         else
  +      {
  +         try
  +         {
            lock.readLock().unlock();
      }
  +         catch (IllegalMonitorStateException imse)
  +         {
  +            // perhaps the RL was already released earlier?
  +         }
  +      }
  +   }
   
      ReentrantReadWriteLock getLock(Object o)
      {
  @@ -118,7 +138,7 @@
      }
   
      /**
  -    * Releases locks on all fqns passed in.  Makes multiple calls to {@link #releaseLock(org.jboss.cache.Fqn)}
  +    * Releases locks on all fqns passed in.  Makes multiple calls to {@link #releaseLock(org.jboss.cache.Fqn)}.  This method is idempotent.
       *
       * @param fqns list of fqns
       * @see #releaseLock(org.jboss.cache.Fqn)
  
  
  



More information about the jboss-cvs-commits mailing list