<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML ><HEAD ><TITLE >Common Techniques</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Unreliable Guide To Locking" HREF="book1.html"><LINK REL="PREVIOUS" TITLE="Hard IRQ Context" HREF="c181.html"><LINK REL="NEXT" TITLE="Deadlock: Simple and Advanced" HREF="x214.html"></HEAD ><BODY CLASS="CHAPTER" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Unreliable Guide To Locking</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="c181.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x214.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="CHAPTER" ><H1 ><A NAME="COMMON-TECHNIQUES" ></A >Common Techniques</H1 ><P > This section lists some common dilemmas and the standard solutions used in the Linux kernel code. If you use these, people will find your code simpler to understand. </P ><P > If I could give you one piece of advice: never sleep with anyone crazier than yourself. But if I had to give you advice on locking: <I CLASS="EMPHASIS" >keep it simple</I >. </P ><P > Lock data, not code. </P ><P > Be reluctant to introduce new locks. </P ><P > Strangely enough, this is the exact reverse of my advice when you <I CLASS="EMPHASIS" >have</I > slept with someone crazier than yourself. </P ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="TECHNIQUES-NO-WRITERS" ></A >No Writers in Interrupt Context</H1 ><P > There is a fairly common case where an interrupt handler needs access to a critical region, but does not need write access. In this case, you do not need to use <TT CLASS="FUNCTION" >read_lock_irq()</TT >, but only <TT CLASS="FUNCTION" >read_lock()</TT > everywhere (since if an interrupt occurs, the irq handler will only try to grab a read lock, which won't deadlock). You will still need to use <TT CLASS="FUNCTION" >write_lock_irq()</TT >. </P ><P > Similar logic applies to locking between softirqs/tasklets/BHs which never need a write lock, and user context: <TT CLASS="FUNCTION" >read_lock()</TT > and <TT CLASS="FUNCTION" >write_lock_bh()</TT >. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="c181.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="book1.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x214.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Hard IRQ Context</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" > </TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Deadlock: Simple and Advanced</TD ></TR ></TABLE ></DIV ></BODY ></HTML >