18 #ifndef RAUL_SRMW_QUEUE_HPP
19 #define RAUL_SRMW_QUEUE_HPP
25 #include <boost/utility.hpp>
27 #include "raul/AtomicInt.hpp"
63 inline size_t capacity()
const {
return _size-1; }
68 inline bool full()
const;
69 inline bool push(
const T& obj);
74 inline bool empty()
const;
75 inline T&
front()
const;
99 , _objects((T*)calloc(_size, sizeof(T)))
102 assert(log2(size) - (
int)log2(size) == 0);
104 assert(_size-1 == (
unsigned)_write_space.get());
106 for (
unsigned i=0; i < _size; ++i) {
107 assert(_valid[i].get() == 0);
112 template <
typename T>
113 SRMWQueue<T>::~SRMWQueue()
123 template <
typename T>
127 return (_write_space.get() <= 0);
138 template <
typename T>
142 const int old_write_space = _write_space.exchange_and_add(-1);
143 const bool already_full = ( old_write_space <= 0 );
161 const unsigned write_index = (unsigned)_back.exchange_and_add(1) % _size;
163 assert(_valid[write_index] == 0);
164 _objects[write_index] = elem;
165 ++(_valid[write_index]);
177 template <
typename T>
181 return (_valid[_front].get() == 0);
190 template <
typename T>
194 return _objects[_front];
205 template <
typename T>
209 assert(_valid[_front] == 1);
212 _front = (_front + 1) % (_size);
214 if (_write_space.get() < 0)
Atomic integer.
Definition: AtomicInt.hpp:29
Realtime-safe single-reader multi-writer queue (aka lock-free ringbuffer)
Definition: SRMWQueue.hpp:55
bool full() const
Return whether the queue is full.
Definition: SRMWQueue.hpp:125
bool push(const T &obj)
Push an item onto the back of the SRMWQueue - realtime-safe, not thread-safe.
Definition: SRMWQueue.hpp:140
void pop()
Pop an item off the front of the queue - realtime-safe, NOT thread-safe.
Definition: SRMWQueue.hpp:207
bool empty() const
Return whether the queue is empty.
Definition: SRMWQueue.hpp:179
T & front() const
Return the element at the front of the queue without removing it.
Definition: SRMWQueue.hpp:192