#pragma once #include "lib/utils/bts_no_copy.h" #include #include #include #include #include namespace bts { namespace utils { /* * WaitFreeQueue defines a circular buffer in the given space (which may be shared memory and should probably be at least 1mb) * There may only be *one* writer thread which can write without *any* back pressure * There may be any number of readers which must poll to read new values. * If the reader does not poll quickly enough it can be overrun by the writer in which case it does not see * the lost values. In this case the overrun is detected and the read cursor re-syncronizes with the queue * and returns the full current state along with the reset flag. * This class is optimized first for never blocking the writer. Latency and throughput are seconday but will probably be pretty good. */ template class WaitFreeQueue { typedef int64_t position; const static position invalid_id = -1; // Store up to 4 per cache line struct ALIGNED_(16) Entry { Entry() : id(invalid_id), value() {} std::atomic id; T value; Entry& operator=(const Entry& other) { set(other.value, other.id.load(std::memory_order_acquire)); return *this; } void set(const T& other, position new_id) { id.store(invalid_id, std::memory_order_release); value = other; id.store(new_id, std::memory_order_release); } bool try_read(T& result, position& out_id) const { out_id = invalid_id; auto curid = id.load(std::memory_order_acquire); if (curid == invalid_id) return false; result = value; out_id = curid; // The read is only valid if the id is unchanged return (curid == id.load(std::memory_order_acquire)); } }; public: WaitFreeQueue(void* buffer, size_t size_bytes, bool writer=false) : _pos(0), _size(size_bytes / sizeof(Entry)), _writer(writer), _buffer((Entry*)buffer) { // Initialize the buffer using the entry constructor if (writer) { memset(buffer,0,size_bytes); for (size_t x = 0; x < _size; ++x) { _buffer[x] = *( new (_buffer+x) Entry() ); } } } ~WaitFreeQueue() { // Mark the whole buffer as invalid if (_writer) { for (size_t x = 0; x < _size; ++x) { _buffer[x].id = invalid_id; } } } void push(const T& t) { entry(_pos).set(t, _pos); ++_pos; } class Cursor { const WaitFreeQueue& _owner; position _pos; public: Cursor(const WaitFreeQueue& owner) : _owner(owner), _pos(0) {} Cursor(const WaitFreeQueue& owner, position pos) : _owner(owner), _pos(pos) {} public: size_t read(std::vector& result, bool* p_overrun=nullptr) { T temp; size_t count = 0; bool overrun; if (p_overrun) { *p_overrun = false; } while(pop(temp, overrun)) { result.push_back(temp); ++count; } if (overrun) { // Attempt to re-sync with the queue result.clear(); _pos = invalid_id; _pos = _owner.entry(0).id.load(); if (_pos == invalid_id) { _pos = 0; } // If we attempted to jump to the head somehow if (p_overrun) { *p_overrun = true; } return read(result); } return count; } size_t read(google::protobuf::RepeatedPtrField& result, bool* p_overrun = nullptr) { T temp; size_t count = 0; bool overrun; if (p_overrun) { *p_overrun = false; } while(pop(temp, overrun)) { result.Add()->CopyFrom(temp); ++count; } if (overrun) { // Attempt to re-sync with the queue result.Clear(); _pos = invalid_id; _pos = _owner.entry(0).id.load(); if (_pos == invalid_id) { _pos = 0; } // If we attempted to jump to the head somehow if (p_overrun) { *p_overrun = true; } return read(result); } return count; } // Attempt to read the next value and detect writer overrun bool pop(T& result, bool& overrun) { overrun = false; const Entry& entry = _owner.entry(_pos); // If the next entry is what we expect we can read it position id; if (entry.try_read(result, id)) { if (id == _pos) { _pos++; return true; } if (id > _pos) { overrun = true; } } return false; } position current_position() { return _pos; } }; const Cursor get_cursor() const { return Cursor(*this); } private: position _pos; size_t _size; bool _writer; Entry* _buffer; Entry& entry(position index) { return _buffer[index % _size]; } const Entry& entry(position index) const { return _buffer[index % _size]; } BTS_NO_COPY(WaitFreeQueue); }; }}//namespace