Sat Jun 25 13:19:14 CEST 2011
Locking and callbacks
Is it possible (and sound?) to do the following:
When a mutex is locked by the current thread, do not lock (causing
deadlock) but throw an error.
This shows up in a case where I'm using callbacks inside a lock to
avoid manually having to manage resources. However, the hierarchy of
the locks of the application is such that it isn't completely
impossible to avoid deadlocks: it's possible to call functions that
ultimately depend on a low level lock the callback has already
reserved, in this particular case a disk lock.
Probably this is just a sign that the cause of the inner deadlock
needs to be decoupled, i.e. by using a thread. In this case, the
inner cause is a logger which might aquire a disk lock, however it is
possible to put the logger in a separate thread and use a buffer to
capture logging data until the disk log is released.
Is there a (practical) way to statically verify lock hierarchy
violations? I.e. this mentions a mechanism where lock hierarchy is
constructed (probably through control flow graph) and it triggers an
error when this graph contains loops, i.e. joins on the same lock.
It seems that it is possible to do it at run-time. That drdobbs
article also mentions layer violation through callbacks, which is
exactly my problem. I'm using callbacks to abstract locking, but this
then has the potential to violate hierarchies.