-
Notifications
You must be signed in to change notification settings - Fork 299
/
CircularQueue.inl
340 lines (290 loc) · 9.82 KB
/
CircularQueue.inl
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
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program 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 program 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 program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/helper/config.h>
#include <sofa/helper/system/thread/CircularQueue.h>
#include <thread>
#include <iostream>
namespace sofa::helper::system::thread
{
template<class T, template<class U> class StoragePolicy, class ThreadAccessPolicy>
CircularQueue<T, StoragePolicy, ThreadAccessPolicy>::CircularQueue()
{
ThreadAccessPolicy::init(this->getQueue(), this->maxCapacity());
}
template<class T, template<class U> class StoragePolicy, class ThreadAccessPolicy>
CircularQueue<T, StoragePolicy, ThreadAccessPolicy>::~CircularQueue()
{
}
template<class T, template<class U> class StoragePolicy, class ThreadAccessPolicy>
bool CircularQueue<T, StoragePolicy, ThreadAccessPolicy>::isEmpty() const
{
return ThreadAccessPolicy::isEmpty(this->maxSize());
}
template<class T, template<class U> class StoragePolicy, class ThreadAccessPolicy>
bool CircularQueue<T, StoragePolicy, ThreadAccessPolicy>::isFull() const
{
return ThreadAccessPolicy::isFull(this->maxSize());
}
template<class T, template<class U> class StoragePolicy, class ThreadAccessPolicy>
unsigned CircularQueue<T, StoragePolicy, ThreadAccessPolicy>::size() const
{
return ThreadAccessPolicy::size(this->maxSize());
}
template<class T, template<class U> class StoragePolicy, class ThreadAccessPolicy>
bool CircularQueue<T, StoragePolicy, ThreadAccessPolicy>::push(const T& item)
{
return ThreadAccessPolicy::push(this->getQueue(), this->maxSize(), this->maxCapacity(), item);
}
template<class T, template<class U> class StoragePolicy, class ThreadAccessPolicy>
bool CircularQueue<T, StoragePolicy, ThreadAccessPolicy>::pop(T& item, bool clear)
{
return ThreadAccessPolicy::pop(this->getQueue(), this->maxSize(), this->maxCapacity(), item, clear);
}
template<class T, template<class U> class StoragePolicy, class ThreadAccessPolicy>
unsigned CircularQueue<T, StoragePolicy, ThreadAccessPolicy>::skip(unsigned maxsize, bool clear)
{
return ThreadAccessPolicy::skip(this->getQueue(), this->maxSize(), this->maxCapacity(), maxsize, clear);
}
template<class T, template<class U> class StoragePolicy, class ThreadAccessPolicy>
template<class OutputIterator>
unsigned CircularQueue<T, StoragePolicy, ThreadAccessPolicy>::pop(OutputIterator out, unsigned maxsize, bool clear)
{
return ThreadAccessPolicy::pop(this->getQueue(), this->maxSize(), this->maxCapacity(), out, maxsize, clear);
}
inline SOFA_HELPER_API OneThreadPerEnd::OneThreadPerEnd()
: head(0), tail(0)
{
}
inline SOFA_HELPER_API bool OneThreadPerEnd::isEmpty(unsigned /*maxSize*/) const
{
return head == tail;
}
inline SOFA_HELPER_API bool OneThreadPerEnd::isFull(unsigned maxSize) const
{
return (tail+1) % maxSize == head;
}
inline SOFA_HELPER_API unsigned OneThreadPerEnd::size(unsigned maxSize) const
{
const unsigned h = head;
unsigned t = tail;
if (t < h) t += maxSize;
return t - h;
}
template<class T>
void OneThreadPerEnd::init(T /*array*/[], unsigned /*maxCapacity*/)
{
}
template<class T>
bool OneThreadPerEnd::push(T array[], unsigned maxSize, unsigned /*maxCapacity*/, const T& item)
{
const unsigned nextTail = (tail + 1) % maxSize;
if(nextTail == head)
{
// queue is full
return false;
}
array[tail] = item;
tail = nextTail;
return true;
}
template<class T>
bool OneThreadPerEnd::pop(T array[], unsigned maxSize, unsigned /*maxCapacity*/, T& item, bool clear)
{
if(isEmpty(maxSize))
{
// queue is empty
return false;
}
item = array[head];
if (clear)
{
array[head] = T();
}
head = (head + 1) % maxSize;
return true;
}
template<class T>
unsigned OneThreadPerEnd::skip(T array[], unsigned maxSize, unsigned /*maxCapacity*/, unsigned outmaxsize, bool clear)
{
const unsigned currentSize = size(maxSize);
const unsigned outsize = (currentSize < outmaxsize) ? currentSize : outmaxsize;
if (outsize == 0)
{
// queue is empty
return 0;
}
if (clear)
{
unsigned i = head;
unsigned iend = head + outsize;
if (iend >= maxSize)
{
for (;i<maxSize;++i)
{
array[i] = T();
}
i = 0;
iend -= maxSize;
}
for (;i<iend;++i)
{
array[i] = T();
}
head = iend;
}
else
{
head = (head + outsize) % maxSize;
}
return outsize;
}
template<class T, class OutputIterator>
unsigned OneThreadPerEnd::pop(T array[], unsigned maxSize, unsigned /*maxCapacity*/, OutputIterator out, unsigned outmaxsize, bool clear)
{
const unsigned currentSize = size(maxSize);
const unsigned outsize = (currentSize < outmaxsize) ? currentSize : outmaxsize;
if (outsize == 0)
{
// queue is empty
return 0;
}
// copy values + optional clear
{
unsigned i = head;
unsigned iend = head + outsize;
if (iend >= maxSize)
{
for (;i<maxSize;++i)
{
*out = array[i];
++out;
if (clear)
{
array[i] = T();
}
}
i = 0;
iend -= maxSize;
}
for (;i<iend;++i)
{
*out = array[i];
++out;
if (clear)
{
array[i] = T();
}
}
head = iend;
}
return outsize;
}
inline SOFA_HELPER_API ManyThreadsPerEnd::ManyThreadsPerEnd()
: head(0), tail(0)
{
}
inline SOFA_HELPER_API bool ManyThreadsPerEnd::isEmpty(unsigned /*maxSize*/) const
{
return head == tail;
}
inline SOFA_HELPER_API bool ManyThreadsPerEnd::isFull(unsigned maxSize) const
{
return tail-head >= (int)maxSize;
}
inline SOFA_HELPER_API unsigned ManyThreadsPerEnd::size(unsigned /*maxSize*/) const
{
return tail - head;
}
inline SOFA_HELPER_API void ManyThreadsPerEnd::init(AtomicInt array[], int maxCapacity)
{
for(int i = 0; i < maxCapacity; ++i)
{
array[i] = -1;
}
}
inline SOFA_HELPER_API bool ManyThreadsPerEnd::pop(AtomicInt array[], int maxSize, int maxCapacity, AtomicInt& item, bool /*clear*/)
{
if(isEmpty(maxSize))
{
std::this_thread::yield();
return false;
}
// atomically reserve the element to read
const int readIdx = head.fetch_add(1) & (maxCapacity-1); // maxCapacity is assumed to be a power of 2
// Active wait:
// loop as long as other threads have not put any valid value in the element.
// It happens when the queue is temporarily empty.
do
{
item.exchange(array[readIdx]);
}
while(item == -1);
//while((item = array[readIdx]) == -1)
// mark the element as available
array[readIdx] = -1;
return true;
}
inline SOFA_HELPER_API bool ManyThreadsPerEnd::push(AtomicInt array[], int maxSize, int maxCapacity, const AtomicInt& item)
{
if(isFull(maxSize))
{
std::this_thread::yield();
return false;
}
// atomically reserve the element to write
const int writeIdx = tail.fetch_add(1) & (maxCapacity-1); // maxCapacity is assumed to be a power of 2
// Active wait:
// loop as long as the element has not been read by another thread (which is indicated by a -1 value).
// It happens when the queue is temporarily full.
int refInt;
do
{
refInt = -1;
}
while(!array[writeIdx].compare_exchange_strong(refInt, item) );
//while(array[writeIdx].compare_and_swap(-1, item) != -1)
return true;
}
inline SOFA_HELPER_API unsigned ManyThreadsPerEnd::skip(AtomicInt array[], int maxSize, int maxCapacity, unsigned outmaxsize, bool clear)
{
// NOT OPTIMIZED
unsigned outsize = 0;
AtomicInt tmp;
while (outsize < outmaxsize && pop(array, maxSize, maxCapacity, tmp, clear))
{
++outsize;
}
return outsize;
}
template<class OutputIterator>
unsigned ManyThreadsPerEnd::pop(AtomicInt array[], int maxSize, int maxCapacity, OutputIterator out, unsigned outmaxsize, bool clear)
{
// NOT OPTIMIZED
unsigned outsize = 0;
while (outsize < outmaxsize && pop(array, maxSize, maxCapacity, *out, clear))
{
++out;
++outsize;
}
return outsize;
}
} // namespace sofa::helper::system::thread