james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nor...@apache.org
Subject svn commit: r1150446 - in /james/mailbox/trunk: api/src/main/java/org/apache/james/mailbox/ store/src/main/java/org/apache/james/mailbox/store/
Date Sun, 24 Jul 2011 17:51:43 GMT
Author: norman
Date: Sun Jul 24 17:51:42 2011
New Revision: 1150446

URL: http://svn.apache.org/viewvc?rev=1150446&view=rev
Log:
Make MailboxPathLocker aware of read / write lock. This is part of MAILBOX-110

Modified:
    james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxPathLocker.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/AbstractMailboxPathLocker.java
    james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/JVMMailboxPathLocker.java

Modified: james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxPathLocker.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxPathLocker.java?rev=1150446&r1=1150445&r2=1150446&view=diff
==============================================================================
--- james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxPathLocker.java
(original)
+++ james/mailbox/trunk/api/src/main/java/org/apache/james/mailbox/MailboxPathLocker.java
Sun Jul 24 17:51:42 2011
@@ -27,15 +27,24 @@ package org.apache.james.mailbox;
 public interface MailboxPathLocker {
 
     /**
+     * @deprecated use {@link #executeWithLock(MailboxSession, MailboxPath, LockAwareExecution,
boolean)} with argument <code>true</code>
+     */
+    @Deprecated
+    public <T> T executeWithLock(MailboxSession session, MailboxPath path, LockAwareExecution<T>
execution) throws MailboxException;
+
+    
+    /**
      * Execute the {@link LockAwareExecution} while holding a lock on the
-     * {@link MailboxPath}
+     * {@link MailboxPath}. 
      * 
      * @param session
      * @param path
      * @param execution
+     * @param writeLokc
+     * 
      * @throws MailboxException
      */
-    public <T> T executeWithLock(MailboxSession session, MailboxPath path, LockAwareExecution<T>
execution) throws MailboxException;
+    public <T> T executeWithLock(MailboxSession session, MailboxPath path, LockAwareExecution<T>
execution, boolean writeLock) throws MailboxException;
 
     /**
      * Execute code while holding a lock

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/AbstractMailboxPathLocker.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/AbstractMailboxPathLocker.java?rev=1150446&r1=1150445&r2=1150446&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/AbstractMailboxPathLocker.java
(original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/AbstractMailboxPathLocker.java
Sun Jul 24 17:51:42 2011
@@ -31,13 +31,19 @@ public abstract class AbstractMailboxPat
      * @see org.apache.james.mailbox.MailboxPathLocker#executeWithLock(org.apache.james.mailbox.MailboxSession,
org.apache.james.mailbox.MailboxPath, org.apache.james.mailbox.MailboxPathLocker.LockAwareExecution)
      */
     public <T> T executeWithLock(MailboxSession session, MailboxPath path, LockAwareExecution<T>
execution) throws MailboxException {
+        return executeWithLock(session, path, execution, true);
+    }
+    
+    @Override
+    public <T> T executeWithLock(MailboxSession session, MailboxPath path, LockAwareExecution<T>
execution, boolean writeLock) throws MailboxException {
         try {
-            lock(session, path);
+            lock(session, path, writeLock);
             return execution.execute();
         } finally {
-            unlock(session, path);
+            unlock(session, path, writeLock);
         }
     }
+
     
     /**
      * Perform lock
@@ -46,7 +52,7 @@ public abstract class AbstractMailboxPat
      * @param path
      * @throws MailboxException
      */
-    protected abstract void lock(MailboxSession session, MailboxPath path) throws MailboxException;
+    protected abstract void lock(MailboxSession session, MailboxPath path, boolean writeLock)
throws MailboxException;
 
     /**
      * Release lock
@@ -55,6 +61,6 @@ public abstract class AbstractMailboxPat
      * @param path
      * @throws MailboxException
      */
-    protected abstract void unlock(MailboxSession session, MailboxPath path) throws MailboxException;
+    protected abstract void unlock(MailboxSession session, MailboxPath path, boolean writeLock)
throws MailboxException;
 
 }

Modified: james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/JVMMailboxPathLocker.java
URL: http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/JVMMailboxPathLocker.java?rev=1150446&r1=1150445&r2=1150446&view=diff
==============================================================================
--- james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/JVMMailboxPathLocker.java
(original)
+++ james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/JVMMailboxPathLocker.java
Sun Jul 24 17:51:42 2011
@@ -21,7 +21,8 @@ package org.apache.james.mailbox.store;
 
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.apache.james.mailbox.MailboxException;
 import org.apache.james.mailbox.MailboxPath;
@@ -31,40 +32,50 @@ import org.apache.james.mailbox.MailboxS
 /**
  * 
  * {@link MailboxPathLocker} implementation which helps to synchronize the access the 
- * same MailboxPath. This is done using one {@link ReentrantLock}
+ * same MailboxPath. This is done using one {@link ReentrantReadWriteLock}
  * per {@link MailboxPath} so its only usable in a single JVM.
  *
  */
 public final class JVMMailboxPathLocker extends AbstractMailboxPathLocker {
 
-    private final ConcurrentHashMap<MailboxPath, Lock> paths = new ConcurrentHashMap<MailboxPath,
Lock>();
+    private final ConcurrentHashMap<MailboxPath, ReadWriteLock> paths = new ConcurrentHashMap<MailboxPath,
ReadWriteLock>();
 
 
     /*
      * (non-Javadoc)
-     * @see org.apache.james.mailbox.store.AbstractMailboxPathLocker#lock(org.apache.james.mailbox.MailboxSession,
org.apache.james.mailbox.MailboxPath)
+     * @see org.apache.james.mailbox.store.AbstractMailboxPathLocker#lock(org.apache.james.mailbox.MailboxSession,
org.apache.james.mailbox.MailboxPath, boolean)
      */
-    protected void lock(MailboxSession session, MailboxPath path) throws MailboxException
{
-        Lock lock = paths.get(path);
+    protected void lock(MailboxSession session, MailboxPath path, boolean writeLock) throws
MailboxException {
+        ReadWriteLock lock = paths.get(path);
         if (lock == null) {
-            lock = new ReentrantLock();
-            Lock storedLock = paths.putIfAbsent(path, lock);
+            lock = new ReentrantReadWriteLock();
+            ReadWriteLock storedLock = paths.putIfAbsent(path, lock);
             if (storedLock != null) {
                 lock = storedLock;
             }
         }
-        lock.lock();        
+        getLock(lock, writeLock).lock();
     }
 
     /*
      * (non-Javadoc)
      * @see org.apache.james.mailbox.store.AbstractMailboxPathLocker#unlock(org.apache.james.mailbox.MailboxSession,
org.apache.james.mailbox.MailboxPath)
      */
-    protected void unlock(MailboxSession session, MailboxPath path) throws MailboxException
{
-        Lock lock = paths.get(path);
+    protected void unlock(MailboxSession session, MailboxPath path, boolean writeLock) throws
MailboxException {
+        ReadWriteLock lock = paths.get(path);
         
         if (lock != null) {
-            lock.unlock();
+            getLock(lock, writeLock).unlock();
         }        
     }
+    
+    private Lock getLock(ReadWriteLock lock, boolean writeLock) {
+        Lock l;
+        if (writeLock) {
+            l = lock.writeLock();
+        } else {
+            l = lock.readLock();
+        }
+        return l;
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message