<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML ><HEAD ><TITLE >Two Main Types of Kernel Locks: Spinlocks and Semaphores</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="Introduction" HREF="c21.html"><LINK REL="NEXT" TITLE="Read/Write Lock Variants" HREF="x105.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="c21.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x105.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="CHAPTER" ><H1 ><A NAME="LOCKS" ></A >Two Main Types of Kernel Locks: Spinlocks and Semaphores</H1 ><P > There are two main types of kernel locks. The fundamental type is the spinlock (<TT CLASS="FILENAME" >include/asm/spinlock.h</TT >), 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 semaphore (<TT CLASS="FILENAME" >include/asm/semaphore.h</TT >): it can have more than one holder at any time (the number decided at initialization time), although it is most commonly used as a single-holder lock (a mutex). If you can't get a semaphore, your task will put itself on the queue, and be woken up when the semaphore is released. This means the CPU will do something else while you are waiting, but there are many cases when you simply can't sleep (see <A HREF="x315.html" >the Section called <I >Things Which Sleep</I > in the chapter called <I >Common Techniques</I ></A >), and so have to use a spinlock instead. </P ><P > Neither type of lock is recursive: see <A HREF="x214.html" >the Section called <I >Deadlock: Simple and Advanced</I > in the chapter called <I >Common Techniques</I ></A >. </P ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="UNIPROCESSOR" ></A >Locks and Uniprocessor Kernels</H1 ><P > For kernels compiled without <SPAN CLASS="SYMBOL" >CONFIG_SMP</SPAN >, spinlocks do not exist at all. This is an excellent design decision: when no-one else can run at the same time, there is no reason to have a lock at all. </P ><P > You should always test your locking code with <SPAN CLASS="SYMBOL" >CONFIG_SMP</SPAN > enabled, even if you don't have an SMP test box, because it will still catch some (simple) kinds of deadlock. </P ><P > Semaphores still exist, because they are required for synchronization between <A HREF="g388.html#GLOSS-USERCONTEXT" ><I CLASS="FIRSTTERM" >user contexts</I ></A >, as we will see below. </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="c21.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="x105.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Introduction</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" > </TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Read/Write Lock Variants</TD ></TR ></TABLE ></DIV ></BODY ></HTML >