[jboss-cvs] JBossAS SVN: r79753 - in projects/ejb3/trunk/testsuite: src/test/java/org/jboss/ejb3/test/singleton and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Oct 20 11:50:07 EDT 2008


Author: alex.loubyansky at jboss.com
Date: 2008-10-20 11:50:06 -0400 (Mon, 20 Oct 2008)
New Revision: 79753

Added:
   projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/
   projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/JUCReadWriteLockFactory.java
   projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/Lock.java
   projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/LockFactory.java
   projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/SimpleReadWriteLockFactory.java
Modified:
   projects/ejb3/trunk/testsuite/build-test.xml
   projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/SingletonLockInterceptor.java
Log:
EJBTHREE-1518 refactoring to be able to easier test alternative lock implementations, added read/write lock based on java.util.concurrency impl

Modified: projects/ejb3/trunk/testsuite/build-test.xml
===================================================================
--- projects/ejb3/trunk/testsuite/build-test.xml	2008-10-20 15:12:37 UTC (rev 79752)
+++ projects/ejb3/trunk/testsuite/build-test.xml	2008-10-20 15:50:06 UTC (rev 79753)
@@ -2726,6 +2726,7 @@
       <jar jarfile="${build.lib}/singleton-test.jar">
          <fileset dir="${build.classes}">
             <include name="org/jboss/ejb3/test/singleton/*.class"/>
+            <include name="org/jboss/ejb3/test/singleton/lock/*.class"/>
          </fileset>
          <fileset dir="${resources}/test/singleton">
             <include name="aspectdomain-ejb3-interceptors-aop.xml"/>

Modified: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/SingletonLockInterceptor.java
===================================================================
--- projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/SingletonLockInterceptor.java	2008-10-20 15:12:37 UTC (rev 79752)
+++ projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/SingletonLockInterceptor.java	2008-10-20 15:50:06 UTC (rev 79753)
@@ -25,6 +25,9 @@
 import org.jboss.aop.joinpoint.MethodInvocation;
 import org.jboss.ejb3.EJBContainer;
 import org.jboss.ejb3.aop.AbstractInterceptor;
