-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspsc.hpp
328 lines (264 loc) · 8.04 KB
/
spsc.hpp
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
#pragma once
// c
#include <stdint.h>
// c++
#include <algorithm>
#include <atomic>
#include <chrono>
#include <memory>
#include <vector>
// spdlog
#include <spdlog/spdlog.h>
#define LOAD_ATOMIC_RELAXED(offset) offset.load(std::memory_order_relaxed)
#define STORE_ATOMIC_RELAXED(offset, value) offset.store(value, std::memory_order_relaxed)
inline uint32_t roundup_pow_of_two(uint32_t n)
{
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
inline uint32_t rounddown_pow_of_two(uint32_t n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return (n + 1) >> 1;
}
// lock free, yet thread-safe single-producer single-consumer buffer
template<typename T>
class lock_free_spsc
{
public:
lock_free_spsc()
: m_stopping(false)
, m_input_offset(0)
, m_output_offset(0)
, m_last_full_log_time(std::chrono::steady_clock::now())
, m_last_full_log_repeat_times(0)
, m_last_empty_log_time(std::chrono::steady_clock::now())
, m_last_empty_log_repeat_times(0)
{
}
lock_free_spsc(uint32_t buffer_size)
: m_stopping(false)
, m_input_offset(0)
, m_output_offset(0)
{
reset(buffer_size);
}
~lock_free_spsc()
{
reset(0);
}
void stopping()
{
m_stopping = true;
}
void reset(uint32_t buffer_size)
{
m_input_offset = 0;
m_output_offset = 0;
m_last_full_log_repeat_times = 0;
m_last_empty_log_repeat_times = 0;
if (0 == buffer_size) {
m_stopping = true;
m_ring_buffer.clear();
}
else {
m_stopping = false;
m_last_full_log_time = std::chrono::steady_clock::now() - std::chrono::seconds(10);
m_last_empty_log_time = std::chrono::steady_clock::now() - std::chrono::seconds(10);
if (buffer_size & (buffer_size - 1)) {
buffer_size = roundup_pow_of_two(buffer_size);
}
if (buffer_size > 0) {
m_ring_buffer.resize(buffer_size, 0);
}
}
}
void clear()
{
get_all();
}
uint32_t buffer_size()
{
return (uint32_t)m_ring_buffer.size();
}
bool is_buffer_null()
{
return m_ring_buffer.empty() && 0 == m_ring_buffer.capacity();
}
bool is_buffer_empty()
{
return 0 == available_data_size();
}
bool is_buffer_full()
{
return (uint32_t)m_ring_buffer.size() == available_data_size();
}
uint32_t available_data_size()
{
return LOAD_ATOMIC_RELAXED(m_input_offset) - LOAD_ATOMIC_RELAXED(m_output_offset);
}
uint32_t available_space_size()
{
return (uint32_t)m_ring_buffer.size() - LOAD_ATOMIC_RELAXED(m_input_offset) + LOAD_ATOMIC_RELAXED(m_output_offset);
}
uint32_t put(const T item)
{
T buff[] = { item };
return put(buff, 1);
}
uint32_t put(const std::vector<T> &input_buffer)
{
return put(input_buffer.data(), (uint32_t)input_buffer.size());
}
uint32_t put_if_not_full(const T *input_buffer, uint32_t length)
{
uint32_t offset = 0;
while (!m_stopping && offset < length) {
uint32_t c = put(input_buffer + offset, length - offset);
if (0 == c) {
break;
}
offset += c;
if (!m_stopping && offset < length) {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
else {
break;
}
}
return offset;
}
uint32_t put(const T *input_buffer, uint32_t length)
{
uint32_t buffer_size = (uint32_t)m_ring_buffer.size();
length = std::min(length, buffer_size - LOAD_ATOMIC_RELAXED(m_input_offset) + LOAD_ATOMIC_RELAXED(m_output_offset));
if (length <= 0) {
m_last_full_log_repeat_times++;
auto now = std::chrono::steady_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_last_full_log_time).count();
if (ms > 10000) {
m_last_full_log_time = now;
SPDLOG_WARN(
"no space, n: {}, p: {}, s({}) - w({}) + o({}) = {}\n",
m_last_full_log_repeat_times, fmt::ptr(this), m_ring_buffer.size(), LOAD_ATOMIC_RELAXED(m_input_offset), LOAD_ATOMIC_RELAXED(m_output_offset), available_space_size()
);
m_last_full_log_repeat_times = 0;
}
return 0;
}
// ensure that we sample the input offset before we start putting bytes into the buffer
std::atomic_thread_fence(std::memory_order_acquire);
uint32_t write_offset = LOAD_ATOMIC_RELAXED(m_input_offset) & (buffer_size - 1);
// first put the data starting from in to buffer end
uint32_t first_part = std::min(length, buffer_size - write_offset);
std::memcpy(m_ring_buffer.data() + write_offset, input_buffer, sizeof(T) * first_part);
// then put the rest (if any) at the beginning of the buffer
std::memcpy(m_ring_buffer.data(), input_buffer + first_part, sizeof(T) * (length - first_part));
// ensure that we add the bytes to the buffer before we update the input offset
std::atomic_thread_fence(std::memory_order_release);
STORE_ATOMIC_RELAXED(m_input_offset, LOAD_ATOMIC_RELAXED(m_input_offset) + length);
return length;
}
uint32_t peek(T &item)
{
uint32_t length = std::min(1u, LOAD_ATOMIC_RELAXED(m_input_offset) - LOAD_ATOMIC_RELAXED(m_output_offset));
if (length <= 0) {
return 0;
}
uint32_t read_offset = LOAD_ATOMIC_RELAXED(m_output_offset) & ((uint32_t)m_ring_buffer.size() - 1);
item = m_ring_buffer[read_offset];
return length;
}
uint32_t get(T &item)
{
T buf[] = { item };
auto count = get(buf, 1);
item = buf[0];
return count;
}
uint32_t get(std::vector<T> &output_buffer)
{
return get(output_buffer.data(), (uint32_t)output_buffer.size());
}
std::vector<T> get_all()
{
std::vector<T> result(available_data_size());
if (result.empty()) {
return result;
}
get(result);
return result;
}
uint32_t get_if_not_empty(T *output_buffer, uint32_t length)
{
uint32_t offset = 0;
while (!m_stopping && 0 == offset) {
uint32_t c = get(output_buffer + offset, length - offset);
if (c == 0 && offset > 0) {
break;
}
offset += c;
if (!m_stopping && 0 == offset) {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
else {
break;
}
}
return offset;
}
uint32_t get(T *output_buffer, uint32_t length)
{
length = std::min(length, LOAD_ATOMIC_RELAXED(m_input_offset) - LOAD_ATOMIC_RELAXED(m_output_offset));
if (length <= 0) {
m_last_empty_log_repeat_times++;
auto now = std::chrono::steady_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_last_empty_log_time).count();
if (ms > 10000) {
m_last_empty_log_time = now;
SPDLOG_WARN(
"no data, n: {}, p: {}, s({}) - w({}) + o({}) = {}\n",
m_last_empty_log_repeat_times, fmt::ptr(this), m_ring_buffer.size(), LOAD_ATOMIC_RELAXED(m_input_offset), LOAD_ATOMIC_RELAXED(m_output_offset), available_data_size()
);
m_last_empty_log_repeat_times = 0;
}
return 0;
}
// ensure that we sample the output offset before we start removing bytes from the buffer
std::atomic_thread_fence(std::memory_order_acquire);
uint32_t buffer_size = (uint32_t)m_ring_buffer.size();
uint32_t read_offset = LOAD_ATOMIC_RELAXED(m_output_offset) & (buffer_size - 1);
// first get the data from out until the end of the buffer
uint32_t first_part = std::min(length, buffer_size - read_offset);
std::memcpy(output_buffer, m_ring_buffer.data() + read_offset, sizeof(T) * first_part);
// then get the rest (if any) from the beginning of the buffer
std::memcpy(output_buffer + first_part, m_ring_buffer.data(), sizeof(T) * (length - first_part));
// ensure that we remove the bytes from the buffer before we update the output offset
std::atomic_thread_fence(std::memory_order_release);
STORE_ATOMIC_RELAXED(m_output_offset, LOAD_ATOMIC_RELAXED(m_output_offset) + length);
return length;
}
private:
bool m_stopping;
std::vector<T> m_ring_buffer; // the buffer holding the data
std::atomic<uint32_t> m_input_offset; // data is added at offset: m_input_offset % (size - 1)
std::atomic<uint32_t> m_output_offset; // data is extracted from offset: m_output_offset % (size - 1)
// last full log time
std::chrono::steady_clock::time_point m_last_full_log_time;
// log full log repeat times
uint32_t m_last_full_log_repeat_times;
// last empty log time
std::chrono::steady_clock::time_point m_last_empty_log_time;
// log empty log repeat times
uint32_t m_last_empty_log_repeat_times;
};