Skip to content

Commit

Permalink
Changed the lock to ReentrantReadWriteWriteShareLock
Browse files Browse the repository at this point in the history
  • Loading branch information
suranjan kumar committed Jul 5, 2018
1 parent d469340 commit 6f2602c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ public interface VersionTagEntry {
public long getRegionVersion();
}

public boolean isWriteLockedBySameThread();
//public boolean isWriteLockedBySameThread();

}
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ public TXRegionState(final LocalRegion r, final TXState tx) {
// in that case don't take lock
ImageState imgState = r.getImageState();

boolean alreadyLocked = imgState.isWriteLockedBySameThread();
//boolean alreadyLocked = imgState.isWriteLockedBySameThread();
if (!r.isInitialized()
&& (alreadyLocked || imgState.lockPendingTXRegionStates(true, false))) {
&& (imgState.lockPendingTXRegionStates(true, false))) {
try {
if (!r.getImageState().addPendingTXRegionState(this)) {
this.pendingTXOps = null;
Expand All @@ -167,7 +167,7 @@ public TXRegionState(final LocalRegion r, final TXState tx) {
this.pendingTXLockFlags = new TIntArrayList();
}
} finally {
if (!alreadyLocked)
//if (!alreadyLocked)
r.getImageState().unlockPendingTXRegionStates(true);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
import com.gemstone.gemfire.i18n.LogWriterI18n;
import com.gemstone.gemfire.internal.Assert;
import com.gemstone.gemfire.internal.cache.locks.LockMode;
import com.gemstone.gemfire.internal.cache.locks.ReentrantReadWriteWriteShareLock;
import com.gemstone.gemfire.internal.util.concurrent.StoppableNonReentrantLock;
import com.gemstone.gemfire.internal.util.concurrent.StoppableReentrantReadWriteLock;
import com.gemstone.gemfire.internal.cache.locks.NonReentrantReadWriteLock;
Expand Down Expand Up @@ -75,8 +77,10 @@ public class UnsharedImageState implements ImageState {
* GII is complete
*/
private volatile THashMapWithCreate pendingTXRegionStates;
private final NonReentrantReadWriteLock pendingTXRegionStatesLock;
private volatile Thread pendingTXRegionStatesLockOwner;
private final ReentrantReadWriteWriteShareLock pendingTXRegionStatesLock;


//private volatile Thread pendingTXRegionStatesLockOwner;
private final AtomicInteger pendingTXOrder;
private volatile TObjectIntHashMap finishedTXIdOrders;

Expand All @@ -101,7 +105,7 @@ public class UnsharedImageState implements ImageState {
this.failedEvents = new ConcurrentTHashSet<EventID>(2);
this.pendingTXRegionStates = isLocal ? null : new THashMapWithCreate();
this.pendingTXRegionStatesLock = isLocal ? null
: new NonReentrantReadWriteLock(stopper);
: new ReentrantReadWriteWriteShareLock();
this.pendingTXOrder = new AtomicInteger(0);
}

Expand Down Expand Up @@ -133,11 +137,11 @@ public void setRequestedUnappliedDelta(boolean flag) {
requestedDelta = flag;
}

@Override
/*@Override
public boolean isWriteLockedBySameThread() {
return this.pendingTXRegionStatesLock.isWriteLocked() &&
(this.pendingTXRegionStatesLockOwner == Thread.currentThread());
}
}*/

@Override
public boolean requestedUnappliedDelta() {
Expand Down Expand Up @@ -407,12 +411,13 @@ public TXRegionState getPendingTXRegionState(TXId txId, boolean lock) {
}
TXRegionState txrs = null;
if (lock) {
if (Thread.currentThread() != this.pendingTXRegionStatesLockOwner) {
this.pendingTXRegionStatesLock.attemptReadLock(-1);
}
else {
//if (Thread.currentThread() != this.pendingTXRegionStatesLockOwner) {
this.pendingTXRegionStatesLock.attemptLock(LockMode.SH,-1, this);
//this.pendingTXRegionStatesLock.attemptReadLock(-1);
//}
//else {
lock = false;
}
//}
}
try {
if (this.pendingTXRegionStates != null) {
Expand All @@ -427,7 +432,8 @@ public TXRegionState getPendingTXRegionState(TXId txId, boolean lock) {
}
} finally {
if (lock) {
this.pendingTXRegionStatesLock.releaseReadLock();
this.pendingTXRegionStatesLock.releaseLock(LockMode.SH,false, this);
//this.pendingTXRegionStatesLock.releaseReadLock();
}
}
return txrs;
Expand All @@ -451,11 +457,13 @@ public boolean lockPendingTXRegionStates(final boolean forWrite,
}
}
if (forWrite) {
this.pendingTXRegionStatesLock.attemptWriteLock(-1);
this.pendingTXRegionStatesLockOwner = Thread.currentThread();
this.pendingTXRegionStatesLock.attemptLock(LockMode.EX,-1, this);
//this.pendingTXRegionStatesLock.attemptWriteLock(-1);
//this.pendingTXRegionStatesLockOwner = Thread.currentThread();
}
else {
this.pendingTXRegionStatesLock.attemptReadLock(-1);
this.pendingTXRegionStatesLock.attemptLock(LockMode.SH,-1, this);
//this.pendingTXRegionStatesLock.attemptReadLock(-1);
}
if (this.pendingTXRegionStates != null) {
if (TXStateProxy.LOG_FINE) {
Expand All @@ -470,11 +478,14 @@ public boolean lockPendingTXRegionStates(final boolean forWrite,
}
else {
if (forWrite) {
this.pendingTXRegionStatesLockOwner = null;
this.pendingTXRegionStatesLock.releaseWriteLock();
//this.pendingTXRegionStatesLockOwner = null;
//this.pendingTXRegionStatesLock.releaseWriteLock();
this.pendingTXRegionStatesLock.releaseLock(LockMode.EX, false, this);
}
else {
this.pendingTXRegionStatesLock.releaseReadLock();
this.pendingTXRegionStatesLock.releaseLock(LockMode.SH, false, this);

//this.pendingTXRegionStatesLock.releaseReadLock();
}
return false;
}
Expand All @@ -487,11 +498,15 @@ public boolean lockPendingTXRegionStates(final boolean forWrite,
public void unlockPendingTXRegionStates(final boolean forWrite) {
if (this.pendingTXRegionStatesLock != null) {
if (forWrite) {
this.pendingTXRegionStatesLockOwner = null;
this.pendingTXRegionStatesLock.releaseWriteLock();
//this.pendingTXRegionStatesLockOwner = null;
//this.pendingTXRegionStatesLock.releaseWriteLock();
this.pendingTXRegionStatesLock.releaseLock(LockMode.EX, false, this);

}
else {
this.pendingTXRegionStatesLock.releaseReadLock();
//this.pendingTXRegionStatesLock.releaseReadLock();
this.pendingTXRegionStatesLock.releaseLock(LockMode.SH, false, this);

}
if (TXStateProxy.LOG_FINE) {
final LogWriterI18n logger = InternalDistributedSystem.getLoggerI18n();
Expand Down Expand Up @@ -535,7 +550,8 @@ public void setTXOrderForFinish(TXRegionState txrs) {
if (this.pendingTXRegionStatesLock != null) {
TObjectIntHashMap finishedOrders;
// assume read lock on pendingTXRegionStates is already held
Assert.assertTrue(this.pendingTXRegionStatesLock.numReaders() > 0);
//Assert.assertTrue(this.pendingTXRegionStatesLock.numReaders() > 0);
Assert.assertTrue(this.pendingTXRegionStatesLock.numReadOnlyLocks() > 0);
if ((finishedOrders = this.finishedTXIdOrders) != null) {
int order = finishedOrders.get(txrs.getTXState().getTransactionId());
if (order != 0) {
Expand Down Expand Up @@ -569,7 +585,8 @@ public void mergeFinishedTXOrders(final LocalRegion region,
final Collection<TXId> txIds) {
final THashMapWithCreate pendingTXRS = this.pendingTXRegionStates;
if (pendingTXRS != null) {
this.pendingTXRegionStatesLock.attemptWriteLock(-1);
//this.pendingTXRegionStatesLock.attemptWriteLock(-1);
this.pendingTXRegionStatesLock.attemptLock(LockMode.EX, -1, this);
try {
// first get the ordering for finished transactions from TX manager;
// this is deliberately invoked under the lock to sync against
Expand Down Expand Up @@ -603,7 +620,8 @@ public void mergeFinishedTXOrders(final LocalRegion region,
this.pendingTXOrder.addAndGet(increment);
this.finishedTXIdOrders = txIdOrders;
} finally {
this.pendingTXRegionStatesLock.releaseWriteLock();
//this.pendingTXRegionStatesLock.releaseWriteLock();
this.pendingTXRegionStatesLock.releaseLock(LockMode.EX, false, this);
}
}
}
Expand Down

0 comments on commit 6f2602c

Please sign in to comment.