com.sun.sgs.impl.util.lock
Class MultiLockManager<K>

java.lang.Object
  extended by com.sun.sgs.impl.util.lock.LockManager<K>
      extended by com.sun.sgs.impl.util.lock.MultiLockManager<K>
Type Parameters:
K - the type of key

public final class MultiLockManager<K>
extends LockManager<K>

A class for managing lock conflicts where locks are not held by transactions and the locker can make simultaneous requests from multiple threads. This class does not detect deadlocks, but provides support for downgrading locks. All Locker objects supplied to this class should be instances of MultiLocker.

This class and its superclass use the Logger named com.sun.sgs.impl.util.lock to log information at the following logging levels:


Field Summary
 
Fields inherited from class com.sun.sgs.impl.util.lock.LockManager
currentKeySync, currentLockerSync, logger
 
Constructor Summary
MultiLockManager(long lockTimeout, int numKeyMaps)
          Creates an instance of this class.
 
Method Summary
 void downgradeLock(Locker<K> locker, K key)
          Downgrades a lock held by a locker from write to read access.
(package private)  LockAttemptResult<K> getWaitingFor()
          Checks if this thread is waiting for a lock.
 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)  void setWaitingFor(LockAttemptResult<K> waitingFor)
          Records the lock that this thread is waiting for, or marks that it is not waiting if the argument is null.
 LockConflict<K> waitForLock(Locker<K> locker)
          Waits for a previous attempt to obtain a lock that blocked.
 
Methods inherited from class com.sun.sgs.impl.util.lock.LockManager
getKeyMap, getLock, getOwners, getWaiters, lockNoWaitInternal, releaseLock, releaseLockInternal, waitForLockInternal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiLockManager

public MultiLockManager(long lockTimeout,
                        int numKeyMaps)
Creates an instance of this class.

Parameters:
lockTimeout - the maximum number of milliseconds to acquire a lock
numKeyMaps - the number of separate maps to use for storing keys
Throws:
IllegalArgumentException - if lockTimeout or numKeyMaps is less than 1
Method Detail

lock

public LockConflict<K> lock(Locker<K> locker,
                            K key,
                            boolean forWrite)
Attempts to acquire a lock, waiting if needed. Returns information about conflicts that occurred while attempting to acquire the lock that prevented the lock from being acquired, or else 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.

Overrides:
lock in class LockManager<K>
Parameters:
locker - the locker requesting the lock
key - the key identifying the lock
forWrite - whether to request a write lock
Returns:
lock conflict information, or null if there was no conflict
Throws:
IllegalArgumentException - if locker has a different lock manager, or if locker is not an instance of MultiLocker

lockNoWait

public LockConflict<K> lockNoWait(Locker<K> locker,
                                  K key,
                                  boolean forWrite)
Attempts to acquire a lock, returning immediately. Returns information about any conflict that occurred while attempting to acquire the lock, or else 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.

Overrides:
lockNoWait in class LockManager<K>
Parameters:
locker - the locker requesting the lock
key - the key identifying the lock
forWrite - whether to request a write lock
Returns:
lock conflict information, or null if there was no conflict
Throws:
IllegalArgumentException - if locker has a different lock manager, or if locker is not an instance of MultiLocker

waitForLock

public LockConflict<K> waitForLock(Locker<K> locker)
Waits for a previous attempt to obtain a lock that blocked. Returns information about any conflict that occurred while attempting to acquire the lock, or else 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.

Overrides:
waitForLock in class LockManager<K>
Parameters:
locker - the locker requesting the lock
Returns:
lock conflict information, or null if there was no conflict
Throws:
IllegalArgumentException - if locker has a different lock manager, or if locker is not an instance of MultiLocker

downgradeLock

public void downgradeLock(Locker<K> locker,
                          K key)
Downgrades a lock held by a locker from write to read access. This method does nothing if the lock is not held for write.

Parameters:
locker - the locker holding the lock
key - the key identifying the lock
Throws:
IllegalArgumentException - if locker has a different lock manager, or if locker is not an instance of MultiLocker

getWaitingFor

LockAttemptResult<K> getWaitingFor()
Checks if this thread is waiting for a lock.

Returns:
the result of the lock request this thread is waiting for or null if it is not waiting

setWaitingFor

void setWaitingFor(LockAttemptResult<K> waitingFor)
Records the lock that this thread is waiting for, or marks that it is not waiting if the argument is null.

Parameters:
waitingFor - the lock or null

RedDwarf, Version 0.10.1
2010-03-14 10:56:12

Copyright © 2010 The RedDwarf Authors. All rights reserved
Copyright © 2007-2010 Sun Microsystems, Inc. All rights reserved