#pragma once #include "bts_assert.h" #include #include #include #include #include namespace bts { // Build to replace tbb::concurrent_vector for // md::QueryServer and MetricsContainer template class ConcurrentAppendCollectionIterator; template class ConcurrentAppendCollection { friend class ConcurrentAppendCollectionIterator; std::vector>> _data; std::atomic _end; std::mutex _mutex; public: ConcurrentAppendCollection() : _data(), _end(0) { add_chunk(); } void push_back(const T &item) { std::lock_guard lock(_mutex); auto end = _end.load(std::memory_order_acquire); auto c = chunk(end); if (c == _data.size()) { add_chunk(); } (*_data[c])[offset(end)] = item; _end += 1; } const T &operator[](uint32_t index) const { return (*_data[chunk(index)])[offset(index)]; } ConcurrentAppendCollectionIterator begin() const { return ConcurrentAppendCollectionIterator(*this, 0); } ConcurrentAppendCollectionIterator end() const { return ConcurrentAppendCollectionIterator(*this, _end); } size_t size() const { return _end; } private: size_t chunk(size_t i) const { return i / CHUNK; } size_t offset(size_t i) const { return i % CHUNK; } void add_chunk() { _data.push_back(std::make_unique>()); } }; template // forward iterator class ConcurrentAppendCollectionIterator { const ConcurrentAppendCollection &_owner; size_t _pos; mutable T const *_cache; public: typedef std::bidirectional_iterator_tag iterator_category; ConcurrentAppendCollectionIterator( const ConcurrentAppendCollection &owner, size_t pos) : _owner(owner), _pos(pos), _cache(nullptr) {} ConcurrentAppendCollectionIterator(const ConcurrentAppendCollectionIterator &other) = default; ConcurrentAppendCollectionIterator & operator=(const ConcurrentAppendCollectionIterator &other) { _owner = other._owner; _pos = other._pos; return *this; } ConcurrentAppendCollectionIterator &operator++() { ++_pos; if (_cache) { if (_pos % CHUNK == 0) { // If the position passes the chunk boundary we invalidate the // pointer _cache = nullptr; } else { _cache++; } } // If we ran off the end it's ok because we will be equal to end return *this; } ConcurrentAppendCollectionIterator operator++(int) { return ++(*this); } ConcurrentAppendCollectionIterator &operator--() { --_pos; _cache = nullptr; return *this; } ConcurrentAppendCollectionIterator operator--(int) { return --(*this); } const T &operator*() const { if (_cache) { return *_cache; } else { _cache = &(_owner[_pos]); return *_cache; } } const T *operator->() const { return &(**this); } template friend bool operator==( ConcurrentAppendCollectionIterator &left, ConcurrentAppendCollectionIterator &right); template friend bool operator!=( ConcurrentAppendCollectionIterator &left, ConcurrentAppendCollectionIterator &right); }; template void swap( ConcurrentAppendCollectionIterator &left, ConcurrentAppendCollectionIterator &right) { ConcurrentAppendCollection temp = left; left = right; right = temp; } template bool operator==( ConcurrentAppendCollectionIterator &left, ConcurrentAppendCollectionIterator &right) { return &left._owner == &right._owner && left._pos == right._pos; } template bool operator!=( ConcurrentAppendCollectionIterator &left, ConcurrentAppendCollectionIterator &right) { return &left._owner != &right._owner || left._pos != right._pos; } } // namespace bts