Author: mircea.markus
Date: 2008-03-04 15:36:26 -0500 (Tue, 04 Mar 2008)
New Revision: 5384
Modified:
core/trunk/src/main/java/org/jboss/cache/lock/LockMap.java
Log:
- rolled back to ConcurrentHashSet as benchmark showed that this brings best performance.
Also added LockMapPerformanceTest perf test for further testing.
- made the readers collection injectable to facilitate testing
Modified: core/trunk/src/main/java/org/jboss/cache/lock/LockMap.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/lock/LockMap.java 2008-03-04 11:43:07 UTC
(rev 5383)
+++ core/trunk/src/main/java/org/jboss/cache/lock/LockMap.java 2008-03-04 20:36:26 UTC
(rev 5384)
@@ -6,6 +6,8 @@
*/
package org.jboss.cache.lock;
+import org.jboss.cache.util.concurrent.ConcurrentHashSet;
+
import java.util.*;
/**
@@ -22,11 +24,24 @@
private Object writeOwner_ = null;
+
+
// This is more efficient (lower CPU utilisation and better concurrency) than a
CopyOnWriteArraySet or ConcurrentHashSet.
// for some reason this barfs with concurrent mod exceptions. Need to see why.
- private final List<Object> readOwnerList_ = Collections.synchronizedList(new
LinkedList<Object>());
-// private final Set<Object> readOwnerList_ = new
ConcurrentHashSet<Object>();
+ private final Collection<Object> readOwners;
+// private final Set<Object> readOwners = new ConcurrentHashSet<Object>();
+
+ public LockMap()
+ {
+ this(new ConcurrentHashSet<Object>());
+ }
+
+ public LockMap(Collection<Object> readOwners)
+ {
+ this.readOwners = readOwners;
+ }
+
/**
* Check whether this owner has reader or writer ownership.
*
@@ -45,9 +60,9 @@
switch (ownership)
{
case OWNER_ANY:
- return ((writeOwner_ != null && caller.equals(writeOwner_)) ||
readOwnerList_.contains(caller));
+ return ((writeOwner_ != null && caller.equals(writeOwner_)) ||
readOwners.contains(caller));
case OWNER_READ:
- return (readOwnerList_.contains(caller));
+ return (readOwners.contains(caller));
case OWNER_WRITE:
return (writeOwner_ != null && caller.equals(writeOwner_));
default:
@@ -63,7 +78,7 @@
*/
public void addReader(Object owner)
{
- readOwnerList_.add(owner);
+ readOwners.add(owner);
}
/**
@@ -101,7 +116,7 @@
*/
public boolean upgrade(Object owner) throws OwnerNotExistedException
{
- boolean old_value = readOwnerList_.remove(owner);
+ boolean old_value = readOwners.remove(owner);
if (!old_value) // didn't exist in the list
throw new OwnerNotExistedException("Can't upgrade lock. Read lock owner
did not exist");
setWriter(owner);
@@ -113,21 +128,12 @@
*/
public Collection<Object> readerOwners()
{
- //make a defense copy, otherwise ConcurrentModificationException might appear while
client code is iterating
- // the original map (see
IdentityLockTest.testConcurrentModificationOfReadLocksAndToString)
- List readOwnersListCopy;
- synchronized (readOwnerList_) //readOwnerList_ is a sync list, make sure noone
modifies it while we make a copy of it.
- {
- readOwnersListCopy = new ArrayList(readOwnerList_);
- readOwnersListCopy.addAll(readOwnerList_);
- }
- return Collections.unmodifiableList(readOwnersListCopy);
-// return readOwnerList_;
+ return Collections.unmodifiableCollection(readOwners);
}
public void releaseReaderOwners(LockStrategy lock)
{
- int size = readOwnerList_.size();
+ int size = readOwners.size();
for (int i = 0; i < size; i++)
lock.readLock().unlock();
}
@@ -145,7 +151,7 @@
*/
public void removeReader(Object owner)
{
- readOwnerList_.remove(owner);
+ readOwners.remove(owner);
}
/**
@@ -165,7 +171,7 @@
public void removeAll()
{
removeWriter();
- readOwnerList_.clear();
+ readOwners.clear();
}
/**
@@ -176,13 +182,13 @@
public String printInfo()
{
StringBuffer buf = new StringBuffer(64);
- buf.append("Read lock owners:
").append(readOwnerList_).append('\n');
+ buf.append("Read lock owners:
").append(readOwners).append('\n');
buf.append("Write lock owner:
").append(writeOwner_).append('\n');
return buf.toString();
}
public boolean isReadLocked()
{
- return !readOwnerList_.isEmpty();
+ return !readOwners.isEmpty();
}
}
Show replies by date