-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular_queue_mp.h
More file actions
312 lines (284 loc) · 10.6 KB
/
circular_queue_mp.h
File metadata and controls
312 lines (284 loc) · 10.6 KB
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
#pragma once
/*
circular_queue_mp.h - Implementation of a lock-free circular queue for EspSoftwareSerial.
Copyright (c) 2019 Dirk O. Kaar. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __circular_queue_mp_h
#define __circular_queue_mp_h
#include "circular_queue.h"
#if defined(ESP8266)
#include <interrupts.h>
using esp8266::InterruptLock;
#endif
/*!
@brief Instance class for a multi-producer, single-consumer circular queue / ring buffer (FIFO).
This implementation is lock-free between producers and consumer for the available(), peek(),
pop(), and push() type functions.
*/
template< typename T, typename ForEachArg = void >
class circular_queue_mp : protected circular_queue<T, ForEachArg>
{
public:
circular_queue_mp() : circular_queue<T, ForEachArg>()
{
m_inPos_mp.store(0);
m_concurrent_mp.store(0);
}
circular_queue_mp(const size_t capacity) : circular_queue<T, ForEachArg>(capacity)
{
m_inPos_mp.store(0);
m_concurrent_mp.store(0);
}
circular_queue_mp(circular_queue_mp<T, ForEachArg>&& cq) : circular_queue<T, ForEachArg>(std::move(cq))
{
m_inPos_mp.store(cq.m_inPos_mp.load());
m_concurrent_mp.store(cq.m_concurrent_mp.load());
}
circular_queue_mp& operator=(circular_queue_mp&& cq)
{
circular_queue<T, ForEachArg>::operator=(std::move(cq));
m_inPos_mp.store(cq.m_inPos_mp.load());
m_concurrent_mp.store(cq.m_concurrent_mp.load());
}
circular_queue_mp& operator=(const circular_queue_mp&) = delete;
using circular_queue<T, ForEachArg>::capacity;
using circular_queue<T, ForEachArg>::flush;
using circular_queue<T, ForEachArg>::peek;
using circular_queue<T, ForEachArg>::pop;
using circular_queue<T, ForEachArg>::pop_n;
using circular_queue<T, ForEachArg>::for_each;
using circular_queue<T, ForEachArg>::for_each_rev_requeue;
T& pushpeek() = delete;
bool push() = delete;
inline size_t IRAM_ATTR available() const ALWAYS_INLINE_ATTR
{
return circular_queue<T, ForEachArg>::available();
}
inline size_t IRAM_ATTR available_for_push() const ALWAYS_INLINE_ATTR
{
return circular_queue<T, ForEachArg>::available_for_push();
}
/*!
@brief Resize the queue. The available elements in the queue are preserved.
This is not lock-free and concurrent producer or consumer access
will lead to corruption.
@return True if the new capacity could accommodate the present elements in
the queue, otherwise nothing is done and false is returned.
*/
bool capacity(const size_t cap);
/*!
@brief Move the rvalue parameter into the queue, guarded
for multiple concurrent producers.
@return true if the queue accepted the value, false if the queue
was full.
*/
bool push(T&& val);
/*!
@brief Push a copy of the parameter into the queue, guarded
for multiple concurrent producers.
@return true if the queue accepted the value, false if the queue
was full.
*/
inline bool IRAM_ATTR push(const T& val) ALWAYS_INLINE_ATTR
{
T v(val);
return push(std::move(v));
}
/*!
@brief Push copies of multiple elements from a buffer into the queue,
in order, beginning at buffer's head. This is safe for
multiple producers.
@return The number of elements actually copied into the queue, counted
from the buffer head.
*/
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
size_t push_n(const T* buffer, size_t size);
#endif
protected:
std::atomic<size_t> m_inPos_mp;
std::atomic<int> m_concurrent_mp;
};
template< typename T, typename ForEachArg >
bool circular_queue_mp<T, ForEachArg>::capacity(const size_t cap)
{
if (cap + 1 == circular_queue<T, ForEachArg>::m_bufSize) return true;
else if (!circular_queue<T, ForEachArg>::capacity(cap)) return false;
m_inPos_mp.store(circular_queue<T, ForEachArg>::m_inPos.load(std::memory_order_relaxed),
std::memory_order_relaxed);
m_concurrent_mp.store(0, std::memory_order_relaxed);
return true;
}
template< typename T, typename ForEachArg >
bool IRAM_ATTR circular_queue_mp<T, ForEachArg>::push(T&& val)
{
size_t inPos_mp;
size_t next;
#if !defined(ESP32) && defined(ARDUINO)
class InterruptLock {
public:
InterruptLock() {
noInterrupts();
}
~InterruptLock() {
interrupts();
}
};
{
InterruptLock lock;
#else
++m_concurrent_mp;
do
{
#endif
inPos_mp = m_inPos_mp.load(std::memory_order_relaxed);
next = (inPos_mp + 1) % circular_queue<T, ForEachArg>::m_bufSize;
if (next == circular_queue<T, ForEachArg>::m_outPos.load(std::memory_order_relaxed)) {
#if !defined(ESP32) && defined(ARDUINO)
return false;
}
m_inPos_mp.store(next, std::memory_order_relaxed);
m_concurrent_mp.store(m_concurrent_mp.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_release);
}
#else
int concurrent_mp;
do
{
inPos_mp = m_inPos_mp.load();
concurrent_mp = m_concurrent_mp.load();
if (1 == concurrent_mp)
{
circular_queue<T, ForEachArg>::m_inPos.store(inPos_mp, std::memory_order_release);
}
}
while (!m_concurrent_mp.compare_exchange_weak(concurrent_mp, concurrent_mp - 1));
return false;
}
}
while (!m_inPos_mp.compare_exchange_weak(inPos_mp, next));
#endif
circular_queue<T, ForEachArg>::m_buffer[inPos_mp] = std::move(val);
std::atomic_thread_fence(std::memory_order_release);
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
if (1 == m_concurrent_mp.load(std::memory_order_relaxed))
{
inPos_mp = m_inPos_mp.load(std::memory_order_relaxed);
circular_queue<T, ForEachArg>::m_inPos.store(inPos_mp, std::memory_order_relaxed);
}
m_concurrent_mp.store(m_concurrent_mp.load(std::memory_order_relaxed) - 1,
std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_release);
}
#else
int concurrent_mp;
do
{
inPos_mp = m_inPos_mp.load();
concurrent_mp = m_concurrent_mp.load();
if (1 == concurrent_mp)
{
circular_queue<T, ForEachArg>::m_inPos.store(inPos_mp, std::memory_order_release);
}
}
while (!m_concurrent_mp.compare_exchange_weak(concurrent_mp, concurrent_mp - 1));
#endif
return true;
}
#if defined(ESP8266) || defined(ESP32) || !defined(ARDUINO)
template< typename T, typename ForEachArg >
size_t circular_queue_mp<T, ForEachArg>::push_n(const T* buffer, size_t size)
{
const auto outPos = circular_queue<T, ForEachArg>::m_outPos.load(std::memory_order_relaxed);
size_t inPos_mp;
size_t next;
size_t blockSize;
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
#else
++m_concurrent_mp;
do
{
#endif
inPos_mp = m_inPos_mp.load(std::memory_order_relaxed);
blockSize = (outPos > inPos_mp) ? outPos - 1 - inPos_mp : (outPos == 0) ? circular_queue<T, ForEachArg>::m_bufSize - 1 - inPos_mp : circular_queue<T, ForEachArg>::m_bufSize - inPos_mp;
blockSize = min(size, blockSize);
if (!blockSize)
{
#if !defined(ESP32) && defined(ARDUINO)
return 0;
}
next = (inPos_mp + blockSize) % circular_queue<T, ForEachArg>::m_bufSize;
m_inPos_mp.store(next, std::memory_order_relaxed);
m_concurrent_mp.store(m_concurrent_mp.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_release);
}
#else
int concurrent_mp = m_concurrent_mp.load();
do
{
inPos_mp = m_inPos_mp.load();
concurrent_mp = m_concurrent_mp.load();
if (1 == concurrent_mp)
{
circular_queue<T, ForEachArg>::m_inPos.store(inPos_mp, std::memory_order_release);
}
}
while (!m_concurrent_mp.compare_exchange_weak(concurrent_mp, concurrent_mp - 1));
return false;
}
next = (inPos_mp + blockSize) % circular_queue<T, ForEachArg>::m_bufSize;
}
while (!m_inPos_mp.compare_exchange_weak(inPos_mp, next));
#endif
auto dest = circular_queue<T, ForEachArg>::m_buffer.get() + inPos_mp;
std::copy_n(std::make_move_iterator(buffer), blockSize, dest);
size = min(size - blockSize, outPos > 1 ? static_cast<size_t>(outPos - next - 1) : 0);
next += size;
dest = circular_queue<T, ForEachArg>::m_buffer.get();
std::copy_n(std::make_move_iterator(buffer + blockSize), size, dest);
std::atomic_thread_fence(std::memory_order_release);
#if !defined(ESP32) && defined(ARDUINO)
{
InterruptLock lock;
if (1 == m_concurrent_mp.load(std::memory_order_relaxed))
{
inPos_mp = m_inPos_mp.load(std::memory_order_relaxed);
circular_queue<T, ForEachArg>::m_inPos.store(inPos_mp, std::memory_order_relaxed);
}
m_concurrent_mp.store(m_concurrent_mp.load(std::memory_order_relaxed) - 1,
std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_release);
}
#else
int concurrent_mp;
do
{
inPos_mp = m_inPos_mp.load();
concurrent_mp = m_concurrent_mp.load();
if (1 == concurrent_mp)
{
circular_queue<T, ForEachArg>::m_inPos.store(inPos_mp, std::memory_order_release);
}
}
while (!m_concurrent_mp.compare_exchange_weak(concurrent_mp, concurrent_mp - 1));
#endif
return blockSize + size;
}
#endif
#endif // __circular_queue_mp_h