<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML ><HEAD ><TITLE >Atomic Operations</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Unreliable Guide To Hacking The Linux Kernel" HREF="book1.html"><LINK REL="PREVIOUS" TITLE="Waking Up Queued Tasks" HREF="x353.html"><LINK REL="NEXT" TITLE="Symbols" HREF="c387.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 Hacking The Linux Kernel</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="x353.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="c387.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="CHAPTER" ><H1 ><A NAME="ATOMIC-OPS" ></A >Atomic Operations</H1 ><P > Certain operations are guaranteed atomic on all platforms. The first class of operations work on <SPAN CLASS="TYPE" >atomic_t</SPAN > <TT CLASS="FILENAME" >include/asm/atomic.h</TT >; this contains a signed integer (at least 24 bits long), and you must use these functions to manipulate or read atomic_t variables. <TT CLASS="FUNCTION" >atomic_read()</TT > and <TT CLASS="FUNCTION" >atomic_set()</TT > get and set the counter, <TT CLASS="FUNCTION" >atomic_add()</TT >, <TT CLASS="FUNCTION" >atomic_sub()</TT >, <TT CLASS="FUNCTION" >atomic_inc()</TT >, <TT CLASS="FUNCTION" >atomic_dec()</TT >, and <TT CLASS="FUNCTION" >atomic_dec_and_test()</TT > (returns <SPAN CLASS="RETURNVALUE" >true</SPAN > if it was decremented to zero). </P ><P > Yes. It returns <SPAN CLASS="RETURNVALUE" >true</SPAN > (i.e. != 0) if the atomic variable is zero. </P ><P > Note that these functions are slower than normal arithmetic, and so should not be used unnecessarily. On some platforms they are much slower, like 32-bit Sparc where they use a spinlock. </P ><P > The second class of atomic operations is atomic bit operations on a <SPAN CLASS="TYPE" >long</SPAN >, defined in <TT CLASS="FILENAME" >include/asm/bitops.h</TT >. These operations generally take a pointer to the bit pattern, and a bit number: 0 is the least significant bit. <TT CLASS="FUNCTION" >set_bit()</TT >, <TT CLASS="FUNCTION" >clear_bit()</TT > and <TT CLASS="FUNCTION" >change_bit()</TT > set, clear, and flip the given bit. <TT CLASS="FUNCTION" >test_and_set_bit()</TT >, <TT CLASS="FUNCTION" >test_and_clear_bit()</TT > and <TT CLASS="FUNCTION" >test_and_change_bit()</TT > do the same thing, except return true if the bit was previously set; these are particularly useful for very simple locking. </P ><P > It is possible to call these operations with bit indices greater than BITS_PER_LONG. The resulting behavior is strange on big-endian platforms though so it is a good idea not to do this. </P ><P > Note that the order of bits depends on the architecture, and in particular, the bitfield passed to these operations must be at least as large as a <SPAN CLASS="TYPE" >long</SPAN >. </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="x353.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="c387.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Waking Up Queued Tasks</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" > </TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Symbols</TD ></TR ></TABLE ></DIV ></BODY ></HTML >