<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML ><HEAD ><TITLE >Avoiding Locks: Atomic Operations</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="Common Techniques" HREF="c196.html"><LINK REL="PREVIOUS" TITLE="Avoiding Locks: Read And Write Ordering" HREF="x261.html"><LINK REL="NEXT" TITLE="Protecting A Collection of Objects: Reference Counts" HREF="x298.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="x261.html" ACCESSKEY="P" ><<< Previous</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Common Techniques</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x298.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="LOCK-AVOIDANCE-ATOMIC-OPS" ></A >Avoiding Locks: Atomic Operations</H1 ><P > There are a number of atomic operations defined in <TT CLASS="FILENAME" >include/asm/atomic.h</TT >: these are guaranteed to be seen atomically from all CPUs in the system, thus avoiding races. If your shared data consists of a single counter, say, these operations might be simpler than using spinlocks (although for anything non-trivial using spinlocks is clearer). </P ><P > Note that the atomic operations do in general not act as memory barriers. Instead you can insert a memory barrier before or after <TT CLASS="FUNCTION" >atomic_inc()</TT > or <TT CLASS="FUNCTION" >atomic_dec()</TT > by inserting <TT CLASS="FUNCTION" >smp_mb__before_atomic_inc()</TT >, <TT CLASS="FUNCTION" >smp_mb__after_atomic_inc()</TT >, <TT CLASS="FUNCTION" >smp_mb__before_atomic_dec()</TT > or <TT CLASS="FUNCTION" >smp_mb__after_atomic_dec()</TT > respectively. The advantage of using those macros instead of <TT CLASS="FUNCTION" >smp_mb()</TT > is, that they are cheaper on some platforms. </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="x261.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="x298.html" ACCESSKEY="N" >Next >>></A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Avoiding Locks: Read And Write Ordering</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c196.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Protecting A Collection of Objects: Reference Counts</TD ></TR ></TABLE ></DIV ></BODY ></HTML >