Cleaned up locks

This commit is contained in:
nathan 2015-06-03 11:27:30 +02:00
parent e70adb2486
commit d1f23220e5

View File

@ -6,11 +6,6 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
abstract class pad<T> extends item<T> {
volatile long z0, z1, z2, z4, z5, z6 = 7L;
}
/** /**
* This data structure is optimized for non-blocking reads even when write operations occur. * This data structure is optimized for non-blocking reads even when write operations occur.
* Running read iterators will not be affected by add operations since writes always insert at the head of the * Running read iterators will not be affected by add operations since writes always insert at the head of the
@ -45,32 +40,34 @@ public abstract class AbstractConcurrentSet<T> implements Set<T> {
} }
boolean changed; boolean changed;
long stamp = this.lock.readLock(); final StampedLock lock = this.lock;
long stamp = lock.readLock();
if (this.entries.containsKey(element)) { if (this.entries.containsKey(element)) {
this.lock.unlockRead(stamp); lock.unlockRead(stamp);
return false; return false;
} }
long origStamp = stamp; long origStamp = stamp;
if ((stamp = this.lock.tryConvertToWriteLock(stamp)) == 0) { if ((stamp = lock.tryConvertToWriteLock(stamp)) == 0) {
this.lock.unlockRead(origStamp); lock.unlockRead(origStamp);
stamp = this.lock.writeLock(); stamp = lock.writeLock();
} }
changed = insert(element); changed = insert(element);
this.lock.unlock(stamp); lock.unlock(stamp);
return changed; return changed;
} }
@Override @Override
public boolean contains(Object element) { public boolean contains(Object element) {
long stamp = this.lock.readLock(); final StampedLock lock = this.lock;
long stamp = lock.readLock();
ISetEntry<T> entry = this.entries.get(element); ISetEntry<T> entry = this.entries.get(element);
this.lock.unlockRead(stamp); lock.unlockRead(stamp);
return entry != null && entry.getValue() != null; return entry != null && entry.getValue() != null;
} }