Skip to content

Commit

Permalink
ISPN-993 Help to prevent deadlocks by making the lock id accessible t…
Browse files Browse the repository at this point in the history
…hrough the public API
  • Loading branch information
maniksurtani committed Apr 25, 2011
1 parent 3208ab1 commit e98e524
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
Expand Up @@ -116,4 +116,18 @@ public interface LockManager {
* @return an integer
*/
int getNumberOfLocksHeld();

/**
* Returns the 'id' of the lock that will be used to guard access to a given key in the cache. Particularly useful
* if Lock Striping is used and locks may guard more than one key. This mechanism can be used to check whether
* keys may end up sharing the same lock.
* <p />
* If lock-striping is not used, the identity hash code of the lock created for this specific key is returned. While
* this may not be of much value, it is done to maintain API compatibility of this method regardless of underlying
* locking scheme.
*
* @param key key to test for
* @return the ID of the lock.
*/
int getLockId(Object key);
}
Expand Up @@ -47,9 +47,10 @@
import javax.transaction.TransactionManager;
import java.util.Iterator;
import java.util.Map;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.concurrent.locks.Lock;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

/**
* Handles locks for the MVCC based LockingInterceptor
*
Expand Down Expand Up @@ -203,4 +204,8 @@ public int getNumberOfLocksHeld() {
public int getNumberOfLocksAvailable() {
return lockContainer.size() - lockContainer.getNumLocksHeld();
}

public int getLockId(Object key) {
return lockContainer.getLockId(key);
}
}
Expand Up @@ -97,4 +97,8 @@ public void releaseLock(Object key) {
Lock l = locks.remove(key);
if (l != null) l.unlock();
}

public int getLockId(Object key) {
return System.identityHashCode(getLock(key));
}
}
Expand Up @@ -23,13 +23,12 @@
package org.infinispan.util.concurrent.locks.containers;

import net.jcip.annotations.ThreadSafe;
import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

import org.infinispan.util.logging.Log;
import org.infinispan.util.logging.LogFactory;

import static org.infinispan.util.Util.safeRelease;

/**
Expand Down Expand Up @@ -110,4 +109,8 @@ public void releaseLock(Object key) {
log.debug("Attempted to unlock a lock we didn't own - swallowing an IllegalMonitorStateException");
}
}

public int getLockId(Object key) {
return hashToIndex(key);
}
}
Expand Up @@ -81,4 +81,18 @@ public interface LockContainer {
* @param key Object on which lock is to be removed
*/
void releaseLock(Object key);

/**
* Returns the 'id' of the lock that will be used to guard access to a given key in the cache. Particularly useful
* if Lock Striping is used and locks may guard more than one key. This mechanism can be used to check whether
* keys may end up sharing the same lock.
* <p />
* If lock-striping is not used, the identity hash code of the lock created for this specific key is returned. While
* this may not be of much value, it is done to maintain API compatibility of this method regardless of underlying
* locking scheme.
*
* @param key key to test for
* @return the ID of the lock.
*/
int getLockId(Object key);
}

0 comments on commit e98e524

Please sign in to comment.