/usr/include/raul/SRSWQueue.hpp is in libraul-dev 0.8.0+dfsg0-0.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | /* This file is part of Raul.
* Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
*
* Raul is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* Raul is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef RAUL_SRSW_QUEUE_HPP
#define RAUL_SRSW_QUEUE_HPP
#include <cassert>
#include <boost/utility.hpp>
#include "raul/AtomicInt.hpp"
namespace Raul {
/** Realtime-safe single-reader single-writer queue (aka lock-free ringbuffer)
*
* This is appropriate for a cross-thread queue of fixed size object. If you
* need to do variable sized reads and writes, use Raul::RingBuffer instead.
*
* Implemented as a dequeue in a fixed array. This is read/write thread-safe,
* pushing and popping may occur simultaneously by seperate threads, but
* the push and pop operations themselves are not thread-safe (ie. there can
* be at most 1 read and at most 1 writer thread).
*
* \ingroup raul
*/
template <typename T>
class SRSWQueue : boost::noncopyable
{
public:
/** @param size Size in number of elements */
explicit SRSWQueue(size_t size);
~SRSWQueue();
// Any thread:
inline size_t capacity() const { return _size-1; }
// Write thread(s):
inline bool full() const;
inline bool push(const T& obj);
// Read thread:
inline bool empty() const;
inline T& front() const;
inline void pop();
private:
AtomicInt _front; ///< Index to front of queue (circular)
AtomicInt _back; ///< Index to back of queue (one past last element) (circular)
const size_t _size; ///< Size of @ref _objects (you can store _size-1 objects)
T* const _objects; ///< Fixed array containing queued elements
};
template<typename T>
SRSWQueue<T>::SRSWQueue(size_t size)
: _front(0)
, _back(0)
, _size(size + 1)
, _objects(new T[_size])
{
assert(size > 1);
}
template <typename T>
SRSWQueue<T>::~SRSWQueue()
{
delete[] _objects;
}
/** Return whether or not the queue is empty.
*/
template <typename T>
inline bool
SRSWQueue<T>::empty() const
{
return (_back.get() == _front.get());
}
/** Return whether or not the queue is full.
*/
template <typename T>
inline bool
SRSWQueue<T>::full() const
{
return (((_front.get() - _back.get() + _size) % _size) == 1);
}
/** Return the element at the front of the queue without removing it
*/
template <typename T>
inline T&
SRSWQueue<T>::front() const
{
return _objects[_front.get()];
}
/** Push an item onto the back of the SRSWQueue - realtime-safe, not thread-safe.
*
* @returns true if @a elem was successfully pushed onto the queue,
* false otherwise (queue is full).
*/
template <typename T>
inline bool
SRSWQueue<T>::push(const T& elem)
{
if (full()) {
return false;
} else {
unsigned back = _back.get();
_objects[back] = elem;
_back = (back + 1) % _size;
return true;
}
}
/** Pop an item off the front of the queue - realtime-safe, not thread-safe.
*
* It is a fatal error to call pop() when the queue is empty.
*
* @returns the element popped.
*/
template <typename T>
inline void
SRSWQueue<T>::pop()
{
assert(!empty());
assert(_size > 0);
_front = (_front.get() + 1) % (_size);
}
} // namespace Raul
#endif // RAUL_SRSW_QUEUE_HPP
|