-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadedQueue.h
328 lines (267 loc) · 7.87 KB
/
ThreadedQueue.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
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
/**
* @file task_graph.h
* @brief This file contains the implementation of the queues used in the system.
*
*/
#ifndef MCRINGBUFFER_H_
#define MCRINGBUFFER_H_
#include "mtsp.h"
#include <stdio.h>
#include <stdlib.h>
/// for padding purposes
#define L1D_LINE_SIZE 64
#define USED 1
#define EMPTY 0
#define Q_LOCKED 1
#define Q_UNLOCKED 0
#define GET_LOCK(ptr) while (__sync_bool_compare_and_swap(ptr, Q_UNLOCKED, Q_LOCKED) == false)
#define RLS_LOCK(ptr) __sync_bool_compare_and_swap(ptr, Q_LOCKED, Q_UNLOCKED)
/**
* This is an implementation of the MCRingBuffer Single-Producer Single-Consumer
* multi threaded queue. So all this is to say that there is only one thread
* producing items and only one thread consuming them. This queue (algorithm)
* is optimized because it does not require the use of locks and also because
* it use paddings and batches to prevent false sharing when accessing the
* queue items.
*/
template <typename T, int QUEUE_SIZE, int BATCH_SIZE, int CONT_FACTOR=100>
class SPSCQueue {
private:
// shared control variables
volatile int read;
volatile int write;
char pad1[L1D_LINE_SIZE - 2*sizeof(int)];
// consumer local control variables
int localWrite;
int nextRead;
char pad2[L1D_LINE_SIZE - 2*sizeof(int)];
// producer local control variables
int localRead;
int nextWrite;
char pad3[L1D_LINE_SIZE - 2*sizeof(int)];
// Circular buffer to insert elements
// all position of the queue are already initialized
T elems[QUEUE_SIZE];
public:
SPSCQueue() {
read = 0;
write = 0;
localRead = 0;
localWrite = 0;
nextRead = 0;
nextWrite = 0;
/// The size of the queue must be a power of 2
if (QUEUE_SIZE == 0 || (QUEUE_SIZE & (QUEUE_SIZE - 1)) != 0) {
printf("CRITICAL: The size of the queue must be a power of 2.\n");
exit(-1);
}
}
/**
* Adds one element to the back of the queue. If the queue is full this
* method will busy wait until a free entry is available.
*
* @param elem The element to be added to the queue.
*/
void enq(T elem) {
//// __itt_task_begin(__itt_mtsp_domain, __itt_null, __itt_null, __itt_SPSC_Enq);
int afterNextWrite = nextWrite + 1;
afterNextWrite &= (QUEUE_SIZE - 1);
if (afterNextWrite == localRead) {
//// __itt_task_begin(__itt_mtsp_domain, __itt_null, __itt_null, __itt_SPSC_Enq_Blocking);
while (afterNextWrite == read) ;
localRead = read;
//// __itt_task_end(__itt_mtsp_domain);
}
elems[nextWrite] = elem;
nextWrite = afterNextWrite;
if ((nextWrite & (BATCH_SIZE-1)) == 0)
write = nextWrite;
//// __itt_task_end(__itt_mtsp_domain);
}
/**
* Returns true if we can enqueue at least one more item on the queue.
* Actually it check to see if after the next enqueue the queue will be full.
* It updates the \c localRead if that would be true.
*/
bool can_enq() {
return ((nextWrite+1) != localRead || ((localRead = read) != (nextWrite + 1)));
}
/**
* Remove one element from the front of the queue. If the queue is empty this
* method will busy wait until one element is available.
*
* @return The element from the front of the queue.
*/
T deq() {
//// __itt_task_begin(__itt_mtsp_domain, __itt_null, __itt_null, __itt_SPSC_Deq);
if (nextRead == localWrite) {
//// __itt_task_begin(__itt_mtsp_domain, __itt_null, __itt_null, __itt_SPSC_Deq_Blocking);
//printf("Blocked, but Will not steal: cur_load = %d, cont_load = %d\n", cur_load(), cont_load());
while (nextRead == write) ;
localWrite = write;
//// __itt_task_end(__itt_mtsp_domain);
}
T data = elems[nextRead];
nextRead += 1;
nextRead &= (QUEUE_SIZE-1);
if ((nextRead & (BATCH_SIZE-1)) == 0)
read = nextRead;
//// __itt_task_end(__itt_mtsp_domain);
return data;
}
/**
* Returns true if there is any item in the queue ready to be dequeued
* We check to see if there is something left in the current batch visible
* to the consumer. If there is not then we check if the producer has
* already produced new items.
*
* (nextRead != localWrite) ==> where I would read is where the producer
* was about to write a next item?
* (nextRead != write) ==> has the producer produced new items yet?
*/
bool can_deq() {
return (nextRead != localWrite || ((localWrite = write) != nextRead));
}
/**
* If the queue is not empty returns in \c elem the element from the front of the
* queue and the function returns true. If the queue is empty the parameter \c elem
* is not changed and the function return false.
*/
bool try_deq(T* elem) {
if (can_deq()) {
*elem = deq();
return true;
}
else {
return false;
}
}
/**
* Returns the number of items currently in the queue
*/
int cur_load() {
int w = write, r = read;
if (w-r >= 0)
return w-r;
else
return (QUEUE_SIZE - r) + w;
}
/**
* This returns the number of items that represents the
* contention factor of this queue.
*/
int cont_load() {
double cf = ((double)CONT_FACTOR / 100.0);
return (int)(QUEUE_SIZE * cf);
}
/**
* This is used to flush items pending in the current batch. It
* is required because this queue update the indexes in batch mode to
* prevent false sharing. However, if for some reason (i.e., there
* is no more items) a batch needs to be prematurely finished this
* function can be called.
*/
void fsh() {
write = nextWrite;
read = nextRead;
}
};
/**
* This is an multi threaded inter core queue implementation that can handle
* multiple producers and/or multiple consumers. To cope with that it uses locks,
* but currently these locks are based on atomic directives.
*/
template <typename T, int QUEUE_SIZE, int CONT_FACTOR=100>
class SimpleQueue {
private:
volatile unsigned int head;
volatile unsigned int tail;
volatile T* data;
volatile bool* status;
volatile bool rlock;
volatile bool wlock;
public:
SimpleQueue() {
if (QUEUE_SIZE <= 0 || (QUEUE_SIZE & (QUEUE_SIZE-1)) != 0) {
printf("Queue size is not a power of 2! [%s, %d]\n", __FILE__, __LINE__);
exit(-1);
}
data = new T[QUEUE_SIZE];
status = new bool[QUEUE_SIZE];
for (int i=0; i<QUEUE_SIZE; i++)
status[i] = EMPTY;
rlock = Q_UNLOCKED;
wlock = Q_UNLOCKED;
head = 0;
tail = 0;
}
/**
* Adds one element to the back of the queue. If the queue is full this
* method will busy wait until a free entry is available.
*
* @param elem The element to be added to the queue.
*/
void enq(T elem) {
GET_LOCK(&wlock);
while (status[tail] != EMPTY);
data[tail] = elem;
status[tail] = USED;
tail = ((tail+1) & (QUEUE_SIZE-1));
RLS_LOCK(&wlock);
}
/**
* Remove one element from the front of the queue. If the queue is empty this
* method will busy wait until one element is available.
*
* @return The element from the front of the queue.
*/
T deq() {
GET_LOCK(&rlock);
while (status[head] == EMPTY);
T elem = data[head];
status[head] = EMPTY;
head = ((head+1) & (QUEUE_SIZE-1));
RLS_LOCK(&rlock);
return elem;
}
/**
* If the queue is not empty returns in \c elem the element from the front of the
* queue and the function returns true. If the queue is empty the parameter \c elem
* is not changed and the function return false.
*/
bool try_deq(T* elem) {
GET_LOCK(&rlock);
if (status[head] != EMPTY) {
//while (status[head] == EMPTY);
*elem = data[head];
status[head] = EMPTY;
head = ((head+1) & (QUEUE_SIZE-1));
RLS_LOCK(&rlock);
return true;
}
RLS_LOCK(&rlock);
return false;
}
/**
* Returns the number of items currently in the queue
*/
int cur_load() {
if (tail-head >= 0)
return tail-head;
else
return (QUEUE_SIZE - head) + tail;
}
/**
* This returns the number of items that represents the
* contention factor of this queue.
*/
int cont_load() {
return QUEUE_SIZE * ((double)CONT_FACTOR / 100.0);
}
~SimpleQueue() {
// For now this is the safest option
//delete [] data;
//delete [] status;
}
};
#endif /* MCRINGBUFFER_H_ */