<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><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.75.2" /><link rel="home" href="index.html" title="Writing an ALSA Driver" /><link rel="up" href="ch05.html" title="Chapter 5. PCM Interface" /><link rel="prev" href="ch05s07.html" title="Interrupt Handler" /><link rel="next" href="ch05s09.html" title="Constraints" /></head><body><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="ch05s07.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PCM Interface</th><td width="20%" align="right"> <a accesskey="n" href="ch05s09.html">Next</a></td></tr></table><hr /></div><div class="section" title="Atomicity"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="pcm-interface-atomicity"></a>Atomicity</h2></div></div></div><p> One of the most important (and thus difficult to debug) problems in kernel programming are race conditions. In the Linux kernel, they are usually avoided via spin-locks, mutexes or semaphores. In general, if a race condition can happen in an interrupt handler, it has to be managed atomically, and you have to use a spinlock to protect the critical session. If the critical section is not in interrupt handler code and if taking a relatively long time to execute is acceptable, you should use mutexes or semaphores instead. </p><p> As already seen, some pcm callbacks are atomic and some are not. For example, the <em class="parameter"><code>hw_params</code></em> callback is non-atomic, while <em class="parameter"><code>trigger</code></em> callback is atomic. This means, the latter is called already in a spinlock held by the PCM middle layer. Please take this atomicity into account when you choose a locking scheme in the callbacks. </p><p> In the atomic callbacks, you cannot use functions which may call <code class="function">schedule</code> or go to <code class="function">sleep</code>. Semaphores and mutexes can sleep, and hence they cannot be used inside the atomic callbacks (e.g. <em class="parameter"><code>trigger</code></em> callback). To implement some delay in such a callback, please use <code class="function">udelay()</code> or <code class="function">mdelay()</code>. </p><p> All three atomic callbacks (trigger, pointer, and ack) are called with local interrupts disabled. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch05s07.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch05.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch05s09.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Interrupt Handler </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Constraints</td></tr></table></div></body></html>