General Deadlock Detection
The bad news is that, as far as I know, there's no tool on the planet that identifies all kinds of blocking cycles for you, especially ones that consist of more than one kind of blocking. Lock hierarchies only guarantee freedom from deadlock among locks in the code you control; message contracts on communication channels only guarantee freedom from deadlock among messages.
Probably the best you can do today is to roll your own deadlock detection in code, by adopting a discipline like the following:
- Identify every condition or resource that can be waited for (a mutex, a message, a value of an atomic variable, an exclusive use of a file) and give it a unique name by creating a dummy object to represent it.
- Instrument every "start wait" and "end wait" point in your code by calling two helper functions: The first records that a condition or resource is about to be waited for (e.g,. StartWait(thing)) and should internally record the current thread's ID and the thing being waited for; it can also check to see if there is now a waiting cycle among threads and resources, and report the deadlock if that occurs. The second records that a wait has ended (e.g., EndWait(thing)) and will remove the waiting edge.
To illustrate how we can apply such a discipline, consider this deadlock between a mutex and a message that arises in the execution A->B->C:
// Global data Mutex mut; MessageQueue queue; // Thread 1 mut.lock(); // A queue.receive(); // B: blocks mut.unlock(); // Thread 2 mut.lock(); // C: blocks queue.send( msg );
We could apply a wait start/end instrumentation discipline as follows. Here the implementation of StartWait and EndWait is left for the reader, but should record which threads are waiting for which objects as described above:
// Global data Mutex mut; WaitableObject w_mut; MessageQueue queue; WaitableObject w_queue; // Thread 1 StartWait( w_mut ); mut.lock(); // A EndWait( w_mut ); StartWait( w_queue ); queue.receive(); // B: blocks EndWait( w_queue ); mut.unlock(); // Thread 2 StartWait( w_mut ); mut.lock(); // C: blocks EndWait( w_mut ); queue.send( msg );
That's a sketch of the idea. It's only a coding discipline, but it's an approach that can help you to instrument all waiting in a unified way, at least within the code you control.
Deadlock can arise whenever there is a blocking (or waiting) cycle among concurrent tasks, where each one is waiting for the next to produce some value or release some resource. Eliminate deadlocks as much as possible by applying ordering techniques like lock hierarchies and message contracts; these techniques are important, even though they are incomplete because each one deals with only a specific kind of waiting. Then consider adding your own deadlock detection by instrumenting the wait points in your code in a uniform way.
Whimsically, we might say that a more correct name for deadlock could be "deadblock"...but the world has already adopted a common spelling that's one letter shorter, and this isn't the time to try to change that. When reasoning about deadlock, remember not to forget the important "b", even though it's silent in pronunciation and in the common spelling.
 H. Sutter. "Use Lock Hierarchies To Avoid Deadlock" (DDJ, January 2008).
 H. Sutter. "Avoid Calling Unknown Code While Inside a Critical Section" (DDJ, December 2007).
 Web Services Choreography Description Language (WS-CDL) (W3C, 2005) (www.w3.org/TR/ws-cdl-10/).