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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2020 Gothel Software e.K.
* Copyright (c) 2020 ZAFENA AB
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef LFRINGBUFFER_HPP_
#define LFRINGBUFFER_HPP_
#include <cstring>
#include <string>
#include <cstdint>
#include <atomic>
#include <memory>
#include <mutex>
#include <condition_variable>
#include "BasicTypes.hpp"
#include "Ringbuffer.hpp"
namespace direct_bt {
/**
* Simple implementation of {@link Ringbuffer},
* exposing <i>lock-free</i>
* {@link #get() get*(..)} and {@link #put(Object) put*(..)} methods.
* <p>
* Implementation utilizes the <i>Always Keep One Slot Open</i>,
* hence implementation maintains an internal array of <code>capacity</code> <i>plus one</i>!
* </p>
* <p>
* Implementation is thread safe if:
* <ul>
* <li>{@link #get() get*(..)} operations from multiple threads.</li>
* <li>{@link #put(Object) put*(..)} operations from multiple threads.</li>
* <li>{@link #get() get*(..)} and {@link #put(Object) put*(..)} thread may be the same.</li>
* </ul>
* </p>
* <p>
* Following methods use acquire the global multi-read and -write mutex:
* <ul>
* <li>{@link #resetFull(Object[])}</li>
* <li>{@link #clear()}</li>
* <li>{@link #growEmptyBuffer(Object[])}</li>
* </ul>
* </p>
* <p>
* Characteristics:
* <ul>
* <li>Read position points to the last read element.</li>
* <li>Write position points to the last written element.</li>
* </ul>
* <table border="1">
* <tr><td>Empty</td><td>writePos == readPos</td><td>size == 0</td></tr>
* <tr><td>Full</td><td>writePos == readPos - 1</td><td>size == capacity</td></tr>
* </table>
* </p>
*/
template <typename T, std::nullptr_t nullelem> class LFRingbuffer : public Ringbuffer<T> {
private:
std::mutex syncRead, syncMultiRead;
std::mutex syncWrite, syncMultiWrite;
std::condition_variable cvRead;
std::condition_variable cvWrite;
/* final */ int volatile capacityPlusOne; // not final due to grow
/* final */ T * volatile array; // not final due to grow
int volatile readPos;
int volatile writePos;
std::atomic_int size;
T * newArray(const int count) {
return new T[count];
}
void freeArray(T * a) {
delete[] a;
}
void cloneFrom(const bool allocArrayAndCapacity, const LFRingbuffer & source) {
if( allocArrayAndCapacity ) {
capacityPlusOne = source.capacityPlusOne;
if( nullptr != array ) {
freeArray(array, true);
}
array = newArray(capacityPlusOne);
} else if( capacityPlusOne != source.capacityPlusOne ) {
throw InternalError("capacityPlusOne not equal: this "+toString()+", source "+source.toString(), E_FILE_LINE);
}
readPos = source.readPos;
writePos = source.writePos;
size = source.size;
int localWritePos = readPos;
for(int i=0; i<size; i++) {
localWritePos = (localWritePos + 1) % capacityPlusOne;
array[localWritePos] = source.array[localWritePos];
}
if( writePos != localWritePos ) {
throw InternalError("copy segment error: this "+toString()+", localWritePos "+std::to_string(localWritePos)+"; source "+source.toString(), E_FILE_LINE);
}
}
void resetImpl(const T * copyFrom, const int copyFromCount) /* throws IllegalArgumentException */ {
// clear all elements, zero size
if( 0 < size ) {
int localReadPos = readPos;
for(int i=0; i<size; i++) {
localReadPos = (localReadPos + 1) % capacityPlusOne;
array[localReadPos] = nullelem;
}
if( writePos != localReadPos ) {
throw InternalError("copy segment error: this "+toString()+", localReadPos "+std::to_string(localReadPos)+"; source count "+std::to_string(copyFromCount), E_FILE_LINE);
}
readPos = localReadPos;
size = 0;
}
// fill with copyFrom elements
if( nullptr != copyFrom && 0 < copyFromCount ) {
if( copyFromCount > capacityPlusOne-1 ) {
throw IllegalArgumentException("copyFrom array length "+std::to_string(copyFromCount)+" > capacity "+toString(), E_FILE_LINE);
}
int localWritePos = writePos;
for(int i=0; i<copyFromCount; i++) {
localWritePos = (localWritePos + 1) % capacityPlusOne;
array[localWritePos] = copyFrom[i];
size++;
}
writePos = localWritePos;
}
}
T getImpl(const bool blocking, const bool peek) /* throws InterruptedException */ {
std::unique_lock<std::mutex> lockMultiRead(syncMultiRead); // RAII-style acquire and relinquish via destructor
int localReadPos = readPos;
if( localReadPos == writePos ) {
if( blocking ) {
std::unique_lock<std::mutex> lockRead(syncRead); // RAII-style acquire and relinquish via destructor
while( localReadPos == writePos ) {
cvRead.wait(lockRead);
}
} else {
return nullelem;
}
}
localReadPos = (localReadPos + 1) % capacityPlusOne;
T r = array[localReadPos];
if( !peek ) {
array[localReadPos] = nullelem;
{
std::unique_lock<std::mutex> lockWrite(syncWrite); // RAII-style acquire and relinquish via destructor
size--;
readPos = localReadPos;
cvWrite.notify_all(); // notify waiting putter
}
}
return r;
}
bool putImpl(const T &e, const bool sameRef, const bool blocking) /* throws InterruptedException */ {
std::unique_lock<std::mutex> lockMultiWrite(syncMultiWrite); // RAII-style acquire and relinquish via destructor
int localWritePos = writePos;
localWritePos = (localWritePos + 1) % capacityPlusOne;
if( localWritePos == readPos ) {
if( blocking ) {
std::unique_lock<std::mutex> lockWrite(syncWrite); // RAII-style acquire and relinquish via destructor
while( localWritePos == readPos ) {
cvWrite.wait(lockWrite);
}
} else {
return false;
}
}
if( !sameRef ) {
array[localWritePos] = e;
}
{
std::unique_lock<std::mutex> lockRead(syncRead); // RAII-style acquire and relinquish via destructor
size++;
writePos = localWritePos;
cvRead.notify_all(); // notify waiting getter
}
return true;
}
public:
std::string toString() const override {
const std::string es = isEmpty() ? ", empty" : "";
const std::string fs = isFull() ? ", full" : "";
return "LFRingbuffer<?>[size "+std::to_string(size)+" / "+std::to_string(capacityPlusOne-1)+
", writePos "+std::to_string(writePos)+", readPos "+std::to_string(readPos)+es+fs+"]";
}
void dump(FILE *stream, std::string prefix) const override {
fprintf(stream, "%s %s {\n", prefix.c_str(), toString().c_str());
for(int i=0; i<capacityPlusOne; i++) {
// fprintf(stream, "\t[%d]: %p\n", i, array[i].get()); // FIXME
}
fprintf(stream, "}\n");
}
/**
* Create a full ring buffer instance w/ the given array's net capacity and content.
* <p>
* Example for a 10 element Integer array:
* <pre>
* Integer[] source = new Integer[10];
* // fill source with content ..
* Ringbuffer<Integer> rb = new LFRingbuffer<Integer>(source);
* </pre>
* </p>
* <p>
* {@link #isFull()} returns true on the newly created full ring buffer.
* </p>
* <p>
* Implementation will allocate an internal array with size of array <code>copyFrom</code> <i>plus one</i>,
* and copy all elements from array <code>copyFrom</code> into the internal array.
* </p>
* @param copyFrom mandatory source array determining ring buffer's net {@link #capacity()} and initial content.
* @throws IllegalArgumentException if <code>copyFrom</code> is <code>nullptr</code>
*/
LFRingbuffer(const std::vector<T> & copyFrom) /* throws IllegalArgumentException */
: capacityPlusOne(copyFrom.size() + 1), array(newArray(capacityPlusOne)),
readPos(0), writePos(0), size(0)
{
resetImpl(copyFrom.data(), copyFrom.size());
}
LFRingbuffer(const T * copyFrom, const int copyFromSize) /* throws IllegalArgumentException */
: capacityPlusOne(copyFromSize + 1), array(newArray(capacityPlusOne)),
readPos(0), writePos(0), size(0)
{
resetImpl(copyFrom, copyFromSize);
}
/**
* Create an empty ring buffer instance w/ the given net <code>capacity</code>.
* <p>
* Example for a 10 element Integer array:
* <pre>
* Ringbuffer<Integer> rb = new LFRingbuffer<Integer>(10, Integer[].class);
* </pre>
* </p>
* <p>
* {@link #isEmpty()} returns true on the newly created empty ring buffer.
* </p>
* <p>
* Implementation will allocate an internal array of size <code>capacity</code> <i>plus one</i>.
* </p>
* @param arrayType the array type of the created empty internal array.
* @param capacity the initial net capacity of the ring buffer
*/
LFRingbuffer(const int capacity)
: capacityPlusOne(capacity + 1), array(newArray(capacityPlusOne)),
readPos(0), writePos(0), size(0)
{ }
~LFRingbuffer() {
freeArray(array);
}
LFRingbuffer(const LFRingbuffer &_source) noexcept
: capacityPlusOne(_source.capacityPlusOne), array(newArray(capacityPlusOne)),
readPos(0), writePos(0), size(0)
{
std::unique_lock<std::mutex> lockMultiReadS(_source.syncMultiRead);
std::unique_lock<std::mutex> lockMultiWriteS(_source.syncMultiWrite);
std::unique_lock<std::mutex> lockMultiRead(syncMultiRead);
std::unique_lock<std::mutex> lockMultiWrite(syncMultiWrite);
cloneFrom(false, _source);
}
LFRingbuffer& operator=(const LFRingbuffer &_source) {
std::unique_lock<std::mutex> lockMultiReadS(_source.syncMultiRead);
std::unique_lock<std::mutex> lockMultiWriteS(_source.syncMultiWrite);
std::unique_lock<std::mutex> lockMultiRead(syncMultiRead);
std::unique_lock<std::mutex> lockMultiWrite(syncMultiWrite);
if( this == &_source ) {
return *this;
}
if( capacityPlusOne != _source.capacityPlusOne ) {
cloneFrom(true, _source);
} else {
resetImpl(nullptr, 0 /* empty, nothing to copy */ ); // clear
cloneFrom(false, _source);
}
return *this;
}
LFRingbuffer(LFRingbuffer &&o) noexcept = default;
LFRingbuffer& operator=(LFRingbuffer &&o) noexcept = default;
int capacity() const override { return capacityPlusOne-1; }
void clear() override {
std::unique_lock<std::mutex> lockMultiRead(syncMultiRead); // RAII-style acquire and relinquish via destructor
std::unique_lock<std::mutex> lockMultiWrite(syncMultiWrite); // ditto
resetImpl(nullptr, 0 /* empty, nothing to copy */ );
}
void reset(const T * copyFrom, const int copyFromCount) override {
std::unique_lock<std::mutex> lockMultiRead(syncMultiRead); // RAII-style acquire and relinquish via destructor
std::unique_lock<std::mutex> lockMultiWrite(syncMultiWrite); // ditto
resetImpl(copyFrom, copyFromCount);
}
void reset(const std::vector<T> & copyFrom) override {
std::unique_lock<std::mutex> lockMultiRead(syncMultiRead); // RAII-style acquire and relinquish via destructor
std::unique_lock<std::mutex> lockMultiWrite(syncMultiWrite); // ditto
resetImpl(copyFrom.data(), copyFrom.size());
}
int getSize() const override { return size; }
int getFreeSlots() const override { return capacityPlusOne - 1 - size; }
bool isEmpty() const override { return writePos == readPos; /* 0 == size */ }
bool isFull() const override { return ( writePos + 1 ) % capacityPlusOne == readPos ; /* capacityPlusOne - 1 == size */; }
T get() override { return getImpl(false, false); }
T getBlocking() override /* throws InterruptedException */ {
return getImpl(true, false);
}
T peek() override {
return getImpl(false, true);
}
T peekBlocking() override /* throws InterruptedException */ {
return getImpl(true, true);
}
bool put(const T & e) override {
return putImpl(e, false, false);
}
void putBlocking(const T & e) override /* throws InterruptedException */ {
if( !putImpl(e, false, true) ) {
throw InternalError("Blocking put failed: "+toString(), E_FILE_LINE);
}
}
bool putSame(bool blocking) override /* throws InterruptedException */ {
return putImpl(nullelem, true, blocking);
}
void waitForFreeSlots(const int count) override /* throws InterruptedException */ {
std::unique_lock<std::mutex> lockMultiWrite(syncMultiWrite); // RAII-style acquire and relinquish via destructor
std::unique_lock<std::mutex> lockRead(syncRead); // RAII-style acquire and relinquish via destructor
while( capacityPlusOne - 1 - size < count ) {
cvRead.wait(lockRead);
}
}
void recapacity(const int newCapacity) override {
std::unique_lock<std::mutex> lockMultiRead(syncMultiRead);
std::unique_lock<std::mutex> lockMultiWrite(syncMultiWrite);
if( capacityPlusOne == newCapacity+1 ) {
return;
}
if( size > newCapacity ) {
throw IllegalArgumentException("amount "+std::to_string(newCapacity)+" < size, "+toString(), E_FILE_LINE);
}
if( 0 > newCapacity ) {
throw IllegalArgumentException("amount "+std::to_string(newCapacity)+" < 0, "+toString(), E_FILE_LINE);
}
// save current data
int oldCapacityPlusOne = capacityPlusOne;
T * oldArray = array;
int oldReadPos = readPos;
// new blank resized array
capacityPlusOne = newCapacity + 1;
array = newArray(capacityPlusOne);
readPos = 0;
writePos = 0;
const int _size = size.load(); // fast access
// copy saved data
if( nullptr != oldArray && 0 < _size ) {
int localWritePos = writePos;
for(int i=0; i<_size; i++) {
localWritePos = (localWritePos + 1) % capacityPlusOne;
oldReadPos = (oldReadPos + 1) % oldCapacityPlusOne;
array[localWritePos] = oldArray[oldReadPos];
}
writePos = localWritePos;
}
freeArray(oldArray); // and release
}
};
} /* namespace direct_bt */
#endif /* LFRINGBUFFER_HPP_ */
|