-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockingQueueMinimal.h
287 lines (252 loc) · 5.16 KB
/
BlockingQueueMinimal.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
#ifndef BUFFER_H
#define BUFFER_H
#include <mutex>
#include <queue>
#include <atomic>
#include <set>
#include <thread>
#include <numeric>
template<class QueueData>class BlockingQueue
{
private:
std::queue<QueueData*> m_queue; // Use STL queue to store data
std::atomic<bool> m_lock;
QueueData* m_latest;
bool m_done;
BlockingQueue(const BlockingQueue &);
BlockingQueue* operator=(const BlockingQueue &);
BlockingQueue(BlockingQueue &&);
BlockingQueue* operator=(BlockingQueue &&);
void lock() {
bool val = false;
while (!m_lock.compare_exchange_strong(val, true,std::memory_order::memory_order_seq_cst)) {
val = false;
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
}
}
void unlock() {
m_lock.store(false);
}
bool try_lock() {
bool val = false;
if (m_lock.compare_exchange_strong(val, true, std::memory_order::memory_order_seq_cst)) {
return true;
}
return false;
}
bool try_lock_for(int _10_nanoseconds_times) {
int count = 0;
while (!try_lock()) {
if (count >= _10_nanoseconds_times) {
return false;
}
count++;
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
}
return true;
}
public:
BlockingQueue()
{
m_done = false;
m_lock = false;
m_latest = nullptr;
}
~BlockingQueue() {
ShutDown();
CleanUp();
};
void Insert(QueueData *_data)
{
lock();
if (m_done) {
unlock();
return;
}
m_queue.push(_data);
m_latest = _data;
unlock();
return;
}
bool Insert_try(QueueData *_data)
{
if (!try_lock_for(10)) {
return false;
}
if (m_done) {
unlock();
return true;
}
m_queue.push(_data);
unlock();
return true;
}
int Remove_try(QueueData **_data)
{
if (!try_lock_for(500)) {
return 2;
}
if (m_queue.size() == 0)
{
if (m_done) {
unlock();
return 0;
}
unlock();
return 2;
}
if (m_latest) {
*_data = m_latest;
m_latest = nullptr;
}
else {
*_data = &(*m_queue.front());
}
m_queue.pop();
unlock();
return 1;
};
int Remove(QueueData **_data) {
while (true) {
auto c = Remove_try(_data);
if (c == 0 || c == 1) {
return c;
}
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
}
}
int CanInsert()
{
lock();
if (m_done) {
unlock();
return 0;
}
unlock();
return 1;
};
void ShutDown()
{
lock();
m_done = true;
unlock();
}
void Restart() {
CleanUp();
lock();
m_done = false;
unlock();
}
bool IsShutDown() {
lock();
bool val= m_done;
unlock();
return val;
}
void CleanUp()
{
lock();
while (!m_queue.empty())
{
m_queue.pop();
}
unlock();
}
};
template<class QueueData>
class BlockingQueueFast {
std::vector<BlockingQueue<QueueData>* > m_qs;
std::vector<std::atomic<bool>*> m_consumer_qs;
std::size_t m_max_val;
std::atomic<int> m_producer_token;
std::atomic<int> m_consumer_token;
int m_token_jmp_consumer;
int m_token_jmp_producer;
public:
BlockingQueueFast(std::size_t _producers,
std::size_t _consumers)
{
std::size_t count = (std::max)((_producers + _consumers) * 2, static_cast<std::size_t>(std::thread::hardware_concurrency()));
m_qs.reserve(count);
std::size_t s = 0;
for (; s < count; s++) {
m_qs.push_back(new BlockingQueue<QueueData>());
m_consumer_qs.push_back(new std::atomic<bool>(true));
}
m_max_val = m_qs.size();
m_consumer_token = 0;
m_producer_token = 0;
m_token_jmp_consumer = static_cast<double>(count) / static_cast<double>(_consumers);
m_token_jmp_producer = static_cast<double>(count) / static_cast<double>(_producers);
}
~BlockingQueueFast() {
ShutDown();
CleanUp();
std::size_t s = 0;
for (; s<m_qs.size(); s++) {
delete m_qs[s];
delete m_consumer_qs[s];
}
m_qs.clear();
};
int get_producer_token() {
return m_producer_token.fetch_add(m_token_jmp_producer);
}
int get_consumer_token() {
return m_consumer_token.fetch_add(m_token_jmp_consumer);
}
void Insert(QueueData *_data, int &_token )
{
for (auto v = 0; v < m_max_val; v++) {
_token = _token % m_max_val;
if (m_qs[_token]->Insert_try(_data)) {
return;
}
_token++;
}
_token = _token % m_max_val;
m_qs[_token]->Insert(_data);
}
// Get data from the queue. Wait for data if not available
int Remove(QueueData **_data, int &_token )
{
auto complete_qs = 0;
while (true) {
_token = _token%m_max_val;
if (m_consumer_qs[_token]->load()) {
int c = m_qs[_token]->Remove_try(_data);
if (c == 1) {
return c;
}
if (c == 0) {
m_consumer_qs[_token]->store(false);
}
if (c == 2) {
std::this_thread::sleep_for(std::chrono::nanoseconds(10));
}
}
else {
complete_qs++;
if (complete_qs >= m_max_val) {
return 0;
}
}
_token++;
}
};
void ShutDown()
{
std::size_t s = 0;
for(;s<m_qs.size();s++){
m_qs[s]->ShutDown();
}
}
void CleanUp()
{
std::size_t s = 0;
for (; s<m_qs.size(); s++) {
m_qs[s]->CleanUp();
}
}
};
#endif