Files
spark/src/lib/utils/GrowableSPSCQueue.h
T
Pedro Pinto 7ab6fd273e Added buffering to mercury queues.
We were dropping events on Dan's system when the queue filled up.
2018-10-16 15:32:54 +00:00

51 lines
1.3 KiB
C++

#pragma once
#include <boost/lockfree/spsc_queue.hpp>
#include <queue>
namespace bts {
namespace utils {
// Wraps a bounded single producer, single consumer to provide extra
// dynamically allocated storage when the wrapped queue is full. The
// assumption is that filling the queue is a very rare event for which
// we can afford some performance penalty. Producers must periodically
// call drain() to ensure that events bufffered while the underlying
// queue was full are eventually put back in the underlying queue
template <class T> class GrowableSPSCQueue {
public:
GrowableSPSCQueue(size_t internal_size)
: _queue(internal_size), _overflow_buffer() {}
// producer
void drain() {
while (_overflow_buffer.size()) {
auto const &t = _overflow_buffer.front();
if (_queue.push(t))
_overflow_buffer.pop();
else
return;
}
}
void push(const T &t) {
if (_overflow_buffer.size() || !_queue.push(t))
_overflow_buffer.push(t);
}
// consumer
bool pop(T &t) { return _queue.pop(t); }
template <class C>
void consume_all(C const&c) {
_queue.consume_all(c);
}
private:
boost::lockfree::spsc_queue<T> _queue;
std::queue<T> _overflow_buffer;
};
} // namespace utils
} // namespace bts