|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sun.sgs.impl.util.lock.LockManager<K>
K - the type of keypublic class LockManager<K>
A class for managing lock conflicts.
This class uses the Logger named com.sun.sgs.impl.util.lock
to log information at the following logging levels:
FINER - Releasing locks; requesting, waiting for,
and returning from lock requests
FINEST - Notifying new lock owners, results of
requesting locks before waiting, releasing locks, results of attempting
to assign locks to waiters
The implementation of this class uses the following thread synchronization scheme to avoid internal deadlocks:
Locker objects and on the Maps that hold Lock objects
Lock class is not synchronized
Callers of non-Object methods on the Lock class should
make sure that they are synchronized on the associated key map.
Locker class and its subclasses only use synchronization for
getter and setter methods
Lock should not synchronize on anything
else The implementation enforces this requirement by having lock methods not make calls to other classes, and by performing minimal work while synchronized on the associated key map.
Locker should not synchronize on a
different locker, but can synchronize on a Lock
In fact, only one method synchronizes on a Locker and on a
Lock -- the waitForLockInternal
method. That method also makes sure that the only synchronized Locker methods that it calls are on the locker it has already
synchronized on.
| Field Summary | |
|---|---|
(package private) ThreadLocal<K> |
currentKeySync
When assertions are enabled, hold the Key whose associated
Map the current thread is synchronized on, if any. |
(package private) ThreadLocal<Locker<K>> |
currentLockerSync
When assertions are enabled, holds the Locker that the
current thread is synchronized on, if any. |
(package private) static LoggerWrapper |
logger
The logger for this class. |
| Constructor Summary | |
|---|---|
LockManager(long lockTimeout,
int numKeyMaps)
Creates an instance of this class. |
|
| Method Summary | |
|---|---|
(package private) Map<K,Lock<K>> |
getKeyMap(K key)
Returns the key map to use for the specified key. |
(package private) Lock<K> |
getLock(K key,
Map<K,Lock<K>> keyMap)
Returns the lock associated with the specified key from the key map, which should be the one returned by calling getKeyMap. |
List<LockRequest<K>> |
getOwners(K key)
Returns a possibly read-only list that contains a snapshot of the current owners of a lock, as identified by lock requests. |
List<LockRequest<K>> |
getWaiters(K key)
Returns a possibly read-only list that contains a snapshot of the current waiters for a lock, as identified by lock requests. |
LockConflict<K> |
lock(Locker<K> locker,
K key,
boolean forWrite)
Attempts to acquire a lock, waiting if needed. |
LockConflict<K> |
lockNoWait(Locker<K> locker,
K key,
boolean forWrite)
Attempts to acquire a lock, returning immediately. |
(package private) LockConflict<K> |
lockNoWaitInternal(Locker<K> locker,
K key,
boolean forWrite)
Attempts to acquire a lock, returning immediately. |
void |
releaseLock(Locker<K> locker,
K key)
Releases a lock held by a locker. |
(package private) void |
releaseLockInternal(Locker<K> locker,
K key,
boolean downgrade)
Releases a lock, but only downgrading it if downgrade is true. |
LockConflict<K> |
waitForLock(Locker<K> locker)
Waits for a previous attempt to obtain a lock that blocked. |
(package private) LockConflict<K> |
waitForLockInternal(Locker<K> locker)
Waits for a previous attempt to obtain a lock that blocked. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
static final LoggerWrapper logger
final ThreadLocal<Locker<K>> currentLockerSync
Locker that the
current thread is synchronized on, if any.
final ThreadLocal<K> currentKeySync
Key whose associated
Map the current thread is synchronized on, if any.
| Constructor Detail |
|---|
public LockManager(long lockTimeout,
int numKeyMaps)
lockTimeout - the maximum number of milliseconds to acquire a
locknumKeyMaps - the number of separate maps to use for storing keys
IllegalArgumentException - if lockTimeout or numKeyMaps is less than 1| Method Detail |
|---|
public LockConflict<K> lock(Locker<K> locker,
K key,
boolean forWrite)
null if the lock
was acquired. If the type field of the return value is DEADLOCK, then the caller should abort the
transaction, and any subsequent lock or wait requests will throw IllegalStateException. Otherwise, the caller can repeat this call, and
any conflicts from earlier calls will be ignored.
locker - the locker requesting the lockkey - the key identifying the lockforWrite - whether to request a write lock
null if there was no
conflict
IllegalArgumentException - if locker has a different lock
manager
IllegalStateException - if an earlier lock attempt for this
transaction produced a deadlock, or if still waiting for an
earlier attempt to complete
public LockConflict<K> lockNoWait(Locker<K> locker,
K key,
boolean forWrite)
null if the lock was acquired. If the attempt to
acquire the lock was blocked, returns a value with a type field
of BLOCKED rather than waiting. If the
type field of the return value is DEADLOCK, then the caller should abort the
transaction, and any subsequent lock or wait requests will throw IllegalStateException. Otherwise, the caller can repeat this call, and
any conflicts from earlier calls will be ignored.
locker - the locker requesting the lockkey - the key identifying the lockforWrite - whether to request a write lock
null if there was no
conflict
IllegalArgumentException - if locker has a different lock
manager
IllegalStateException - if an earlier lock attempt for this
transaction produced a deadlock, or if still waiting for an
earlier attempt to completepublic LockConflict<K> waitForLock(Locker<K> locker)
null if the lock was acquired or the
transaction was not waiting. If the type field of the return
value is DEADLOCK, then the caller
should abort the transaction, and any subsequent lock or wait requests
will throw IllegalStateException.
locker - the locker requesting the lock
null if there was no
conflict
IllegalArgumentException - if locker has a different lock
manager
public void releaseLock(Locker<K> locker,
K key)
locker - the locker holding the lockkey - the key identifying the lock
IllegalArgumentException - if locker has a different lock
managerpublic List<LockRequest<K>> getOwners(K key)
key - the key identifying the lock
public List<LockRequest<K>> getWaiters(K key)
key - the key identifying the lock
Map<K,Lock<K>> getKeyMap(K key)
key - the key
Lock<K> getLock(K key,
Map<K,Lock<K>> keyMap)
getKeyMap. The lock on keyMap should be held.
key - the keykeyMap - the keyMap
LockConflict<K> lockNoWaitInternal(Locker<K> locker,
K key,
boolean forWrite)
lockNoWait(com.sun.sgs.impl.util.lock.Locker, K, boolean) , but does not check that the correct lock manager was
supplied.
locker - the locker requesting the lockkey - the key identifying the lockforWrite - whether to request a write lock
null if there was no
conflict
IllegalStateException - if an earlier lock attempt for this
transaction produced a deadlock, or if still waiting for an
earlier attempt to completeLockConflict<K> waitForLockInternal(Locker<K> locker)
waitForLock(com.sun.sgs.impl.util.lock.Locker) , but does not check that the correct lock manager was
supplied.
locker - the locker requesting the lock
null if there was no
conflict
void releaseLockInternal(Locker<K> locker,
K key,
boolean downgrade)
downgrade is true.
Like releaseLock(com.sun.sgs.impl.util.lock.Locker, K) , but permits specifying if the lock is being
downgraded rather than fully released.
locker - the locker holding the lockkey - the key identifying the lockdowngrade - whether the lock should only be downgraded
IllegalArgumentException - if locker has a different lock
manager
|
RedDwarf, Version 0.10.1 2010-03-14 10:56:12 |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||