Skip to content

Commit

Permalink
IGNITE-59 Added gate to CacheLockImpl.
Browse files Browse the repository at this point in the history
  • Loading branch information
sevdokimov-gg committed Jan 27, 2015
1 parent 3647e88 commit 6badd2b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
Expand Up @@ -30,9 +30,15 @@
/** /**
* *
*/ */
class CacheLockImpl<K> implements Lock { class CacheLockImpl<K, V> implements Lock {
/** Gateway. */
private final GridCacheGateway<K, V> gate;

/** */ /** */
private final GridCacheProjectionEx<K, ?> delegate; private final GridCacheProjectionEx<K, V> delegate;

/** Projection. */
private final GridCacheProjectionImpl<K, V> prj;


/** */ /** */
private final Collection<? extends K> keys; private final Collection<? extends K> keys;
Expand All @@ -44,17 +50,22 @@ class CacheLockImpl<K> implements Lock {
private volatile Thread lockedThread; private volatile Thread lockedThread;


/** /**
* @param gate Gate.
* @param delegate Delegate. * @param delegate Delegate.
* @param prj Projection.
* @param keys Keys. * @param keys Keys.
*/ */
CacheLockImpl(GridCacheProjectionEx<K, ?> delegate, Collection<? extends K> keys) { CacheLockImpl(GridCacheGateway<K, V> gate, GridCacheProjectionEx<K, V> delegate, GridCacheProjectionImpl<K, V> prj,
Collection<? extends K> keys) {
this.gate = gate;
this.delegate = delegate; this.delegate = delegate;
this.prj = prj;
this.keys = keys; this.keys = keys;
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public void lock() { @Override public void lock() {
//cctx.readlock(); GridCacheProjectionImpl<K, V> prev = gate.enter(prj);


try { try {
delegate.lockAll(keys, 0); delegate.lockAll(keys, 0);
Expand All @@ -65,7 +76,7 @@ class CacheLockImpl<K> implements Lock {
throw new CacheException(e.getMessage(), e); throw new CacheException(e.getMessage(), e);
} }
finally { finally {
//cctx.readunlock(); gate.leave(prev);
} }
} }


Expand All @@ -87,6 +98,8 @@ private void incrementLockCounter() {


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public boolean tryLock() { @Override public boolean tryLock() {
GridCacheProjectionImpl<K, V> prev = gate.enter(prj);

try { try {
boolean res = delegate.lockAll(keys, -1); boolean res = delegate.lockAll(keys, -1);


Expand All @@ -98,6 +111,9 @@ private void incrementLockCounter() {
catch (IgniteCheckedException e) { catch (IgniteCheckedException e) {
throw new CacheException(e.getMessage(), e); throw new CacheException(e.getMessage(), e);
} }
finally {
gate.leave(prev);
}
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand All @@ -108,6 +124,8 @@ private void incrementLockCounter() {
if (time <= 0) if (time <= 0)
return tryLock(); return tryLock();


GridCacheProjectionImpl<K, V> prev = gate.enter(prj);

try { try {
IgniteFuture<Boolean> fut = delegate.lockAllAsync(keys, unit.toMillis(time)); IgniteFuture<Boolean> fut = delegate.lockAllAsync(keys, unit.toMillis(time));


Expand Down Expand Up @@ -142,10 +160,15 @@ private void incrementLockCounter() {
catch (IgniteCheckedException e) { catch (IgniteCheckedException e) {
throw new CacheException(e.getMessage(), e); throw new CacheException(e.getMessage(), e);
} }
finally {
gate.leave(prev);
}
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public void unlock() { @Override public void unlock() {
GridCacheProjectionImpl<K, V> prev = gate.enter(prj);

try { try {
if (lockedThread != Thread.currentThread()) { if (lockedThread != Thread.currentThread()) {
throw new IllegalStateException("Failed to unlock keys (did current thread acquire lock " + throw new IllegalStateException("Failed to unlock keys (did current thread acquire lock " +
Expand All @@ -164,6 +187,9 @@ private void incrementLockCounter() {
catch (IgniteCheckedException e) { catch (IgniteCheckedException e) {
throw new CacheException(e.getMessage(), e); throw new CacheException(e.getMessage(), e);
} }
finally {
gate.leave(prev);
}
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand Down
Expand Up @@ -189,7 +189,7 @@ public GridCacheContext<K, V> context() {


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public Lock lockAll(final Collection<? extends K> keys) { @Override public Lock lockAll(final Collection<? extends K> keys) {
return new CacheLockImpl<>(delegate, keys); return new CacheLockImpl<>(gate, delegate, prj, keys);
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand Down

0 comments on commit 6badd2b

Please sign in to comment.