18 #ifndef RAUL_SRMW_QUEUE_HPP
19 #define RAUL_SRMW_QUEUE_HPP
24 #include <boost/utility.hpp>
25 #include <raul/AtomicInt.hpp>
64 inline size_t capacity()
const {
return _size-1; }
69 inline bool full()
const;
70 inline bool push(
const T& obj);
75 inline bool empty()
const;
76 inline T& front()
const;
86 AtomicInt _write_space;
90 AtomicInt*
const _valid;
100 , _objects((T*)calloc(_size, sizeof(T)))
101 , _valid((AtomicInt*)calloc(_size, sizeof(AtomicInt)))
103 assert(log2(size) - (
int)log2(size) == 0);
105 assert(_size-1 == (
unsigned)_write_space.get());
107 for (
unsigned i=0; i < _size; ++i) {
108 assert(_valid[i].
get() == 0);
113 template <
typename T>
114 SRMWQueue<T>::~SRMWQueue()
124 template <
typename T>
128 return (_write_space.get() <= 0);
139 template <
typename T>
143 const int old_write_space = _write_space.exchange_and_add(-1);
144 const bool already_full = ( old_write_space <= 0 );
162 const unsigned write_index = (unsigned)_back.exchange_and_add(1) % _size;
164 assert(_valid[write_index] == 0);
165 _objects[write_index] = elem;
166 ++(_valid[write_index]);
178 template <
typename T>
182 return (_valid[_front].
get() == 0);
191 template <
typename T>
195 return _objects[_front];
206 template <
typename T>
210 assert(_valid[_front] == 1);
213 _front = (_front + 1) % (_size);
215 if (_write_space.get() < 0)
224 #endif // RAUL_SRMW_QUEUE_HPP
Realtime-safe single-reader multi-writer queue (aka lock-free ringbuffer)
Definition: SRMWQueue.hpp:55
bool push(const T &obj)
Push an item onto the back of the SRMWQueue - realtime-safe, not thread-safe.
Definition: SRMWQueue.hpp:141
bool empty() const
Return whether the queue is empty.
Definition: SRMWQueue.hpp:180
void pop()
Pop an item off the front of the queue - realtime-safe, NOT thread-safe.
Definition: SRMWQueue.hpp:208
T & front() const
Return the element at the front of the queue without removing it.
Definition: SRMWQueue.hpp:193
bool full() const
Return whether the queue is full.
Definition: SRMWQueue.hpp:126