1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Atomicity</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Writing an ALSA Driver"><link rel="up" href="pcm-interface.html" title="Chapter&#160;5.&#160;PCM Interface"><link rel="prev" href="pcm-interface-interrupt-handler.html" title="Interrupt Handler"><link rel="next" href="pcm-interface-constraints.html" title="Constraints"></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">Atomicity</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="pcm-interface-interrupt-handler.html">Prev</a>&#160;</td><th width="60%" align="center">Chapter&#160;5.&#160;PCM Interface</th><td width="20%" align="right">&#160;<a accesskey="n" href="pcm-interface-constraints.html">Next</a></td></tr></table><hr></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="pcm-interface-atomicity"></a>Atomicity</h2></div></div></div><p>
2      One of the most important (and thus difficult to debug) problems
3      in kernel programming are race conditions.
4      In the Linux kernel, they are usually avoided via spin-locks, mutexes
5      or semaphores.  In general, if a race condition can happen
6      in an interrupt handler, it has to be managed atomically, and you
7      have to use a spinlock to protect the critical session. If the
8      critical section is not in interrupt handler code and
9      if taking a relatively long time to execute is acceptable, you
10      should use mutexes or semaphores instead.
11      </p><p>
12      As already seen, some pcm callbacks are atomic and some are
13      not.  For example, the <em class="parameter"><code>hw_params</code></em> callback is
14      non-atomic, while <em class="parameter"><code>trigger</code></em> callback is
15      atomic.  This means, the latter is called already in a spinlock
16      held by the PCM middle layer. Please take this atomicity into
17      account when you choose a locking scheme in the callbacks.
18      </p><p>
19      In the atomic callbacks, you cannot use functions which may call
20      <code class="function">schedule</code> or go to
21      <code class="function">sleep</code>.  Semaphores and mutexes can sleep,
22      and hence they cannot be used inside the atomic callbacks
23      (e.g. <em class="parameter"><code>trigger</code></em> callback).
24      To implement some delay in such a callback, please use
25      <code class="function">udelay()</code> or <code class="function">mdelay()</code>.
26      </p><p>
27      All three atomic callbacks (trigger, pointer, and ack) are
28      called with local interrupts disabled.
29      </p><p>
30      The recent changes in PCM core code, however, allow all PCM
31      operations to be non-atomic.  This assumes that the all caller
32      sides are in non-atomic contexts.  For example, the function
33      <code class="function">snd_pcm_period_elapsed()</code> is called
34      typically from the interrupt handler.  But, if you set up the
35      driver to use a threaded interrupt handler, this call can be in
36      non-atomic context, too.  In such a case, you can set
37      <em class="structfield"><code>nonatomic</code></em> filed of
38      <span class="structname">snd_pcm</span> object after creating it.
39      When this flag is set, mutex and rwsem are used internally in
40      the PCM core instead of spin and rwlocks, so that you can call
41      all PCM functions safely in a non-atomic context.
42      </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="pcm-interface-interrupt-handler.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="pcm-interface.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="pcm-interface-constraints.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Interrupt Handler&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Constraints</td></tr></table></div></body></html>
43