+import org.jboss.ejb3.test.singleton.lock.Lock;
+import org.jboss.ejb3.test.singleton.lock.LockFactory;
+import org.jboss.ejb3.test.singleton.lock.SimpleReadWriteLockFactory;
 import org.jboss.logging.Logger;
 
 /**
@@ -38,8 +41,15 @@
    private static final Logger log = Logger.getLogger(SingletonLockInterceptor.class);
 
    // container/instance lock
-   private final Lock lock = new Lock();
+   private final Lock readLock;
+   private final Lock writeLock;
    
+   {
+      LockFactory factory = new  SimpleReadWriteLockFactory(); //JUCReadWriteLockFactory();
+      readLock = factory.createLock(true);
+      writeLock = factory.createLock(false);
+   }
+   
    public String getName()
    {
       return "SingletonLockInterceptor";
@@ -67,143 +77,15 @@
          //   methodName += "(" + mi.getArguments()[0] + ')';
       }
       
-      lock.sync();
+      Lock lock = isReadMethod ? readLock : writeLock;      
+      lock.lock();
       try
       {
-         boolean wait;
-         if(isReadMethod)
-            wait = lock.isWriteInProgress();
-         else
-            wait = lock.hasActiveThreads();
-         
-         while(wait)
-         {
-            //log.info(methodName + " is waiting; active threads=" + lock.activeThreads);
-            synchronized (lock)
-            {
-               lock.releaseSync();
-
-               try
-               {
-                  lock.wait();
-               }
-               catch (InterruptedException e)
-               {
-               }
-            }
-            
-            lock.sync();
-            
-            if(isReadMethod)
-               wait = lock.isWriteInProgress();
-            else
-               wait = lock.hasActiveThreads();
-         }
-
-         //log.info(methodName + " coming through; active threads=" + lock.activeThreads);
-
-         lock.increaseActiveThreads();
-         if(!isReadMethod)
-            lock.setWriteInProgress(true);
-      }
-      finally
-      {
-         lock.releaseSync();
-      }
-      
-      try
-      {
          return invocation.invokeNext();
       }
       finally
       {
-         lock.sync();
-         try
-         {
-            lock.decreaseActiveThreads();
-            if(!isReadMethod)
-               lock.setWriteInProgress(false);
-         }
-         finally
-         {
-            //log.info(methodName + " is done; active threads=" + lock.activeThreads);
-            lock.releaseSync();
-         }
+         lock.unlock();
       }
    }
-   
-   private static class Lock
-   {
-      private int activeThreads;
-
-      private Thread synched;
-      private int synchedDepth;
-   
-      private boolean writeInProgress;
-      
-      public void increaseActiveThreads()
-      {
-         ++activeThreads;
-      }
-      
-      public void decreaseActiveThreads()
-      {
-         --activeThreads;
-      }
-      
-      public boolean hasActiveThreads()
-      {
-         return activeThreads > 0;
-      }
-      
-      public boolean isWriteInProgress()
-      {
-         return writeInProgress;
-      }
-      
-      public void setWriteInProgress(boolean writeInProgress)
-      {
-         this.writeInProgress = writeInProgress;
-      }
-      
-      /**
-       * A method that checks if the calling thread has the lock, and if it
-       * does not blocks until the lock is available. If there is no current owner
-       * of the lock, or the calling thread already owns the lock then the
-       * calling thread will immeadiately acquire the lock.
-       */ 
-      public void sync()
-      {
-         synchronized (this)
-         {
-            Thread thread = Thread.currentThread();
-            while (synched != null && !synched.equals(thread))
-            {
-               try
-               {
-                  this.wait();
-               }
-               catch (InterruptedException ex)
-               { /* ignore */
-               }
-            }
-
-            synched = thread;
-
-            if(synchedDepth > 0)
-               throw new IllegalStateException("At the moment synchedDepth shouldn't be more than 1");
-            ++synchedDepth;
-         }
-      }
-    
-      public void releaseSync()
-      {
-         synchronized(this)
-         {
-            if (--synchedDepth == 0)
-               synched = null;
-            this.notify();
-         }
-      }
-   }
 }

Added: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/JUCReadWriteLockFactory.java
===================================================================
--- projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/JUCReadWriteLockFactory.java	                        (rev 0)
+++ projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/JUCReadWriteLockFactory.java	2008-10-20 15:50:06 UTC (rev 79753)
@@ -0,0 +1,66 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.singleton.lock;
+
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * A JUCReadWriteLockFactory.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class JUCReadWriteLockFactory implements LockFactory
+{
+   private final ReadWriteLock lock;
+   
+   public JUCReadWriteLockFactory()
+   {
+      lock = new ReentrantReadWriteLock(true);
+   }
+   
+   public Lock createLock(boolean read)
+   {
+      return new JUCLockAdaptor(read ? lock.readLock() : lock.writeLock());
+   }
+
+   private static class JUCLockAdaptor implements Lock
+   {
+      private final java.util.concurrent.locks.Lock lock;
+      
+      public JUCLockAdaptor(java.util.concurrent.locks.Lock lock)
+      {
+         this.lock = lock;
+      }
+      
+      public void lock()
+      {
+         lock.lock();
+      }
+
+      public void unlock()
+      {
+         lock.unlock();
+      }
+   }
+}


