com.bigdata.concurrent
Class ResourceQueue<R,T>

java.lang.Object
  extended by com.bigdata.concurrent.ResourceQueue<R,T>

public class ResourceQueue<R,T>
extends Object

Unbounded queue of operations waiting to gain an exclusive lock on a resource. By default, the queue imposes a "fair" schedule for access to the resource. Deadlocks among resources are detected using a WAITS_FOR graph that is shared by all resources and transactions for a given database instance.

Note: deadlock detection MAY be disabled when all lock requests are (a) pre-declared; and (b) sorted. When disabled the WAITS_FOR graph is NOT maintained.

Version:
$Id: ResourceQueue.java 2265 2009-10-26 12:51:06Z thompsonbry $
Author:
Bryan Thompson
See Also:
LockManager, TxDag

Field Summary
protected static boolean DEBUG
           
protected static boolean INFO
           
protected static org.apache.log4j.Logger log
           
 
Constructor Summary
ResourceQueue(R resource, TxDag waitsFor)
          Create a queue of lock requests for a resource.
 
Method Summary
 void clear(T tx)
          Causes pending lock requests to abort (the threads that are blocked will throw an InterruptedException) and releases the lock held by the caller.
 int getQueueSize()
          The #of pending requests for a lock on the resource.
 R getResource()
          The resource whose locks are administeded by this object.
 boolean isGranted(T tx)
          Return true if the transaction currently holds the lock.
 boolean isLocked()
          True iff a lock is granted.
 void lock(T tx)
          Obtain a lock on the resource.
 void lock(T tx, long timeout)
          Obtain a lock on the resource.
 String toString()
          Note: This uses LinkedBlockingQueue.toString() to serialize the state of the resource queue so the result will be consistent per the contract of that method and LinkedBlockingQueue.iterator().
 void unlock(T tx)
          Release the lock held by the tx on the resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

protected static final org.apache.log4j.Logger log

INFO

protected static final boolean INFO

DEBUG

protected static final boolean DEBUG
Constructor Detail

ResourceQueue

public ResourceQueue(R resource,
                     TxDag waitsFor)
Create a queue of lock requests for a resource.

Parameters:
resource - The resource.
waitsFor - The WAITS_FOR graph shared by all transactions and all resources (optional). When NOT specified operations MUST pre-declare their locks and the LockManager MUST sort the resources in each lock request into a common order such that deadlocks CAN NOT occur.
Method Detail

getResource

public R getResource()
The resource whose locks are administeded by this object.


isLocked

public boolean isLocked()
True iff a lock is granted.

TODO:
method signature may conflict with implicit use of the Thread as the transaction object.

getQueueSize

public int getQueueSize()
The #of pending requests for a lock on the resource.

Returns:

isGranted

public boolean isGranted(T tx)
Return true if the transaction currently holds the lock.

Parameters:
tx - The transaction.

toString

public String toString()
Note: This uses LinkedBlockingQueue.toString() to serialize the state of the resource queue so the result will be consistent per the contract of that method and LinkedBlockingQueue.iterator().

Overrides:
toString in class Object

lock

public void lock(T tx)
          throws InterruptedException,
                 DeadlockException
Obtain a lock on the resource.

Parameters:
tx - The transaction.
Throws:
InterruptedException - if the lock was interrupted (the transaction should handle this exception by aborting).
DeadlockException - if the request would cause a deadlock among the running transactions.

lock

public void lock(T tx,
                 long timeout)
          throws InterruptedException,
                 DeadlockException
Obtain a lock on the resource.

Parameters:
tx - The transaction.
timeout - The timeout (ms) -or- 0L to wait forever.
Throws:
InterruptedException - if the lock was interrupted (the transaction should handle this exception by aborting).
DeadlockException - if the request would cause a deadlock among the running transactions.

unlock

public void unlock(T tx)
Release the lock held by the tx on the resource.

Parameters:
tx - The transaction.
Throws:
IllegalStateException - if the transaction does not hold the lock.

clear

public void clear(T tx)
Causes pending lock requests to abort (the threads that are blocked will throw an InterruptedException) and releases the lock held by the caller.

Parameters:
tx - The transaction.
Throws:
IllegalStateException - if the transaction does not hold the lock.


Copyright © 2006-2011 SYSTAP, LLC. All Rights Reserved.