1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Preventing Deadlock</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Unreliable Guide To Locking"><link rel="up" href="common-problems.html" title="Chapter 8. Common Problems"><link rel="prev" href="common-problems.html" title="Chapter 8. Common Problems"><link rel="next" href="racing-timers.html" title="Racing Timers: A Kernel Pastime"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Preventing Deadlock</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="common-problems.html">Prev</a> </td><th width="60%" align="center">Chapter 8. Common Problems</th><td width="20%" align="right"> <a accesskey="n" href="racing-timers.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="techs-deadlock-prevent"></a>Preventing Deadlock</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="techs-deadlock-prevent.html#techs-deadlock-overprevent">Overzealous Prevention Of Deadlocks</a></span></dt></dl></div><p> 2 Textbooks will tell you that if you always lock in the same 3 order, you will never get this kind of deadlock. Practice 4 will tell you that this approach doesn't scale: when I 5 create a new lock, I don't understand enough of the kernel 6 to figure out where in the 5000 lock hierarchy it will fit. 7 </p><p> 8 The best locks are encapsulated: they never get exposed in 9 headers, and are never held around calls to non-trivial 10 functions outside the same file. You can read through this 11 code and see that it will never deadlock, because it never 12 tries to grab another lock while it has that one. People 13 using your code don't even need to know you are using a 14 lock. 15 </p><p> 16 A classic problem here is when you provide callbacks or 17 hooks: if you call these with the lock held, you risk simple 18 deadlock, or a deadly embrace (who knows what the callback 19 will do?). Remember, the other programmers are out to get 20 you, so don't do this. 21 </p><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="techs-deadlock-overprevent"></a>Overzealous Prevention Of Deadlocks</h3></div></div></div><p> 22 Deadlocks are problematic, but not as bad as data 23 corruption. Code which grabs a read lock, searches a list, 24 fails to find what it wants, drops the read lock, grabs a 25 write lock and inserts the object has a race condition. 26 </p><p> 27 If you don't see why, please stay the fuck away from my code. 28 </p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="common-problems.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="common-problems.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="racing-timers.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 8. Common Problems </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Racing Timers: A Kernel Pastime</td></tr></table></div></body></html> 29