Property changes on: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/JUCReadWriteLockFactory.java
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/Lock.java
===================================================================
--- projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/Lock.java	                        (rev 0)
+++ projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/Lock.java	2008-10-20 15:50:06 UTC (rev 79753)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.singleton.lock;
+
+/**
+ * A Lock.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public interface Lock
+{
+   void lock();
+   
+   void unlock();
+}


Property changes on: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/Lock.java
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/LockFactory.java
===================================================================
--- projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/LockFactory.java	                        (rev 0)
+++ projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/LockFactory.java	2008-10-20 15:50:06 UTC (rev 79753)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.singleton.lock;
+
+/**
+ * A LockFactory.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public interface LockFactory
+{
+   Lock createLock(boolean read);
+}


Property changes on: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/LockFactory.java
___________________________________________________________________
Name: svn:executable
   + *

Added: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/SimpleReadWriteLockFactory.java
===================================================================
--- projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/SimpleReadWriteLockFactory.java	                        (rev 0)
+++ projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/SimpleReadWriteLockFactory.java	2008-10-20 15:50:06 UTC (rev 79753)
@@ -0,0 +1,185 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.singleton.lock;
+
+
+/**
+ * A SimpleReadWriteLockFactory.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class SimpleReadWriteLockFactory implements LockFactory
+{
+   private final Sync sync = new Sync();
+   
+   public Lock createLock(boolean read)
+   {
+      return new SimpleReadWriteLock(sync, read);
+   }
+   
+   private class SimpleReadWriteLock implements Lock
+   {
+      private final Sync sync;
+      private final boolean read;
+      
+      public SimpleReadWriteLock(Sync sync, boolean read)
+      {
+         this.sync = sync;
+         this.read = read;
+      }
+      
+      public void lock()
+      {
+         try
+         {
+            boolean wait;
+            sync.sync();      
+            if (read)
+               wait = sync.isWriteInProgress();
+            else
+               wait = sync.hasActiveThreads();
+
+            while (wait)
+            {
+               sync.releaseSync();
+
+               //log.info(methodName + " is waiting; active threads=" + lock.activeThreads);
+               synchronized (sync)
+               {
+                  try
+                  {
+                     sync.wait();
+                  }
+                  catch (InterruptedException e)
+                  {
+                  }
+                  
+                  sync.sync();
+                  if (read)
+                     wait = sync.isWriteInProgress();
+                  else
+                     wait = sync.hasActiveThreads();
+               }
+            }
+
+            sync.increaseActiveThreads();
+            if (!read)
+               sync.setWriteInProgress(true);
+         }
+         finally
+         {
+            sync.releaseSync();
+         }
+      }
+
+      public void unlock()
+      {
+         try
+         {
+            sync.sync();
+            sync.decreaseActiveThreads();
+            if (!read)
+               sync.setWriteInProgress(false);
+         }
+         finally
+         {
+            sync.releaseSync();
+         }
+      }
+   }
+   
+   private static class Sync
+   {
+      private int activeThreads;
+
+      private Thread synched;
+      private int synchedDepth;
+
+      private boolean writeInProgress;
+      
+      public void increaseActiveThreads()
+      {
+         ++activeThreads;
+      }
+      
+      public void decreaseActiveThreads()
+      {
+         --activeThreads;
+      }
+      
+      public boolean hasActiveThreads()
+      {
+         return activeThreads > 0;
+      }
+      
+      public boolean isWriteInProgress()
+      {
+         return writeInProgress;
+      }
+      
+      public void setWriteInProgress(boolean writeInProgress)
+      {
+         this.writeInProgress = writeInProgress;
+      }
+      
+      /**
+       * A method that checks if the calling thread has the lock, and if it
+       * does not blocks until the lock is available. If there is no current owner
+       * of the lock, or the calling thread already owns the lock then the
+       * calling thread will immeadiately acquire the lock.
+       */ 
+      public void sync()
+      {
+         synchronized (this)
+         {
+            Thread thread = Thread.currentThread();
+            while (synched != null && !synched.equals(thread))
+            {
+               try
+               {
+                  this.wait();
+               }
+               catch (InterruptedException ex)
+               { /* ignore */
+               }
+            }
+
+            synched = thread;
+
+            if(synchedDepth > 0)
+               throw new IllegalStateException("At the moment synchedDepth shouldn't be more than 1");
+            ++synchedDepth;
+         }
+      }
+    
+      public void releaseSync()
+      {
+         synchronized(this)
+         {
+            if (--synchedDepth == 0)
+               synched = null;
+            this.notify();
+         }
+      }
+   }
+}


Property changes on: projects/ejb3/trunk/testsuite/src/test/java/org/jboss/ejb3/test/singleton/lock/SimpleReadWriteLockFactory.java
___________________________________________________________________
Name: svn:executable
   + *




More information about the jboss-cvs-commits mailing list