/builddir/build/BUILD/raul-0.4.0/raul/SRSWQueue.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
00003  * 
00004  * Raul is free software; you can redistribute it and/or modify it under the
00005  * terms of the GNU General Public License as published by the Free Software
00006  * Foundation; either version 2 of the License, or (at your option) any later
00007  * version.
00008  * 
00009  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
00010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
00012  * 
00013  * You should have received a copy of the GNU General Public License along
00014  * with this program; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00016  */
00017 
00018 #ifndef RAUL_SRSW_QUEUE_HPP
00019 #define RAUL_SRSW_QUEUE_HPP
00020 
00021 #include <cassert>
00022 #include <cstdlib>
00023 #include <boost/utility.hpp>
00024 #include <raul/AtomicInt.hpp>
00025 
00026 namespace Raul {
00027 
00028 
00038 template <typename T>
00039 class SRSWQueue : boost::noncopyable
00040 {
00041 public:
00042     SRSWQueue(size_t size);
00043     ~SRSWQueue();
00044     
00045     // Any thread:
00046     
00047     inline size_t capacity() const { return _size-1; }
00048 
00049     
00050     // Write thread(s):
00051 
00052     inline bool full() const;
00053     inline bool push(const T& obj);
00054     
00055 
00056     // Read thread:
00057     
00058     inline bool empty() const;
00059     inline T&   front() const;
00060     inline void pop();
00061     
00062 private:
00063     volatile size_t _front;   
00064     volatile size_t _back;    
00065     const    size_t _size;    
00066     T* const        _objects; 
00067 };
00068 
00069 
00070 template<typename T>
00071 SRSWQueue<T>::SRSWQueue(size_t size)
00072 : _front(0),
00073   _back(0),
00074   _size(size+1),
00075   _objects((T*)calloc(_size, sizeof(T)))
00076 {
00077     assert(size > 1);
00078 }
00079 
00080 
00081 template <typename T>
00082 SRSWQueue<T>::~SRSWQueue()
00083 {
00084     free(_objects);
00085 }
00086 
00087 
00090 template <typename T>
00091 inline bool
00092 SRSWQueue<T>::empty() const
00093 {
00094     return (_back == _front);
00095 }
00096 
00097 
00100 template <typename T>
00101 inline bool
00102 SRSWQueue<T>::full() const
00103 {
00104     // FIXME: uses both _front and _back - thread safe?
00105     return ( ((_front - _back + _size) % _size) == 1 );
00106 }
00107 
00108 
00111 template <typename T>
00112 inline T&
00113 SRSWQueue<T>::front() const
00114 {
00115     return _objects[_front];
00116 }
00117 
00118 
00124 template <typename T>
00125 inline bool
00126 SRSWQueue<T>::push(const T& elem)
00127 {
00128     if (full()) {
00129         return false;
00130     } else {
00131         _objects[_back] = elem;
00132         _back = (_back + 1) % (_size);
00133         return true;
00134     }
00135 }
00136 
00137 
00144 template <typename T>
00145 inline void
00146 SRSWQueue<T>::pop()
00147 {
00148     assert(!empty());
00149     assert(_size > 0);
00150 
00151     _front = (_front + 1) % (_size);
00152 }
00153 
00154 
00155 } // namespace Raul
00156 
00157 #endif // RAUL_SRSW_QUEUE_HPP

Generated on Wed Apr 9 08:14:41 2008 for RAUL by  doxygen 1.5.1