org.exist.storage.lock
Class DeadlockDetection

java.lang.Object
  extended by org.exist.storage.lock.DeadlockDetection

public class DeadlockDetection
extends Object

Deadlock detection for resource and collection locks. The static methods in this class keep track of all waiting threads, which are currently waiting on a resource or collection lock. In some scenarios (e.g. a complex XQuery which modifies resources), a single thread may acquire different read/write locks on resources in a collection. The locks can be arbitrarily nested. For example, a thread may first acquire a read lock on a collection, then a read lock on a resource and later acquires a write lock on the collection to remove the resource. Since we have locks on both, collections and resources, deadlock situations are sometimes unavoidable. For example, imagine the following scenario:

The code should probably be redesigned to avoid this kind of crossed collection-resource locking, which easily leads to circular wait conditions. However, this needs to be done with care. In the meantime, DeadlockDetection is used to detect deadlock situations as the one described above. The lock classes can then try to resolve the deadlock by suspending one thread.


Constructor Summary
DeadlockDetection()
           
 
Method Summary
static void addCollectionWaiter(Thread waiter, Lock lock)
          Register a thread as waiting for a resource lock.
static void addResourceWaiter(Thread thread, WaitingThread waiter)
          Register a thread as waiting for a resource lock.
static Lock clearCollectionWaiter(Thread waiter)
           
static Lock clearResourceWaiter(Thread thread)
          Deregister a waiting thread.
static WaitingThread deadlockCheckResource(Thread threadA, Thread threadB)
          Check if there's a risk for a circular wait between threadA and threadB.
static void debug()
           
static void debug(PrintWriter writer, String name, LockInfo info)
           
static void debug(String name, LockInfo info)
           
static WaitingThread getResourceWaiter(Thread thread)
           
static Map getWaitingThreads()
           
static boolean isBlockedBy(Thread threadA, Thread threadB)
          Check if the second thread is currently waiting for a resource lock and is blocked by the first thread.
static Lock isWaitingFor(Thread waiter)
           
static boolean wouldDeadlock(Thread waiter, Thread owner, List waiters)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DeadlockDetection

public DeadlockDetection()
Method Detail

addResourceWaiter

public static void addResourceWaiter(Thread thread,
                                     WaitingThread waiter)
Register a thread as waiting for a resource lock.

Parameters:
thread - the thread
waiter - the WaitingThread object which wraps around the thread

clearResourceWaiter

public static Lock clearResourceWaiter(Thread thread)
Deregister a waiting thread.

Parameters:
thread -
Returns:
lock

getResourceWaiter

public static WaitingThread getResourceWaiter(Thread thread)

deadlockCheckResource

public static WaitingThread deadlockCheckResource(Thread threadA,
                                                  Thread threadB)
Check if there's a risk for a circular wait between threadA and threadB. The method tests if threadB is currently waiting for a resource lock (read or write). It then checks if threadA holds a lock on this resource. If yes, the WaitingThread object for threadB is returned. This object can be used to suspend the waiting thread in order to temporarily yield the lock to threadA.

Parameters:
threadA -
threadB -
Returns:
waiting thread

isBlockedBy

public static boolean isBlockedBy(Thread threadA,
                                  Thread threadB)
Check if the second thread is currently waiting for a resource lock and is blocked by the first thread.

Parameters:
threadA - the thread whose lock might be blocking threadB
threadB - the thread to check
Returns:
true if threadB is currently blocked by a lock held by threadA

wouldDeadlock

public static boolean wouldDeadlock(Thread waiter,
                                    Thread owner,
                                    List waiters)

addCollectionWaiter

public static void addCollectionWaiter(Thread waiter,
                                       Lock lock)
Register a thread as waiting for a resource lock.

Parameters:
waiter - the thread
lock - the lock object

clearCollectionWaiter

public static Lock clearCollectionWaiter(Thread waiter)

isWaitingFor

public static Lock isWaitingFor(Thread waiter)

getWaitingThreads

public static Map getWaitingThreads()

debug

public static void debug(String name,
                         LockInfo info)

debug

public static void debug(PrintWriter writer,
                         String name,
                         LockInfo info)

debug

public static void debug()


Copyright (C) Wolfgang Meier. All rights reserved.