<!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"/> <title>RAUL: SRMWQueue.hpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.7.1 --> <div class="navigation" id="top"> <div class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File List</span></a></li> </ul> </div> <div class="header"> <div class="headertitle"> <h1>SRMWQueue.hpp</h1> </div> </div> <div class="contents"> <div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* This file is part of Raul.</span> <a name="l00002"></a>00002 <span class="comment"> * Copyright (C) 2007-2009 David Robillard <http://drobilla.net></span> <a name="l00003"></a>00003 <span class="comment"> *</span> <a name="l00004"></a>00004 <span class="comment"> * Raul is free software; you can redistribute it and/or modify it under the</span> <a name="l00005"></a>00005 <span class="comment"> * terms of the GNU General Public License as published by the Free Software</span> <a name="l00006"></a>00006 <span class="comment"> * Foundation; either version 2 of the License, or (at your option) any later</span> <a name="l00007"></a>00007 <span class="comment"> * version.</span> <a name="l00008"></a>00008 <span class="comment"> *</span> <a name="l00009"></a>00009 <span class="comment"> * Raul is distributed in the hope that it will be useful, but WITHOUT ANY</span> <a name="l00010"></a>00010 <span class="comment"> * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS</span> <a name="l00011"></a>00011 <span class="comment"> * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.</span> <a name="l00012"></a>00012 <span class="comment"> *</span> <a name="l00013"></a>00013 <span class="comment"> * You should have received a copy of the GNU General Public License along</span> <a name="l00014"></a>00014 <span class="comment"> * with this program; if not, write to the Free Software Foundation, Inc.,</span> <a name="l00015"></a>00015 <span class="comment"> * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA</span> <a name="l00016"></a>00016 <span class="comment"> */</span> <a name="l00017"></a>00017 <a name="l00018"></a>00018 <span class="preprocessor">#ifndef RAUL_SRMW_QUEUE_HPP</span> <a name="l00019"></a>00019 <span class="preprocessor"></span><span class="preprocessor">#define RAUL_SRMW_QUEUE_HPP</span> <a name="l00020"></a>00020 <span class="preprocessor"></span> <a name="l00021"></a>00021 <span class="preprocessor">#include <cassert></span> <a name="l00022"></a>00022 <span class="preprocessor">#include <cstdlib></span> <a name="l00023"></a>00023 <span class="preprocessor">#include <cmath></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <boost/utility.hpp></span> <a name="l00025"></a>00025 <span class="preprocessor">#include "raul/AtomicInt.hpp"</span> <a name="l00026"></a>00026 <a name="l00027"></a>00027 <span class="keyword">namespace </span>Raul { <a name="l00028"></a>00028 <a name="l00029"></a>00029 <a name="l00051"></a>00051 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00052"></a><a class="code" href="classRaul_1_1SRMWQueue.html">00052</a> <span class="keyword">class </span><a class="code" href="classRaul_1_1SRMWQueue.html" title="Realtime-safe single-reader multi-writer queue (aka lock-free ringbuffer).">SRMWQueue</a> : boost::noncopyable <a name="l00053"></a>00053 { <a name="l00054"></a>00054 <span class="keyword">public</span>: <a name="l00055"></a>00055 <a class="code" href="classRaul_1_1SRMWQueue.html" title="Realtime-safe single-reader multi-writer queue (aka lock-free ringbuffer).">SRMWQueue</a>(<span class="keywordtype">size_t</span> size); <a name="l00056"></a>00056 ~<a class="code" href="classRaul_1_1SRMWQueue.html" title="Realtime-safe single-reader multi-writer queue (aka lock-free ringbuffer).">SRMWQueue</a>(); <a name="l00057"></a>00057 <a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="comment">// Any thread:</span> <a name="l00060"></a>00060 <a name="l00061"></a>00061 <span class="keyword">inline</span> <span class="keywordtype">size_t</span> capacity()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> _size-1; } <a name="l00062"></a>00062 <a name="l00063"></a>00063 <a name="l00064"></a>00064 <span class="comment">// Write thread(s):</span> <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a class="code" href="classRaul_1_1SRMWQueue.html#ac995705cef1d2d8d0d485c5dc16b4ef3" title="Return whether the queue is full.">full</a>() <span class="keyword">const</span>; <a name="l00067"></a>00067 <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a class="code" href="classRaul_1_1SRMWQueue.html#a7ef2c3c9fabf10b027d54cf64207bd02" title="Push an item onto the back of the SRMWQueue - realtime-safe, not thread-safe.">push</a>(<span class="keyword">const</span> T& obj); <a name="l00068"></a>00068 <a name="l00069"></a>00069 <a name="l00070"></a>00070 <span class="comment">// Read thread:</span> <a name="l00071"></a>00071 <a name="l00072"></a>00072 <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a class="code" href="classRaul_1_1SRMWQueue.html#a37ee1c91dfe17124d25cbc36328948ea" title="Return whether the queue is empty.">empty</a>() <span class="keyword">const</span>; <a name="l00073"></a>00073 <span class="keyword">inline</span> T& <a class="code" href="classRaul_1_1SRMWQueue.html#a84df34267a3b3ffad3ea5702f0023093" title="Return the element at the front of the queue without removing it.">front</a>() <span class="keyword">const</span>; <a name="l00074"></a>00074 <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classRaul_1_1SRMWQueue.html#abed0279e75ef735e74fa476a9710f47e" title="Pop an item off the front of the queue - realtime-safe, NOT thread-safe.">pop</a>(); <a name="l00075"></a>00075 <a name="l00076"></a>00076 <span class="keyword">private</span>: <a name="l00077"></a>00077 <a name="l00078"></a>00078 <span class="comment">// Note that _front doesn't need to be an AtomicInt since it's only accessed</span> <a name="l00079"></a>00079 <span class="comment">// by the (single) reader thread</span> <a name="l00080"></a>00080 <a name="l00081"></a>00081 <span class="keywordtype">unsigned</span> _front; <a name="l00082"></a>00082 AtomicInt _back; <a name="l00083"></a>00083 AtomicInt _write_space; <a name="l00084"></a>00084 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> _size; <a name="l00085"></a>00085 <a name="l00086"></a>00086 T* <span class="keyword">const</span> _objects; <a name="l00087"></a>00087 AtomicInt* <span class="keyword">const</span> _valid; <a name="l00088"></a>00088 }; <a name="l00089"></a>00089 <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="keyword">template</span><<span class="keyword">typename</span> T> <a name="l00092"></a>00092 <a class="code" href="classRaul_1_1SRMWQueue.html" title="Realtime-safe single-reader multi-writer queue (aka lock-free ringbuffer).">SRMWQueue<T>::SRMWQueue</a>(<span class="keywordtype">size_t</span> size) <a name="l00093"></a>00093 : _front(0) <a name="l00094"></a>00094 , _back(0) <a name="l00095"></a>00095 , _write_space(size) <a name="l00096"></a>00096 , _size(size+1) <a name="l00097"></a>00097 , _objects((T*)calloc(_size, sizeof(T))) <a name="l00098"></a>00098 , _valid((AtomicInt*)calloc(_size, sizeof(AtomicInt))) <a name="l00099"></a>00099 { <a name="l00100"></a>00100 assert(log2(size) - (<span class="keywordtype">int</span>)log2(size) == 0); <a name="l00101"></a>00101 assert(size > 1); <a name="l00102"></a>00102 assert(_size-1 == (<span class="keywordtype">unsigned</span>)_write_space.get()); <a name="l00103"></a>00103 <a name="l00104"></a>00104 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i < _size; ++i) { <a name="l00105"></a>00105 assert(_valid[i].<span class="keyword">get</span>() == 0); <a name="l00106"></a>00106 } <a name="l00107"></a>00107 } <a name="l00108"></a>00108 <a name="l00109"></a>00109 <a name="l00110"></a>00110 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00111"></a>00111 SRMWQueue<T>::~SRMWQueue() <a name="l00112"></a>00112 { <a name="l00113"></a>00113 free(_objects); <a name="l00114"></a>00114 } <a name="l00115"></a>00115 <a name="l00116"></a>00116 <a name="l00121"></a>00121 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00122"></a>00122 <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a name="l00123"></a><a class="code" href="classRaul_1_1SRMWQueue.html#ac995705cef1d2d8d0d485c5dc16b4ef3">00123</a> <a class="code" href="classRaul_1_1SRMWQueue.html#ac995705cef1d2d8d0d485c5dc16b4ef3" title="Return whether the queue is full.">SRMWQueue<T>::full</a>()<span class="keyword"> const</span> <a name="l00124"></a>00124 <span class="keyword"></span>{ <a name="l00125"></a>00125 <span class="keywordflow">return</span> (_write_space.get() <= 0); <a name="l00126"></a>00126 } <a name="l00127"></a>00127 <a name="l00128"></a>00128 <a name="l00136"></a>00136 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00137"></a>00137 <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a name="l00138"></a><a class="code" href="classRaul_1_1SRMWQueue.html#a7ef2c3c9fabf10b027d54cf64207bd02">00138</a> <a class="code" href="classRaul_1_1SRMWQueue.html#a7ef2c3c9fabf10b027d54cf64207bd02" title="Push an item onto the back of the SRMWQueue - realtime-safe, not thread-safe.">SRMWQueue<T>::push</a>(<span class="keyword">const</span> T& elem) <a name="l00139"></a>00139 { <a name="l00140"></a>00140 <span class="keyword">const</span> <span class="keywordtype">int</span> old_write_space = _write_space.exchange_and_add(-1); <a name="l00141"></a>00141 <span class="keyword">const</span> <span class="keywordtype">bool</span> already_full = ( old_write_space <= 0 ); <a name="l00142"></a>00142 <a name="l00143"></a>00143 <span class="comment">/* Technically right here pop could be called in the reader thread and</span> <a name="l00144"></a>00144 <span class="comment"> * make space available, but no harm in failing anyway - this queue</span> <a name="l00145"></a>00145 <span class="comment"> * really isn't designed to be filled... */</span> <a name="l00146"></a>00146 <a name="l00147"></a>00147 <span class="keywordflow">if</span> (already_full) { <a name="l00148"></a>00148 <a name="l00149"></a>00149 <span class="comment">/* if multiple threads simultaneously get here, _write_space may be 0</span> <a name="l00150"></a>00150 <span class="comment"> * or negative. The next call to pop() will set _write_space back to</span> <a name="l00151"></a>00151 <span class="comment"> * a sane value. Note that _write_space is not exposed, so this is okay</span> <a name="l00152"></a>00152 <span class="comment"> * (... assuming this code is correct) */</span> <a name="l00153"></a>00153 <a name="l00154"></a>00154 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00155"></a>00155 <a name="l00156"></a>00156 } <span class="keywordflow">else</span> { <a name="l00157"></a>00157 <a name="l00158"></a>00158 <span class="comment">// Note: _size must be a power of 2 for this to not explode when _back overflows</span> <a name="l00159"></a>00159 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> write_index = (unsigned)_back.exchange_and_add(1) % _size; <a name="l00160"></a>00160 <a name="l00161"></a>00161 assert(_valid[write_index] == 0); <a name="l00162"></a>00162 _objects[write_index] = elem; <a name="l00163"></a>00163 ++(_valid[write_index]); <a name="l00164"></a>00164 <a name="l00165"></a>00165 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00166"></a>00166 <a name="l00167"></a>00167 } <a name="l00168"></a>00168 } <a name="l00169"></a>00169 <a name="l00170"></a>00170 <a name="l00175"></a>00175 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00176"></a>00176 <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a name="l00177"></a><a class="code" href="classRaul_1_1SRMWQueue.html#a37ee1c91dfe17124d25cbc36328948ea">00177</a> <a class="code" href="classRaul_1_1SRMWQueue.html#a37ee1c91dfe17124d25cbc36328948ea" title="Return whether the queue is empty.">SRMWQueue<T>::empty</a>()<span class="keyword"> const</span> <a name="l00178"></a>00178 <span class="keyword"></span>{ <a name="l00179"></a>00179 <span class="keywordflow">return</span> (_valid[_front].<span class="keyword">get</span>() == 0); <a name="l00180"></a>00180 } <a name="l00181"></a>00181 <a name="l00182"></a>00182 <a name="l00188"></a>00188 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00189"></a>00189 <span class="keyword">inline</span> T& <a name="l00190"></a><a class="code" href="classRaul_1_1SRMWQueue.html#a84df34267a3b3ffad3ea5702f0023093">00190</a> <a class="code" href="classRaul_1_1SRMWQueue.html#a84df34267a3b3ffad3ea5702f0023093" title="Return the element at the front of the queue without removing it.">SRMWQueue<T>::front</a>()<span class="keyword"> const</span> <a name="l00191"></a>00191 <span class="keyword"></span>{ <a name="l00192"></a>00192 <span class="keywordflow">return</span> _objects[_front]; <a name="l00193"></a>00193 } <a name="l00194"></a>00194 <a name="l00195"></a>00195 <a name="l00203"></a>00203 <span class="keyword">template</span> <<span class="keyword">typename</span> T> <a name="l00204"></a>00204 <span class="keyword">inline</span> <span class="keywordtype">void</span> <a name="l00205"></a><a class="code" href="classRaul_1_1SRMWQueue.html#abed0279e75ef735e74fa476a9710f47e">00205</a> <a class="code" href="classRaul_1_1SRMWQueue.html#abed0279e75ef735e74fa476a9710f47e" title="Pop an item off the front of the queue - realtime-safe, NOT thread-safe.">SRMWQueue<T>::pop</a>() <a name="l00206"></a>00206 { <a name="l00207"></a>00207 assert(_valid[_front] == 1); <a name="l00208"></a>00208 --(_valid[_front]); <a name="l00209"></a>00209 <a name="l00210"></a>00210 _front = (_front + 1) % (_size); <a name="l00211"></a>00211 <a name="l00212"></a>00212 <span class="keywordflow">if</span> (_write_space.get() < 0) <a name="l00213"></a>00213 _write_space = 1; <a name="l00214"></a>00214 <span class="keywordflow">else</span> <a name="l00215"></a>00215 ++_write_space; <a name="l00216"></a>00216 } <a name="l00217"></a>00217 <a name="l00218"></a>00218 <a name="l00219"></a>00219 } <span class="comment">// namespace Raul</span> <a name="l00220"></a>00220 <a name="l00221"></a>00221 <span class="preprocessor">#endif // RAUL_SRMW_QUEUE_HPP</span> </pre></div></div> </div> <hr class="footer"/><address class="footer"><small>Generated on Wed Oct 6 2010 for RAUL by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.1 </small></address> </body> </html>