-
Notifications
You must be signed in to change notification settings - Fork 43
/
view_buffer.h
267 lines (221 loc) · 8.03 KB
/
view_buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//==============================================================
// Copyright (C) Intel Corporation
//
// SPDX-License-Identifier: MIT
// =============================================================
// LICENSE HERE
#ifndef SRC_VIEW_BUFFER_H_
#define SRC_VIEW_BUFFER_H_
#include <assert.h>
#include <array>
#include <condition_variable>
#include <cstdint>
#include <cstring>
#include <functional>
#include <mutex>
#include <optional>
#include <queue>
#include <type_traits>
#include <unordered_map>
#include <utility>
namespace pti {
namespace view {
namespace utilities {
template <typename U = unsigned char>
struct ViewRecordBuffer {
public:
using UnderlyingType = U;
using SizeType = std::size_t;
ViewRecordBuffer() = default;
constexpr explicit ViewRecordBuffer(UnderlyingType* buffer, SizeType size,
SizeType start_position)
: buf_(buffer), size_(size), pos_(start_position) {}
ViewRecordBuffer(const ViewRecordBuffer&) = delete;
ViewRecordBuffer& operator=(const ViewRecordBuffer&) = delete;
ViewRecordBuffer(ViewRecordBuffer&& other)
: buf_(std::exchange(other.buf_, nullptr)),
size_(std::exchange(other.size_, 0)),
pos_(std::exchange(other.pos_, 0)) {}
ViewRecordBuffer& operator=(ViewRecordBuffer&& other) noexcept {
if (this != &other) {
std::swap(other.buf_, buf_); // keep ptr to data around
size_ = std::exchange(other.size_, 0);
pos_ = std::exchange(other.pos_, 0);
}
return *this;
}
virtual ~ViewRecordBuffer() = default;
inline void Refresh(UnderlyingType* buffer, SizeType size) {
buf_ = buffer;
pos_ = 0;
size_ = size;
}
// Return the buffer pointer to the inserted record
template <typename T>
inline UnderlyingType* Insert(const T& view_record) {
static_assert(std::is_trivially_copyable<T>::value,
"One can only insert trivially copyable types into the "
"ViewBuffer (view records)");
assert(!IsNull());
assert(FreeBytes() >= sizeof(T));
auto* inserted_record = buf_ + pos_;
std::memcpy(inserted_record, &view_record, sizeof(T));
pos_ += sizeof(T);
return inserted_record;
}
template <typename T>
inline T* Peek() const {
return Peek<T>(nullptr, 0);
}
template <typename T>
inline T* Peek(T* element) const {
return Peek<T>(element, 0);
}
template <typename T>
inline T* Peek(SizeType loc) const {
return Peek<T>(nullptr, loc);
}
/**
* @brief Peek inside the buffer and view the next element
*
* @param element (optional) pointer to current element IN the buffer. If
* nullptr or unspecified, starts at beginning of buffer
* @param loc (optional) location of next element. If unspecified or 0,
* return current element in buffer.
* @return a pointer to an element inside the buffer, nullptr at end of buffer
*/
template <typename T>
inline T* Peek(T* element, SizeType loc) const {
auto* buffer_window = element ? reinterpret_cast<UnderlyingType*>(element) : buf_;
auto* peek_element = buffer_window + loc;
if (peek_element == GetRecordsEnd()) {
peek_element = nullptr;
}
return reinterpret_cast<T*>(peek_element);
}
inline bool IsNull() const { return (!buf_ || !size_); }
constexpr UnderlyingType* GetBuffer() { return buf_; }
constexpr UnderlyingType* GetBuffer() const { return buf_; }
constexpr SizeType GetBufferSize() const { return size_; }
constexpr UnderlyingType* GetBufferEnd() { return buf_ + size_; }
constexpr SizeType GetValidBytes() const { return pos_; }
constexpr UnderlyingType* GetRecordsEnd() { return buf_ + pos_; }
constexpr UnderlyingType* GetRecordsEnd() const { return buf_ + pos_; }
constexpr SizeType FreeBytes() const {
assert(size_ - pos_ >= 0);
return size_ - pos_;
}
template <typename T>
constexpr bool BufferFull() const {
static_assert(std::is_trivially_copyable<T>::value,
"One can only insert trivially copyable types into the "
"ViewBuffer (view records)");
return sizeof(T) >= FreeBytes();
}
friend void Swap(ViewRecordBuffer& lhs, ViewRecordBuffer& rhs) { std::swap(lhs, rhs); }
private:
UnderlyingType* buf_ = nullptr;
SizeType size_ = 0;
SizeType pos_ = 0;
};
template <typename T>
struct ViewRecordBufferQueue {
public:
ViewRecordBufferQueue() = default;
explicit ViewRecordBufferQueue(std::size_t depth) : buffer_depth_(depth) {}
ViewRecordBufferQueue& operator=(const ViewRecordBufferQueue&) = delete;
ViewRecordBufferQueue& operator=(ViewRecordBufferQueue&& other) = delete;
ViewRecordBufferQueue(const ViewRecordBufferQueue&) = delete;
ViewRecordBufferQueue(ViewRecordBufferQueue&& other) = delete;
inline void Push(T&& buffer) {
std::unique_lock<std::mutex> buffer_lock(buffer_queue_mtx_);
if (buffer_depth_.has_value()) {
buffer_available_.wait(buffer_lock, [this] { return buffer_queue_.size() < buffer_depth_; });
}
buffer_queue_.push(std::move(buffer));
buffer_lock.unlock();
buffer_available_.notify_one();
}
inline T Pop() {
std::unique_lock<std::mutex> buffer_lock(buffer_queue_mtx_);
buffer_available_.wait(buffer_lock, [this] { return !buffer_queue_.empty(); });
auto buffer = std::move(buffer_queue_.front());
buffer_queue_.pop();
buffer_lock.unlock();
buffer_available_.notify_all();
return buffer;
}
template <typename Condition>
inline void WaitUntilEmptyOr(const Condition& cond) {
std::unique_lock<std::mutex> buffer_lock(buffer_queue_mtx_);
buffer_available_.wait(buffer_lock, [this, &cond] { return buffer_queue_.empty() || cond; });
}
inline std::size_t Size() {
std::lock_guard<std::mutex> buffer_lock(buffer_queue_mtx_);
return std::size(buffer_queue_);
}
inline void ResetBufferDepth() {
std::lock_guard<std::mutex> buffer_lock(buffer_queue_mtx_);
buffer_depth_.reset();
}
inline void SetBufferDepth(std::size_t depth) {
std::lock_guard<std::mutex> buffer_lock(buffer_queue_mtx_);
buffer_depth_ = depth;
}
virtual ~ViewRecordBufferQueue() = default;
private:
std::queue<T> buffer_queue_;
mutable std::mutex buffer_queue_mtx_;
std::condition_variable buffer_available_;
std::optional<std::size_t> buffer_depth_;
};
// This is not a perfect abstraction.
// User beware. TODO(matthew.schilling@intel.com): Boost? Make more robust?
template <typename KeyT, typename ValueT>
struct ThreadSafeHashTable {
public:
ThreadSafeHashTable() = default;
ThreadSafeHashTable& operator=(const ThreadSafeHashTable&) = delete;
ThreadSafeHashTable& operator=(ThreadSafeHashTable&& other) = delete;
ThreadSafeHashTable(const ThreadSafeHashTable&) = delete;
ThreadSafeHashTable(ThreadSafeHashTable&& other) = delete;
virtual ~ThreadSafeHashTable() = default;
inline auto& operator[](const KeyT& key) {
std::lock_guard<std::mutex> lock_table(hash_table_mtx_);
return hash_table_[key];
}
inline void Erase(const KeyT& key) {
std::lock_guard<std::mutex> lock_table(hash_table_mtx_);
hash_table_.erase(key);
}
bool Empty() const {
std::lock_guard<std::mutex> lock_table(hash_table_mtx_);
return hash_table_.empty();
}
inline ValueT* TryFindElement(const KeyT& key) {
std::lock_guard<std::mutex> lock_table(hash_table_mtx_);
if (hash_table_.find(key) != hash_table_.end()) {
return &hash_table_[key];
}
return nullptr;
}
template <typename Callable>
inline void ForEach(Callable&& user_function) {
std::lock_guard<std::mutex> lock_table(hash_table_mtx_);
auto user_callable = std::forward<Callable>(user_function);
for (auto&& [key, value] : hash_table_) {
user_callable(key, value);
}
}
private:
std::unordered_map<KeyT, ValueT> hash_table_;
mutable std::mutex hash_table_mtx_;
};
using ViewBuffer = ViewRecordBuffer<unsigned char>;
using ViewBufferQueue = ViewRecordBufferQueue<ViewBuffer>;
template <typename KeyT>
using ViewBufferTable = ThreadSafeHashTable<KeyT, ViewBuffer>;
} // namespace utilities
} // namespace view
} // namespace pti
#endif // SRC_VIEW_BUFFER_H_