[concurrency-interest] Enforced lock ordering

William M. Shubert wms@igoweb.org
12 Apr 2003 17:51:09 -0700


Hi. I just joined the list, and see that last month there was a post
related to enforced lock ordering. Before all threaded applications I've
used had a lock order to prevent deadlock, and in some cases a C library
that forces all locks to be strictly ordered and aborts the program when
you try to acquire locks out of order was used. With the right tool set,
checking your lock ordering can be done pretty painlessly, and it helps
avoid disasterous "about once a month the damn program just locks up"
type bugs. I see that the concurrency API is geared more towards using
timeouts, but that seems to really be asking for major problems on
systems where reliability is important - if you get halfway through a
procedure, then timeout on a lock indicating possible deadlock, you must
be able to safely undo all work so far. This would be a process that is
tricky, happens rarely, and is hard to test, a great way to make sure
there are bugs in your code.

I made a cut at a library to force lock ordering for the current java
monitor operations. You can see javadoc at
http://www.igoweb.org/~wms/org/igoweb/util/LockOrder.html - it just
requires an extra function call before each "synchronized" or "wait"
block. These extra function calls throw runtime exceptions if the
operation you are about to perform would break lock ordering. The
implementation was quite simple (under 300 lines). Any thoughts on
providing such a system to go with the semaphores, etc., of the
concurrency API?