-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitchbuffer_impl.h
369 lines (307 loc) · 9.44 KB
/
switchbuffer_impl.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#ifndef SWITCHBUFFER_IMPL_H
#define SWITCHBUFFER_IMPL_H
#ifndef SWITCHBUFFER_H
# error Include this file via switchbuffer.h only
#endif
#include <algorithm>
#include <cassert>
#include <iterator>
#include <mutex>
#if __cplusplus >= 201703L
# include <optional>
#endif // __cplusplus >= 201703L
#include <vector>
namespace detail
{
#if __cplusplus >= 201703L
using std::optional;
#else
template<typename T>
struct optional
{
template<typename... Args>
T &emplace(Args&&... args)
{
m_ptr.reset(new T(std::forward<Args>(args)...));
return *m_ptr;
}
void reset() noexcept
{
m_ptr.reset();
}
T *operator->()
{
return m_ptr.get();
}
operator bool() const noexcept
{
return !!m_ptr;
}
private:
std::unique_ptr<T> m_ptr;
};
#endif // __cplusplus >= 201703L
template<typename Buffer>
struct SwitchBufferImpl
{
using Ring = std::vector<std::unique_ptr<Buffer>>;
class RingIterator
: public std::iterator<std::input_iterator_tag, typename Ring::value_type>
{
public:
RingIterator(Ring *ring)
: m_ring(ring)
, m_pos(ring->size()) // initialize with invalid position index
{}
RingIterator &operator++()
{
++m_pos;
m_pos %= m_ring->size();
return *this;
}
typename Ring::reference operator*()
{
assert(*this);
return (*m_ring)[m_pos];
}
bool operator==(RingIterator const &other) const
{
assert(m_ring == other.m_ring);
return (m_pos == other.m_pos);
}
// check for valid position index
operator bool() const
{
return (m_pos < m_ring->size());
}
private:
Ring *m_ring;
typename Ring::size_type m_pos;
};
struct Producer
{
RingIterator curr; // points to the most recently produced buffer, initialized to invalid
RingIterator next; // points to the in-production buffer, initialized to invalid
RingIterator olde; // points to the oldest produced buffer, initialized to invalid
bool isClosed; // flag whether producer has shut down
Producer(Ring *ring)
: curr(ring)
, next(ring)
, olde(ring)
, isClosed(false)
{}
};
struct Consumer
{
SwitchBufferConsumer<Buffer> const *parent; // parent address used as ID
RingIterator pos; // points to the in-consumption buffer, initialized to invalid
bool isFull; // flag whether ring is full of consumable slots
bool isEmpty; // flag whether ring is empty of consumable slots
std::unique_ptr<Buffer> sanctuary; // storage to save in-consumption buffer before being overwritten
optional<std::promise<Buffer const &>> promise; // promise to fulfill after empty ring
Consumer(SwitchBufferConsumer<Buffer> const *parent, Ring *ring)
: parent(parent)
, pos(ring)
, isFull(false)
, isEmpty(true)
, sanctuary(new Buffer)
{}
Consumer(const Consumer &other) = delete;
Consumer(Consumer &&other) noexcept = default;
~Consumer() = default;
Consumer &operator=(const Consumer &other) = delete;
Consumer &operator=(Consumer &&other) noexcept = default;
// for use with std::find
bool operator==(SwitchBufferConsumer<Buffer> const *otherParent) const
{
return (parent == otherParent);
}
};
using Consumers = std::vector<Consumer>;
Ring ring;
Producer producer;
Consumers consumers;
std::mutex mtx;
SwitchBufferImpl(size_t ringBufferSize)
: ring(ringBufferSize)
, producer(&ring)
{
if (ringBufferSize <= 1U)
throw std::logic_error("SwitchBuffer: ring buffer size must be larger than 1");
for (auto &&slot : ring)
slot.reset(new Buffer);
}
SwitchBufferImpl(const SwitchBufferImpl &) = delete;
SwitchBufferImpl(SwitchBufferImpl &&) = delete;
~SwitchBufferImpl()
{
assert(consumers.empty());
}
SwitchBufferImpl &operator=(const SwitchBufferImpl &) = delete;
SwitchBufferImpl &operator=(SwitchBufferImpl &&) = delete;
void CreateConsumer(SwitchBufferConsumer<Buffer> const *parent)
{
std::lock_guard<std::mutex> lock(mtx);
consumers.emplace_back(Consumer(parent, &ring));
}
void CloseProducer()
{
std::lock_guard<std::mutex> lock(mtx);
producer.isClosed = true;
// if there are open promises, break them
for (auto &&consumer : consumers)
consumer.promise.reset();
}
void CloseConsumer(SwitchBufferConsumer<Buffer> const *parent)
{
std::lock_guard<std::mutex> lock(mtx);
auto const it = std::find(std::begin(consumers), std::end(consumers), parent);
assert(it != std::end(consumers));
(void)consumers.erase(it);
}
Buffer &SwitchProducer()
{
std::lock_guard<std::mutex> lock(mtx);
// advance ring iterators
producer.curr = producer.next;
++producer.next;
if (producer.olde == producer.next) {
++producer.olde;
} else if (!producer.olde) {
producer.olde = producer.curr;
}
if (producer.curr) {
// notify consumers that something has been produced
for (auto &&consumer : consumers) {
if (consumer.promise) {
assert(consumer.isEmpty);
consumer.pos = producer.curr;
// fulfill open promise
consumer.promise->set_value(**consumer.pos);
consumer.promise.reset();
} else {
consumer.isEmpty = false;
if (producer.next == consumer.pos) {
// save buffer that is currently consumed
std::swap(*producer.next, consumer.sanctuary);
consumer.isFull = true;
} else if (consumer.isFull) {
consumer.pos = producer.next;
}
}
}
} else {
// no consumable buffer yet
}
return **producer.next;
}
std::future<Buffer const &> SwitchConsumer(
SwitchBufferConsumer<Buffer> const *parent, bool skipToMostRecent)
{
std::lock_guard<std::mutex> lock(mtx);
// determine consumer storage
auto const it = std::find(std::begin(consumers), std::end(consumers), parent);
assert(it != std::end(consumers));
auto &&consumer = *it;
if (consumer.isEmpty) {
if (producer.isClosed) {
// create a promise to be broken immediately
return std::promise<Buffer const &>().get_future();
} else {
// create a promise to fulfill on next Production
consumer.promise.emplace();
return consumer.promise->get_future();
}
} else {
// advance ring iterator
if (skipToMostRecent) {
consumer.pos = producer.curr;
consumer.isEmpty = true;
} else {
consumer.pos = (consumer.pos ? std::next(consumer.pos) : producer.olde);
consumer.isEmpty = (consumer.pos == producer.curr);
}
consumer.isFull = false;
// return buffer immediately
std::promise<Buffer const &> p;
p.set_value(**consumer.pos);
return p.get_future();
}
}
};
} // namespace detail
template<typename Buffer>
SwitchBufferProducer<Buffer>::~SwitchBufferProducer()
{
m_impl->CloseProducer();
}
template<typename Buffer>
SwitchBufferProducer<Buffer>::SwitchBufferProducer(SwitchBufferProducer<Buffer> &&other) noexcept
: m_impl(std::move(other.m_impl))
{}
template<typename Buffer>
SwitchBufferProducer<Buffer> &
SwitchBufferProducer<Buffer>::operator=(SwitchBufferProducer<Buffer> &&other) noexcept
{
m_impl = std::move(other.m_impl);
return *this;
}
template<typename Buffer>
Buffer &SwitchBufferProducer<Buffer>::Switch()
{
return m_impl->SwitchProducer();
}
template<typename Buffer>
SwitchBufferProducer<Buffer>::SwitchBufferProducer(
std::shared_ptr<detail::SwitchBufferImpl<Buffer>> impl)
: m_impl(std::move(impl))
{}
template<typename Buffer>
SwitchBufferConsumer<Buffer>::~SwitchBufferConsumer()
{
m_impl->CloseConsumer(this);
}
template<typename Buffer>
std::future<Buffer const &> SwitchBufferConsumer<Buffer>::Switch(bool skipToMostRecent)
{
return m_impl->SwitchConsumer(this, skipToMostRecent);
}
template<typename Buffer>
SwitchBufferConsumer<Buffer>::SwitchBufferConsumer(
std::shared_ptr<detail::SwitchBufferImpl<Buffer>> impl)
: m_impl(std::move(impl))
{
m_impl->CreateConsumer(this);
}
template<typename Buffer>
SwitchBuffer<Buffer>::SwitchBuffer(size_t ringBufferSize)
: m_impl(new detail::SwitchBufferImpl<Buffer>(ringBufferSize))
, m_producer(new SwitchBufferProducer<Buffer>(m_impl))
{}
template<typename Buffer>
SwitchBuffer<Buffer>::~SwitchBuffer() = default;
template<typename Buffer>
SwitchBuffer<Buffer>::SwitchBuffer(SwitchBuffer<Buffer> &&other) noexcept
: m_impl(std::move(other.m_impl))
{}
template<typename Buffer>
SwitchBuffer<Buffer> &
SwitchBuffer<Buffer>::operator=(SwitchBuffer<Buffer> &&other) noexcept
{
m_impl = std::move(other.m_impl);
return *this;
}
template<typename Buffer>
typename SwitchBuffer<Buffer>::Producer SwitchBuffer<Buffer>::GetProducer()
{
if (!m_producer)
throw std::logic_error("SwitchBuffer: only one producer supported");
else
return std::move(m_producer);
}
template<typename Buffer>
typename SwitchBuffer<Buffer>::Consumer SwitchBuffer<Buffer>::GetConsumer()
{
return Consumer(new SwitchBufferConsumer<Buffer>(m_impl));
}
#endif // SWITCHBUFFER_IMPL_H