<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968" /><title>Chapter 3. Locking in the Linux Kernel</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><link rel="home" href="index.html" title="Unreliable Guide To Locking" /><link rel="up" href="index.html" title="Unreliable Guide To Locking" /><link rel="prev" href="ch02.html" title="Chapter 2. The Problem With Concurrency" /><link rel="next" href="ch03s02.html" title="Locks and Uniprocessor Kernels" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 3. Locking in the Linux Kernel</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch03s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter 3. Locking in the Linux Kernel"><div class="titlepage"><div><div><h2 class="title"><a id="locks"></a>Chapter 3. Locking in the Linux Kernel</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch03.html#lock-intro">Two Main Types of Kernel Locks: Spinlocks and Mutexes</a></span></dt><dt><span class="sect1"><a href="ch03s02.html">Locks and Uniprocessor Kernels</a></span></dt><dt><span class="sect1"><a href="ch03s03.html">Locking Only In User Context</a></span></dt><dt><span class="sect1"><a href="ch03s04.html">Locking Between User Context and Softirqs</a></span></dt><dt><span class="sect1"><a href="ch03s05.html">Locking Between User Context and Tasklets</a></span></dt><dt><span class="sect1"><a href="ch03s06.html">Locking Between User Context and Timers</a></span></dt><dt><span class="sect1"><a href="ch03s07.html">Locking Between Tasklets/Timers</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03s07.html#lock-tasklets-same">The Same Tasklet/Timer</a></span></dt><dt><span class="sect2"><a href="ch03s07.html#lock-tasklets-different">Different Tasklets/Timers</a></span></dt></dl></dd><dt><span class="sect1"><a href="ch03s08.html">Locking Between Softirqs</a></span></dt><dd><dl><dt><span class="sect2"><a href="ch03s08.html#lock-softirqs-same">The Same Softirq</a></span></dt><dt><span class="sect2"><a href="ch03s08.html#lock-softirqs-different">Different Softirqs</a></span></dt></dl></dd></dl></div><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: <span class="emphasis"><em>keep it simple</em></span>. </p><p> Be reluctant to introduce new locks. </p><p> Strangely enough, this last one is the exact reverse of my advice when you <span class="emphasis"><em>have</em></span> slept with someone crazier than yourself. And you should think about getting a big dog. </p><div class="sect1" title="Two Main Types of Kernel Locks: Spinlocks and Mutexes"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="lock-intro"></a>Two Main Types of Kernel Locks: Spinlocks and Mutexes</h2></div></div></div><p> There are two main types of kernel locks. The fundamental type is the spinlock (<code class="filename">include/asm/spinlock.h</code>), which is a very simple single-holder lock: if you can't get the spinlock, you keep trying (spinning) until you can. Spinlocks are very small and fast, and can be used anywhere. </p><p> The second type is a mutex (<code class="filename">include/linux/mutex.h</code>): it is like a spinlock, but you may block holding a mutex. If you can't lock a mutex, your task will suspend itself, and be woken up when the mutex is released. This means the CPU can do something else while you are waiting. There are many cases when you simply can't sleep (see <a class="xref" href="ch10.html" title="Chapter 10. What Functions Are Safe To Call From Interrupts?">Chapter 10, <i>What Functions Are Safe To Call From Interrupts?</i></a>), and so have to use a spinlock instead. </p><p> Neither type of lock is recursive: see <a class="xref" href="ch08.html#deadlock" title="Deadlock: Simple and Advanced">the section called “Deadlock: Simple and Advanced”</a>. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch03s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 2. The Problem With Concurrency </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Locks and Uniprocessor Kernels</td></tr></table></div></body></html>