<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML ><HEAD ><TITLE >Locking Only In User Context</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="UP" TITLE="Two Main Types of Kernel Locks: Spinlocks and Semaphores" HREF="c88.html"><LINK REL="PREVIOUS" TITLE="Read/Write Lock Variants" HREF="x105.html"><LINK REL="NEXT" TITLE="Locking Between User Context and BHs" HREF="x126.html"></HEAD ><BODY CLASS="SECT1" 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="x105.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Two Main Types of Kernel Locks: Spinlocks and Semaphores</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x126.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="USERCONTEXTLOCKING" ></A >Locking Only In User Context</H1 ><P > If you have a data structure which is only ever accessed from user context, then you can use a simple semaphore (<TT CLASS="FILENAME" >linux/asm/semaphore.h</TT >) to protect it. This is the most trivial case: you initialize the semaphore to the number of resources available (usually 1), and call <TT CLASS="FUNCTION" >down_interruptible()</TT > to grab the semaphore, and <TT CLASS="FUNCTION" >up()</TT > to release it. There is also a <TT CLASS="FUNCTION" >down()</TT >, which should be avoided, because it will not return if a signal is received. </P ><P > Example: <TT CLASS="FILENAME" >linux/net/core/netfilter.c</TT > allows registration of new <TT CLASS="FUNCTION" >setsockopt()</TT > and <TT CLASS="FUNCTION" >getsockopt()</TT > calls, with <TT CLASS="FUNCTION" >nf_register_sockopt()</TT >. Registration and de-registration are only done on module load and unload (and boot time, where there is no concurrency), and the list of registrations is only consulted for an unknown <TT CLASS="FUNCTION" >setsockopt()</TT > or <TT CLASS="FUNCTION" >getsockopt()</TT > system call. The <TT CLASS="VARNAME" >nf_sockopt_mutex</TT > is perfect to protect this, especially since the setsockopt and getsockopt calls may well sleep. </P ></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="x105.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="x126.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Read/Write Lock Variants</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c88.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Locking Between User Context and BHs</TD ></TR ></TABLE ></DIV ></BODY ></HTML >