1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter&#160;8.&#160;Atomic Operations</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Unreliable Guide To Hacking The Linux Kernel"><link rel="up" href="index.html" title="Unreliable Guide To Hacking The Linux Kernel"><link rel="prev" href="queue-waking.html" title="Waking Up Queued Tasks"><link rel="next" href="symbols.html" title="Chapter&#160;9.&#160;Symbols"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&#160;8.&#160;Atomic Operations</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="queue-waking.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="symbols.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="atomic-ops"></a>Chapter&#160;8.&#160;Atomic Operations</h1></div></div></div><p>
2   Certain operations are guaranteed atomic on all platforms.  The
3   first class of operations work on <span class="type">atomic_t</span>
4
5   <code class="filename">include/asm/atomic.h</code>; this
6   contains a signed integer (at least 32 bits long), and you must use
7   these functions to manipulate or read atomic_t variables.
8   <code class="function">atomic_read()</code> and
9   <code class="function">atomic_set()</code> get and set the counter,
10   <code class="function">atomic_add()</code>,
11   <code class="function">atomic_sub()</code>,
12   <code class="function">atomic_inc()</code>,
13   <code class="function">atomic_dec()</code>, and
14   <code class="function">atomic_dec_and_test()</code> (returns
15   <span class="returnvalue">true</span> if it was decremented to zero).
16  </p><p>
17   Yes.  It returns <span class="returnvalue">true</span> (i.e. != 0) if the
18   atomic variable is zero.
19  </p><p>
20   Note that these functions are slower than normal arithmetic, and
21   so should not be used unnecessarily.
22  </p><p>
23   The second class of atomic operations is atomic bit operations on an
24   <span class="type">unsigned long</span>, defined in
25
26   <code class="filename">include/linux/bitops.h</code>.  These
27   operations generally take a pointer to the bit pattern, and a bit
28   number: 0 is the least significant bit.
29   <code class="function">set_bit()</code>, <code class="function">clear_bit()</code>
30   and <code class="function">change_bit()</code> set, clear, and flip the
31   given bit.  <code class="function">test_and_set_bit()</code>,
32   <code class="function">test_and_clear_bit()</code> and
33   <code class="function">test_and_change_bit()</code> do the same thing,
34   except return true if the bit was previously set; these are
35   particularly useful for atomically setting flags.
36  </p><p>
37   It is possible to call these operations with bit indices greater
38   than BITS_PER_LONG.  The resulting behavior is strange on big-endian
39   platforms though so it is a good idea not to do this.
40  </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="queue-waking.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="symbols.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Waking Up Queued Tasks&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Chapter&#160;9.&#160;Symbols</td></tr></table></div></body></html>
41