-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaitFreeBufferQueue.hpp
375 lines (303 loc) · 8.23 KB
/
WaitFreeBufferQueue.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
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
370
371
372
373
374
375
#ifndef _WAIT_FREE_BUFFER_QUEUE_HPP_
#define _WAIT_FREE_BUFFER_QUEUE_HPP_
#include "Buffer.hpp"
#include <stdint.h>
#include <string>
#include <string.h>
#include <mutex>
#include <atomic>
#include <type_traits>
namespace photon
{
template<uint32_t Length>
class WaitFreeBufferQueue
{
public:
WaitFreeBufferQueue() :
m_preIn(0u),
m_postIn(0u),
m_preOut(0u),
m_postOut(0u)
{
}
WaitFreeBufferQueue& operator<<(const int8_t& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const uint8_t& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const int16_t& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const uint16_t& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const int32_t& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const uint32_t& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const int64_t& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const uint64_t& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const float& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const double& value)
{
push(reinterpret_cast<const char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator<<(const Buffer& value)
{
*this << value.m_length;
push(value.m_buffer, value.m_length);
return *this;
}
WaitFreeBufferQueue& operator<<(const std::string& value)
{
*this << (uint32_t)value.length();
push(value.c_str(), (uint32_t)value.length());
return *this;
}
WaitFreeBufferQueue& operator>>(int8_t& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator>>(uint8_t& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator>>(int16_t& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator>>(uint16_t& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator>>(int32_t& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator>>(uint32_t& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator>>(int64_t& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator>>(uint64_t& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator>>(float& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return *this;
}
WaitFreeBufferQueue& operator>>(double& value)
{
pop(reinterpret_cast<char*>(&value), sizeof(value));
return this;
}
WaitFreeBufferQueue& operator>>(Buffer& value)
{
*this >> value.m_length;
pop(value.m_buffer, value.m_length);
return *this;
}
WaitFreeBufferQueue& operator>>(std::string& value)
{
uint32_t length = 0u;
*this >> length;
value.resize(length);
pop(&value[0], length);
return *this;
}
inline static uint32_t getSize(const int8_t& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const uint8_t& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const int16_t& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const uint16_t& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const int32_t& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const uint32_t& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const int64_t& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const uint64_t& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const float& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const double& value)
{
return sizeof(value);
}
inline static uint32_t getSize(const Buffer& value)
{
return sizeof(value.m_length) + value.m_length;
}
inline static uint32_t getSize(const std::string& value)
{
return (uint32_t)sizeof(uint32_t) + (uint32_t)value.size();
}
void push(const char* buffer, uint32_t length)
{
uint32_t preIn = m_preIn.fetch_add(length); //take position
while (preIn - m_postOut + length > Length); //wait readers read
uint32_t backFreeStart = preIn & (Length - 1);
uint32_t backFreeToEndLen = Length - backFreeStart;
if (backFreeToEndLen >= length)
{
//队列剩余空间连续,直接填充数据
memcpy(m_buffer + backFreeStart, buffer, length);
}
else
{
//剩余空间不连续,先填满队列尾部空间
memcpy(m_buffer + backFreeStart, buffer, backFreeToEndLen);
//再将剩余部分填充至头部
memcpy(m_buffer, buffer + backFreeToEndLen, length - backFreeToEndLen);
}
while (m_postIn < preIn); //wait previous writer finish
m_postIn += length;
}
void pop(char* buffer, uint32_t length)
{
uint32_t preOut = m_preOut.fetch_add(length); //take position
while (m_postIn - preOut < length); //wait writers write
uint32_t backDataStart = preOut & (Length - 1);
uint32_t backDataToEndLen = Length - backDataStart;
if (backDataToEndLen >= length)
{
//队列数据空间连续,直接填充数据
memcpy(buffer, m_buffer + backDataStart, length);
}
else
{
//数据空间不连续,先使用队列尾部数据
memcpy(buffer, m_buffer + backDataStart, backDataToEndLen);
//再使用队列头部数据
memcpy(buffer + backDataToEndLen, m_buffer, length - backDataToEndLen);
}
while (m_postOut < preOut);
m_postOut += length;
}
/*const Buffer front() const
{
if (empty())
{
return Buffer();
}
uint32_t dataStart = m_out & (Length - 1);
uint32_t dataEnd = m_in & (Length - 1);
if (dataStart < dataEnd)
{
return Buffer(const_cast<char*>(m_buffer) + dataStart, dataEnd - dataStart);
}
return Buffer(const_cast<char*>(m_buffer) + dataStart, Length - dataStart);
}
void pop(uint32_t length)
{
m_out += length;
}
Buffer reserve() noexcept
{
if (full())
{
return Buffer();
}
uint32_t freeStart = m_in & (Length - 1);
uint32_t freeEnd = m_out & (Length - 1);
if (freeStart < freeEnd)
{
return Buffer(m_buffer + freeStart, freeEnd - freeStart);
}
return Buffer(m_buffer + freeStart, Length - freeStart);
}
void push(uint32_t length) noexcept
{
m_in += length;
}*/
bool empty() const noexcept
{
return m_preIn == m_postOut;
}
bool full() const noexcept
{
return m_preIn - m_postOut == Length;
}
uint32_t totalSize() const noexcept
{
return Length;
}
uint32_t dataSize() const noexcept
{
return m_postIn - m_preOut;
}
uint32_t freeSize() const noexcept
{
return Length - dataSize();
}
private:
std::atomic<uint32_t> m_preIn;
std::atomic<uint32_t> m_postIn;
std::atomic<uint32_t> m_preOut;
std::atomic<uint32_t> m_postOut;
typename std::enable_if<Length != 0 && (Length & (Length - 1)) == 0, char>::type m_buffer[Length];
};
}
#endif //_WAIT_FREE_BUFFER_QUEUE_HPP_