July 31, 2008
The Many Faces of DeadlockGeneral 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:
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:
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:
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.
Summary
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. Notes
[1] H. Sutter. "Use Lock Hierarchies To Avoid Deadlock" (DDJ, January 2008).
[2] H. Sutter. "Avoid Calling Unknown Code While Inside a Critical Section" (DDJ, December 2007).
[3] Web Services Choreography Description Language (WS-CDL) (W3C, 2005) (www.w3.org/TR/ws-cdl-10/).
|
|
||||||||||||||||||||||||||||||
|
|
|
|