org.jboss.cache.lock
Class StripedLock

java.lang.Object
  extended by org.jboss.cache.lock.StripedLock

@ThreadSafe
public class StripedLock
extends Object

A simple implementation of lock striping, using Fqns as the keys to lock on, primarily used to help make CacheLoader implemtations thread safe.

Backed by a set of ReentrantReadWriteLock instances, and using the Fqn hashcodes to determine buckets.

Since buckets are used, it doesn't matter that the Fqn in question is not removed from the lock map when no longer in use, since the Fqn is not referenced in this class. Rather, the hash code is used.

Since:
2.0.0
Author:
Manik Surtani

Constructor Summary
StripedLock()
          This constructor just calls StripedLock(int) with a default concurrency value of 20.
StripedLock(int concurrency)
          Creates a new StripedLock which uses a certain number of shared locks across all elements that need to be locked.
 
Method Summary
 void acquireAllLocks(List<Fqn> fqns, boolean exclusive)
          Acquires locks on all fqns passed in.
 void acquireLock(Fqn fqn, boolean exclusive)
          Blocks until a lock is acquired.
 void releaseAllLocks(List<Fqn> fqns)
          Releases locks on all fqns passed in.
 void releaseLock(Fqn fqn)
          Releases a lock the caller may be holding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StripedLock

public StripedLock()
This constructor just calls StripedLock(int) with a default concurrency value of 20.


StripedLock

public StripedLock(int concurrency)
Creates a new StripedLock which uses a certain number of shared locks across all elements that need to be locked.

Parameters:
concurrency - number of threads expected to use this class concurrently.
Method Detail

acquireLock

public void acquireLock(Fqn fqn,
                        boolean exclusive)
Blocks until a lock is acquired.

Parameters:
fqn - the Fqn to lock on
exclusive - if true, a write (exclusive) lock is attempted, otherwise a read (shared) lock is used.

releaseLock

public void releaseLock(Fqn fqn)
Releases a lock the caller may be holding. This method is idempotent.

Parameters:
fqn - the Fqn to release

releaseAllLocks

public void releaseAllLocks(List<Fqn> fqns)
Releases locks on all fqns passed in. Makes multiple calls to releaseLock(org.jboss.cache.Fqn). This method is idempotent.

Parameters:
fqns - list of fqns
See Also:
releaseLock(org.jboss.cache.Fqn)

acquireAllLocks

public void acquireAllLocks(List<Fqn> fqns,
                            boolean exclusive)
Acquires locks on all fqns passed in. Makes multiple calls to acquireLock(org.jboss.cache.Fqn,boolean)

Parameters:
fqns - list of fqns
exclusive - whether locks are exclusive.
See Also:
acquireLock(org.jboss.cache.Fqn,boolean)


Copyright © 2008 JBoss, a division of Red Hat. All Rights Reserved.