Files
spark/src/lib/utils/ConcurrentAppendCollection.h
T

138 lines
4.2 KiB
C++

#pragma once
#include "bts_assert.h"
#include <atomic>
#include <list>
#include <mutex>
#include <thread>
#include <vector>
namespace bts {
// Build to replace tbb::concurrent_vector for
// md::QueryServer and MetricsContainer
template <class T, uint32_t CHUNK> class ConcurrentAppendCollectionIterator;
template <class T, uint32_t CHUNK = 1024> class ConcurrentAppendCollection {
friend class ConcurrentAppendCollectionIterator<T, CHUNK>;
std::vector<std::unique_ptr<std::array<T, CHUNK>>> _data;
std::atomic<size_t> _end;
std::mutex _mutex;
public:
ConcurrentAppendCollection() : _data(), _end(0) { add_chunk(); }
void push_back(const T &item) {
std::lock_guard<std::mutex> 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<T, CHUNK> begin() const {
return ConcurrentAppendCollectionIterator<T, CHUNK>(*this, 0);
}
ConcurrentAppendCollectionIterator<T, CHUNK> end() const {
return ConcurrentAppendCollectionIterator<T, CHUNK>(*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<std::array<T, CHUNK>>());
}
};
template <class T, uint32_t CHUNK>
// forward iterator
class ConcurrentAppendCollectionIterator {
const ConcurrentAppendCollection<T, CHUNK> &_owner;
size_t _pos;
mutable T const *_cache;
public:
typedef std::bidirectional_iterator_tag iterator_category;
ConcurrentAppendCollectionIterator(
const ConcurrentAppendCollection<T, CHUNK> &owner, size_t pos)
: _owner(owner), _pos(pos), _cache(nullptr) {}
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 <class A, uint32_t B>
friend bool operator==(
ConcurrentAppendCollectionIterator<A, B> &left,
ConcurrentAppendCollectionIterator<A, B> &right);
template <class A, uint32_t B>
friend bool operator!=(
ConcurrentAppendCollectionIterator<A, B> &left,
ConcurrentAppendCollectionIterator<A, B> &right);
};
template <class T, uint32_t CHUNK>
void swap(
ConcurrentAppendCollectionIterator<T, CHUNK> &left,
ConcurrentAppendCollectionIterator<T, CHUNK> &right) {
ConcurrentAppendCollection temp = left;
left = right;
right = temp;
}
template <class T, uint32_t CHUNK>
bool operator==(
ConcurrentAppendCollectionIterator<T, CHUNK> &left,
ConcurrentAppendCollectionIterator<T, CHUNK> &right) {
return &left._owner == &right._owner && left._pos == right._pos;
}
template <class T, uint32_t CHUNK>
bool operator!=(
ConcurrentAppendCollectionIterator<T, CHUNK> &left,
ConcurrentAppendCollectionIterator<T, CHUNK> &right) {
return &left._owner != &right._owner || left._pos != right._pos;
}
} // namespace bts