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

Manik Surtani manik at jboss.org
Wed May 23 06:28:58 EDT 2007


  User: msurtani
  Date: 07/05/23 06:28:58

  Modified:    src/org/jboss/cache/lock  LockMap.java
  Log:
  Initiated a bunch of performance fixes, including replacing CopyOnWriteArraySets with org.jboss.cache.util.concurrent.ConcurrentHashSet.
  Also ran an imports optimiser on the code base - there were a lot of unused imports floating about.
  
  Revision  Changes    Path
  1.12      +28 -24    JBossCache/src/org/jboss/cache/lock/LockMap.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: LockMap.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/src/org/jboss/cache/lock/LockMap.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -b -r1.11 -r1.12
  --- LockMap.java	8 Dec 2006 18:50:49 -0000	1.11
  +++ LockMap.java	23 May 2007 10:28:58 -0000	1.12
  @@ -6,15 +6,16 @@
    */
   package org.jboss.cache.lock;
   
  +import org.jboss.cache.util.concurrent.ConcurrentHashSet;
  +
   import java.util.Collections;
   import java.util.Set;
  -import java.util.concurrent.CopyOnWriteArraySet;
   
   /**
    * Provide lock ownership mapping.
    *
    * @author Ben Wang
  - * @version $Id: LockMap.java,v 1.11 2006/12/08 18:50:49 genman Exp $
  + * @version $Id: LockMap.java,v 1.12 2007/05/23 10:28:58 msurtani Exp $
    */
   public class LockMap
   {
  @@ -22,13 +23,12 @@
      public static final int OWNER_READ = 1;
      public static final int OWNER_WRITE = 2;
   
  -   private Object writeOwner_=null;
  +   private Object writeOwner_ = null;
   
  -   /**
  -    * Set of owners.  As this set is small (not like 100+) this structure is
  -    * sufficient.
  -    */
  -   private final Set readOwnerList_ = new CopyOnWriteArraySet();
  +   // a CopyOnWriteArraySet is HUGELY inefficient since MANY LockMaps are created and are frequently read from.
  +   // private final Set readOwnerList_ = new CopyOnWriteArraySet();
  +
  +   private final Set readOwnerList_ = new ConcurrentHashSet();
   
      public LockMap()
      {
  @@ -42,7 +42,6 @@
       * @param ownership Either <code>OWNER_ANY</code>, <code>OWNER_READ</code>,
       *                  or <code>OWNER_WRITE</code>.
       * @return
  -    * 
       * @throws NullPointerException if <code>caller</code> is <code>null</code>.
       */
      public boolean isOwner(Object caller, int ownership)
  @@ -51,7 +50,8 @@
            and only the current thread can *change* the writer or readers, so this cannot happen while we read.
         */
   
  -      switch (ownership) {
  +      switch (ownership)
  +      {
            case OWNER_ANY:
               return (writeOwner_ != null && caller.equals(writeOwner_) || readOwnerList_.contains(caller));
            case OWNER_READ:
  @@ -64,7 +64,6 @@
      }
   
   
  -
      /**
       * Adding a reader owner.
       *
  @@ -82,18 +81,21 @@
       */
      public void setWriterIfNotNull(Object owner)
      {
  -      synchronized(this) {
  -         if(writeOwner_ != null)
  +      synchronized (this)
  +      {
  +         if (writeOwner_ != null)
               throw new IllegalStateException("there is already a writer holding the lock: " + writeOwner_);
  -         writeOwner_=owner;
  +         writeOwner_ = owner;
         }
      }
   
  -   private Object setWriter(Object owner) {
  +   private Object setWriter(Object owner)
  +   {
         Object old;
  -      synchronized(this) {
  -         old=writeOwner_;
  -         writeOwner_=owner;
  +      synchronized (this)
  +      {
  +         old = writeOwner_;
  +         writeOwner_ = owner;
         }
         return old;
      }
  @@ -108,7 +110,7 @@
      public boolean upgrade(Object owner) throws OwnerNotExistedException
      {
         boolean old_value = readOwnerList_.remove(owner);
  -      if(!old_value) // didn't exist in the list
  +      if (!old_value) // didn't exist in the list
            throw new OwnerNotExistedException("Can't upgrade lock. Read lock owner did not exist");
         setWriter(owner);
         return true;
  @@ -150,8 +152,9 @@
       */
      public void removeWriter()
      {
  -      synchronized(this) {
  -         writeOwner_=null;
  +      synchronized (this)
  +      {
  +         writeOwner_ = null;
         }
      }
   
  @@ -177,7 +180,8 @@
         return buf.toString();
      }
   
  -   public boolean isReadLocked() {
  +   public boolean isReadLocked()
  +   {
         return !readOwnerList_.isEmpty();
      }
   }
  
  
  



More information about the jboss-cvs-commits mailing list