Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > a41e5ac5fb332416cb8019ae392f1f99 > files > 325

lib64pulseaudio-devel-10.0-1.1.mga6.x86_64.rpm

<!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/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>PulseAudio: Threaded Main Loop</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">PulseAudio
   &#160;<span id="projectnumber">10.0.0-1.1.mga6</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',false,false,'search.php','Search');
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('threaded_mainloop.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Threaded Main Loop </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="overv_sec"></a>
Overview</h1>
<p>The threaded main loop implementation is a special version of the primary main loop implementation (see <a class="el" href="mainloop.html">Main Loop</a>). For the basic design, see its documentation.</p>
<p>The added feature in the threaded main loop is that it spawns a new thread that runs the real main loop. This allows a synchronous application to use the asynchronous API without risking to stall the PulseAudio library.</p>
<h1><a class="anchor" id="creat_sec"></a>
Creation</h1>
<p>A pa_threaded_mainloop object is created using <a class="el" href="thread-mainloop_8h.html#a3b8bf5f49d7eb9d6fe6ea008eb6482f2" title="Allocate a new threaded main loop object. ">pa_threaded_mainloop_new()</a>. This will only allocate the required structures though, so to use it the thread must also be started. This is done through <a class="el" href="thread-mainloop_8h.html#a2faf4ddb9a94f74d6e7ef80843e73d30" title="Start the event loop thread. ">pa_threaded_mainloop_start()</a>, after which you can start using the main loop.</p>
<h1><a class="anchor" id="destr_sec"></a>
Destruction</h1>
<p>When the PulseAudio connection has been terminated, the thread must be stopped and the resources freed. Stopping the thread is done using <a class="el" href="thread-mainloop_8h.html#a2a017a59832e81a44aa8989c403fb70e" title="Terminate the event loop thread cleanly. ">pa_threaded_mainloop_stop()</a>, which must be called without the lock (see below) held. When that function returns, the thread is stopped and the pa_threaded_mainloop object can be freed using <a class="el" href="thread-mainloop_8h.html#a00afff8173668f12e19d574cd4e4134b" title="Free a threaded main loop object. ">pa_threaded_mainloop_free()</a>.</p>
<h1><a class="anchor" id="lock_sec"></a>
Locking</h1>
<p>Since the PulseAudio API doesn't allow concurrent accesses to objects, a locking scheme must be used to guarantee safe usage. The threaded main loop API provides such a scheme through the functions <a class="el" href="thread-mainloop_8h.html#a2fd002f510ed24cabacb63a26bf4c977" title="Lock the event loop object, effectively blocking the event loop thread from processing events...">pa_threaded_mainloop_lock()</a> and <a class="el" href="thread-mainloop_8h.html#ab2b2beaa3a66135da67d61db211f0829" title="Unlock the event loop object, inverse of pa_threaded_mainloop_lock(). ">pa_threaded_mainloop_unlock()</a>.</p>
<p>The lock is recursive, so it's safe to use it multiple times from the same thread. Just make sure you call <a class="el" href="thread-mainloop_8h.html#ab2b2beaa3a66135da67d61db211f0829" title="Unlock the event loop object, inverse of pa_threaded_mainloop_lock(). ">pa_threaded_mainloop_unlock()</a> the same number of times you called <a class="el" href="thread-mainloop_8h.html#a2fd002f510ed24cabacb63a26bf4c977" title="Lock the event loop object, effectively blocking the event loop thread from processing events...">pa_threaded_mainloop_lock()</a>.</p>
<p>The lock needs to be held whenever you call any PulseAudio function that uses an object associated with this main loop. Make sure you do not hold on to the lock more than necessary though, as the threaded main loop stops while the lock is held.</p>
<p>Example:</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> my_check_stream_func(<a class="code" href="thread-mainloop_8h.html#a40393b9b64dc8213bcd5f017344baf74">pa_threaded_mainloop</a> *m, <a class="code" href="stream_8h.html#a960d798980692a2ff01e5a70553d4f29">pa_stream</a> *s) {</div><div class="line">    <a class="code" href="def_8h.html#a112cf6dbe9ac5554fddc8da251797477">pa_stream_state_t</a> state;</div><div class="line"></div><div class="line">    <a class="code" href="thread-mainloop_8h.html#a2fd002f510ed24cabacb63a26bf4c977">pa_threaded_mainloop_lock</a>(m);</div><div class="line"></div><div class="line">    state = <a class="code" href="stream_8h.html#ab1da38d494d6485e35f0715f40dff0ab">pa_stream_get_state</a>(s);</div><div class="line"></div><div class="line">    <a class="code" href="thread-mainloop_8h.html#ab2b2beaa3a66135da67d61db211f0829">pa_threaded_mainloop_unlock</a>(m);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (state == <a class="code" href="def_8h.html#a71341d6e189549fc0bd25ab669016df9a1e72fb989b308e2317c0b0949afe5446">PA_STREAM_READY</a>)</div><div class="line">        printf(<span class="stringliteral">&quot;Stream is ready!&quot;</span>);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        printf(<span class="stringliteral">&quot;Stream is not ready!&quot;</span>);</div><div class="line">}</div></div><!-- fragment --><h1><a class="anchor" id="cb_sec"></a>
Callbacks</h1>
<p>Callbacks in PulseAudio are asynchronous, so they require extra care when using them together with a threaded main loop.</p>
<p>The easiest way to turn the callback based operations into synchronous ones, is to simply wait for the callback to be called and continue from there. This is the approach chosen in PulseAudio's threaded API.</p>
<h2><a class="anchor" id="basic_subsec"></a>
Basic callbacks</h2>
<p>For the basic case, where all that is required is to wait for the callback to be invoked, the code should look something like this:</p>
<p>Example:</p>
<div class="fragment"><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> my_drain_callback(<a class="code" href="stream_8h.html#a960d798980692a2ff01e5a70553d4f29">pa_stream</a> *s, <span class="keywordtype">int</span> success, <span class="keywordtype">void</span> *userdata) {</div><div class="line">    <a class="code" href="thread-mainloop_8h.html#a40393b9b64dc8213bcd5f017344baf74">pa_threaded_mainloop</a> *m;</div><div class="line"></div><div class="line">    m = userdata;</div><div class="line">    assert(m);</div><div class="line"></div><div class="line">    <a class="code" href="thread-mainloop_8h.html#ad253b70911af81c04417793841a15766">pa_threaded_mainloop_signal</a>(m, 0);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> my_drain_stream_func(<a class="code" href="thread-mainloop_8h.html#a40393b9b64dc8213bcd5f017344baf74">pa_threaded_mainloop</a> *m, <a class="code" href="stream_8h.html#a960d798980692a2ff01e5a70553d4f29">pa_stream</a> *s) {</div><div class="line">    <a class="code" href="operation_8h.html#a5614a07f2e7a129e4cb16596ed452a0c">pa_operation</a> *o;</div><div class="line"></div><div class="line">    <a class="code" href="thread-mainloop_8h.html#a2fd002f510ed24cabacb63a26bf4c977">pa_threaded_mainloop_lock</a>(m);</div><div class="line"></div><div class="line">    o = <a class="code" href="stream_8h.html#a8d263f188073f244b3820f3f50db4ba5">pa_stream_drain</a>(s, my_drain_callback, m);</div><div class="line">    assert(o);</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span> (<a class="code" href="operation_8h.html#acabeaea114e87658e1dadc214d7a2c35">pa_operation_get_state</a>(o) == <a class="code" href="def_8h.html#ac0a07dd2ef771a72d48e5ba2c56ac3bca9486b0f1b7da21dcc34f5cb3f7091c6f">PA_OPERATION_RUNNING</a>)</div><div class="line">        <a class="code" href="thread-mainloop_8h.html#ac96d19260567d0406350f1b3a14c4bed">pa_threaded_mainloop_wait</a>(m);</div><div class="line"></div><div class="line">    <a class="code" href="operation_8h.html#a8d2ef9bb2ff961ee31675882bf125227">pa_operation_unref</a>(o);</div><div class="line"></div><div class="line">    <a class="code" href="thread-mainloop_8h.html#ab2b2beaa3a66135da67d61db211f0829">pa_threaded_mainloop_unlock</a>(m);</div><div class="line">}</div></div><!-- fragment --><p>The main function, my_drain_stream_func(), will wait for the callback to be called using <a class="el" href="thread-mainloop_8h.html#ac96d19260567d0406350f1b3a14c4bed" title="Wait for an event to be signalled by the event loop thread. ">pa_threaded_mainloop_wait()</a>.</p>
<p>If your application is multi-threaded, then this waiting must be done inside a while loop. The reason for this is that multiple threads might be using <a class="el" href="thread-mainloop_8h.html#ac96d19260567d0406350f1b3a14c4bed" title="Wait for an event to be signalled by the event loop thread. ">pa_threaded_mainloop_wait()</a> at the same time. Each thread must therefore verify that it was its callback that was invoked. Also the underlying OS synchronization primitives are usually not free of spurious wake-ups, so a <a class="el" href="thread-mainloop_8h.html#ac96d19260567d0406350f1b3a14c4bed" title="Wait for an event to be signalled by the event loop thread. ">pa_threaded_mainloop_wait()</a> must be called within a loop even if you have only one thread waiting.</p>
<p>The callback, my_drain_callback(), indicates to the main function that it has been called using <a class="el" href="thread-mainloop_8h.html#ad253b70911af81c04417793841a15766" title="Signal all threads waiting for a signalling event in pa_threaded_mainloop_wait(). ...">pa_threaded_mainloop_signal()</a>.</p>
<p>As you can see, <a class="el" href="thread-mainloop_8h.html#ac96d19260567d0406350f1b3a14c4bed" title="Wait for an event to be signalled by the event loop thread. ">pa_threaded_mainloop_wait()</a> may only be called with the lock held. The same thing is true for <a class="el" href="thread-mainloop_8h.html#ad253b70911af81c04417793841a15766" title="Signal all threads waiting for a signalling event in pa_threaded_mainloop_wait(). ...">pa_threaded_mainloop_signal()</a>, but as the lock is held before the callback is invoked, you do not have to deal with that.</p>
<p>The functions will not dead lock because the wait function will release the lock before waiting and then regrab it once it has been signalled. For those of you familiar with threads, the behaviour is that of a condition variable.</p>
<h2><a class="anchor" id="data_subsec"></a>
Data callbacks</h2>
<p>For many callbacks, simply knowing that they have been called is insufficient. The callback also receives some data that is desired. To access this data safely, we must extend our example a bit:</p>
<div class="fragment"><div class="line"><span class="keyword">static</span> <span class="keyword">volatile</span> <span class="keywordtype">int</span> *drain_result = NULL;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> my_drain_callback(<a class="code" href="stream_8h.html#a960d798980692a2ff01e5a70553d4f29">pa_stream</a>*s, <span class="keywordtype">int</span> success, <span class="keywordtype">void</span> *userdata) {</div><div class="line">    <a class="code" href="thread-mainloop_8h.html#a40393b9b64dc8213bcd5f017344baf74">pa_threaded_mainloop</a> *m;</div><div class="line"></div><div class="line">    m = userdata;</div><div class="line">    assert(m);</div><div class="line"></div><div class="line">    drain_result = &amp;success;</div><div class="line"></div><div class="line">    <a class="code" href="thread-mainloop_8h.html#ad253b70911af81c04417793841a15766">pa_threaded_mainloop_signal</a>(m, 1);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> my_drain_stream_func(<a class="code" href="thread-mainloop_8h.html#a40393b9b64dc8213bcd5f017344baf74">pa_threaded_mainloop</a> *m, <a class="code" href="stream_8h.html#a960d798980692a2ff01e5a70553d4f29">pa_stream</a> *s) {</div><div class="line">    <a class="code" href="operation_8h.html#a5614a07f2e7a129e4cb16596ed452a0c">pa_operation</a> *o;</div><div class="line"></div><div class="line">    <a class="code" href="thread-mainloop_8h.html#a2fd002f510ed24cabacb63a26bf4c977">pa_threaded_mainloop_lock</a>(m);</div><div class="line"></div><div class="line">    o = <a class="code" href="stream_8h.html#a8d263f188073f244b3820f3f50db4ba5">pa_stream_drain</a>(s, my_drain_callback, m);</div><div class="line">    assert(o);</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span> (drain_result == NULL)</div><div class="line">        <a class="code" href="thread-mainloop_8h.html#ac96d19260567d0406350f1b3a14c4bed">pa_threaded_mainloop_wait</a>(m);</div><div class="line"></div><div class="line">    <a class="code" href="operation_8h.html#a8d2ef9bb2ff961ee31675882bf125227">pa_operation_unref</a>(o);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (*drain_result)</div><div class="line">        printf(<span class="stringliteral">&quot;Success!&quot;</span>);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        printf(<span class="stringliteral">&quot;Bitter defeat...&quot;</span>);</div><div class="line"></div><div class="line">    <a class="code" href="thread-mainloop_8h.html#a254b0862e30845a4b0427a7a74b3db91">pa_threaded_mainloop_accept</a>(m);</div><div class="line"></div><div class="line">    <a class="code" href="thread-mainloop_8h.html#ab2b2beaa3a66135da67d61db211f0829">pa_threaded_mainloop_unlock</a>(m);</div><div class="line">}</div></div><!-- fragment --><p>The example is a bit silly as it would probably have been easier to just copy the contents of success, but for larger data structures this can be wasteful.</p>
<p>The difference here compared to the basic callback is the value 1 passed to <a class="el" href="thread-mainloop_8h.html#ad253b70911af81c04417793841a15766" title="Signal all threads waiting for a signalling event in pa_threaded_mainloop_wait(). ...">pa_threaded_mainloop_signal()</a> and the call to <a class="el" href="thread-mainloop_8h.html#a254b0862e30845a4b0427a7a74b3db91" title="Accept a signal from the event thread issued with pa_threaded_mainloop_signal(). ">pa_threaded_mainloop_accept()</a>. What will happen is that <a class="el" href="thread-mainloop_8h.html#ad253b70911af81c04417793841a15766" title="Signal all threads waiting for a signalling event in pa_threaded_mainloop_wait(). ...">pa_threaded_mainloop_signal()</a> will signal the main function and then wait. The main function is then free to use the data in the callback until <a class="el" href="thread-mainloop_8h.html#a254b0862e30845a4b0427a7a74b3db91" title="Accept a signal from the event thread issued with pa_threaded_mainloop_signal(). ">pa_threaded_mainloop_accept()</a> is called, which will allow the callback to continue.</p>
<p>Note that <a class="el" href="thread-mainloop_8h.html#a254b0862e30845a4b0427a7a74b3db91" title="Accept a signal from the event thread issued with pa_threaded_mainloop_signal(). ">pa_threaded_mainloop_accept()</a> must be called some time between exiting the while loop and unlocking the main loop! Failure to do so will result in a race condition. I.e. it is not ok to release the lock and regrab it before calling <a class="el" href="thread-mainloop_8h.html#a254b0862e30845a4b0427a7a74b3db91" title="Accept a signal from the event thread issued with pa_threaded_mainloop_signal(). ">pa_threaded_mainloop_accept()</a>.</p>
<h2><a class="anchor" id="async_subsec"></a>
Asynchronous callbacks</h2>
<p>PulseAudio also has callbacks that are completely asynchronous, meaning that they can be called at any time. The threaded main loop API provides the locking mechanism to handle concurrent accesses, but nothing else. Applications will have to handle communication from the callback to the main program through their own mechanisms.</p>
<p>The callbacks that are completely asynchronous are:</p>
<ul>
<li>State callbacks for contexts, streams, etc. </li>
<li>Subscription notifications </li>
</ul>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="index.html">index</a></li><li class="navelem"><a class="el" href="async.html">Asynchronous API</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.13 </li>
  </ul>
</div>
</body>
</html>