forked from cksystemsgroup/scal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boundedsize_kfifo.h
285 lines (249 loc) · 8.55 KB
/
boundedsize_kfifo.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
// Copyright (c) 2012-2013, the Scal Project Authors. All rights reserved.
// Please see the AUTHORS file for details. Use of this source code is governed
// by a BSD license that can be found in the LICENSE file.
// Implementing the queue from:
//
// C.M. Kirsch, M. Lippautz, and H. Payer. Fast and Scalable k-FIFO Queues.
// Technical Report 2012-04, Department of Computer Sciences, University of
// Salzburg, June 2012.
#ifndef SCAL_DATASTRUCTURES_BOUNDEDSIZE_KFIFO_H_
#define SCAL_DATASTRUCTURES_BOUNDEDSIZE_KFIFO_H_
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "datastructures/queue.h"
#include "util/allocation.h"
#include "util/atomic_value_new.h"
#include "util/platform.h"
#include "util/random.h"
#define BSKFIFO_WASTE 1
#ifdef BSKFIFO_WASTE
#define PTR_ALIGNMENT (4096)
#define ITEM_PAD (128 * 4)
#else
#define PTR_ALIGNMENT (128)
#define ITEM_PAD (128)
#endif // BSKFIFO_WASTE
namespace scal {
template<typename T>
class BoundedSizeKFifo : public Queue<T> {
public:
BoundedSizeKFifo(uint64_t k, uint64_t num_segments);
bool enqueue(T item);
bool dequeue(T *item);
private:
typedef TaggedValue<uint64_t> SegmentPtr;
typedef AtomicTaggedValue<uint64_t, PTR_ALIGNMENT, 128> AtomicSegmentPtr;
typedef TaggedValue<T> Item;
typedef AtomicTaggedValue<T, 0, ITEM_PAD> AtomicItem;
_always_inline bool find_index(
uint64_t start_index, bool empty, int64_t *item_index, Item* old);
_always_inline bool advance_head(const SegmentPtr& head_old);
_always_inline bool advance_tail(const SegmentPtr& tail_old);
_always_inline bool queue_full(
const SegmentPtr& head_old, const SegmentPtr& tail_old);
_always_inline bool segment_not_empty(const SegmentPtr& head_old);
_always_inline bool not_in_valid_region(uint64_t tail_old_pointer,
uint64_t tail_current_pointer,
uint64_t head_current_pointer);
_always_inline bool in_valid_region(uint64_t tail_old_pointer,
uint64_t tail_current_pointer,
uint64_t head_current_pointer);
_always_inline bool committed(
const SegmentPtr& tail_old, const Item& new_item, uint64_t item_index);
uint64_t queue_size_;
size_t k_;
AtomicSegmentPtr* head_;
AtomicSegmentPtr* tail_;
AtomicItem* queue_;
uint8_t pad_[
128
- sizeof(queue_size_)
- sizeof(k_)
- sizeof(head_)
- sizeof(tail_)
- sizeof(queue_)];
};
template<typename T>
BoundedSizeKFifo<T>::BoundedSizeKFifo(uint64_t k, uint64_t num_segments)
: queue_size_(k * num_segments)
, k_(k)
, head_(new AtomicSegmentPtr())
, tail_(new AtomicSegmentPtr())
, queue_(static_cast<AtomicItem*>(
CallocAligned(k * num_segments, sizeof(AtomicItem), 64))) {
}
template<typename T>
bool BoundedSizeKFifo<T>::find_index(
uint64_t start_index, bool empty, int64_t *item_index, Item* old) {
const uint64_t random_index = pseudorand() % k_;
uint64_t index;
for (size_t i = 0; i < k_; i++) {
index = (start_index + ((random_index + i) % k_)) % queue_size_;
*old = queue_[index].load();
if ((empty && old->value() == (T)NULL)
|| (!empty && old->value() != (T)NULL)) {
*item_index = index;
return true;
}
}
return false;
}
template<typename T>
bool BoundedSizeKFifo<T>::advance_head(const SegmentPtr& head_old) {
return head_->swap(
head_old, SegmentPtr((head_old.value() + k_) % queue_size_, head_old.tag() + 1));
}
template<typename T>
bool BoundedSizeKFifo<T>::advance_tail(const SegmentPtr& tail_old) {
return tail_->swap(
tail_old, SegmentPtr((tail_old.value() + k_) % queue_size_, tail_old.tag() + 1));
}
template<typename T>
bool BoundedSizeKFifo<T>::queue_full(
const SegmentPtr& head_old, const SegmentPtr& tail_old) {
if (((tail_old.value() + k_) % queue_size_) == head_old.value() &&
(head_old.value() == head_->load().value())) {
return true;
}
return false;
}
template<typename T>
bool BoundedSizeKFifo<T>::segment_not_empty(const SegmentPtr& head_old) {
const uint64_t start = head_old.value();
for (size_t i = 0; i < k_; i++) {
if (queue_[(start + i) % queue_size_].load().value() != (T)NULL) {
return true;
}
}
return false;
}
template<typename T>
bool BoundedSizeKFifo<T>::in_valid_region(uint64_t tail_old_pointer,
uint64_t tail_current_pointer,
uint64_t head_current_pointer) {
bool wrap_around = (tail_current_pointer < head_current_pointer)
? true : false;
if (!wrap_around) {
return (head_current_pointer < tail_old_pointer
&& tail_old_pointer <= tail_current_pointer) ? true : false;
}
return (head_current_pointer < tail_old_pointer
|| tail_old_pointer <= tail_current_pointer) ? true : false;
}
template<typename T>
bool BoundedSizeKFifo<T>::not_in_valid_region(uint64_t tail_old_pointer,
uint64_t tail_current_pointer,
uint64_t head_current_pointer) {
bool wrap_around = (tail_current_pointer < head_current_pointer)
? true : false;
if (!wrap_around) {
return (tail_old_pointer < tail_current_pointer
|| head_current_pointer < tail_old_pointer) ? true : false;
}
return (tail_old_pointer < tail_current_pointer
&& head_current_pointer < tail_old_pointer) ? true : false;
}
template<typename T>
bool BoundedSizeKFifo<T>::committed(const SegmentPtr& tail_old,
//uint64_t tail_old_pointer,
//AtomicValue<T> *new_item,
const Item& new_item,
uint64_t item_index) {
if (queue_[item_index].load() != new_item) {
return true;
}
SegmentPtr tail_current = tail_->load();
SegmentPtr head_current = head_->load();
if (in_valid_region(tail_old.value(), tail_current.value(),
head_current.value())) {
return true;
} else if (not_in_valid_region(tail_old.value(), tail_current.value(),
head_current.value())) {
if (!queue_[item_index].swap(
new_item, Item((T)NULL, new_item.tag() + 1))) {
return true;
}
} else {
if (head_->swap(
head_current, SegmentPtr(head_current.value(),
head_current.tag() + 1))) {
return true;
}
if (!queue_[item_index].swap(
new_item, Item((T)NULL, new_item.tag() + 1))) {
return true;
}
}
return false;
}
template<typename T>
bool BoundedSizeKFifo<T>::dequeue(T *item) {
SegmentPtr tail_old;
SegmentPtr head_old;
int64_t item_index;
Item old_item;
bool found_idx;
while (true) {
head_old = head_->load();
tail_old = tail_->load();
found_idx = find_index(head_old.value(), false, &item_index, &old_item);
if (head_old == head_->load()) {
if (found_idx) {
if (head_old.value() == tail_old.value()) {
advance_tail(tail_old);
}
if (queue_[item_index].swap(
old_item, Item((T)NULL, old_item.tag() + 1))) {
*item = old_item.value();
return true;
}
} else {
if ((head_old.value() == tail_old.value()) &&
(tail_old.value() == tail_->load().value())) {
return false;
}
advance_head(head_old);
}
}
}
}
template<typename T>
bool BoundedSizeKFifo<T>::enqueue(T item) {
TaggedValue<T>::CheckCompatibility(item);
if (item == (T)NULL) {
printf("%s: unable to enqueue NULL or equivalent value\n", __func__);
abort();
}
SegmentPtr tail_old;
SegmentPtr head_old;
int64_t item_index;
Item old_item;
bool found_idx;
while (true) {
tail_old = tail_->load();
head_old = head_->load();
found_idx = find_index(tail_old.value(), true, &item_index, &old_item);
if (tail_old == tail_->load()) {
if (found_idx) {
const Item new_item(item, old_item.tag() + 1);
if (queue_[item_index].swap(old_item, new_item)) {
if (committed(tail_old, new_item, item_index)) {
return true;
}
}
} else {
if (queue_full(head_old, tail_old)) {
if (segment_not_empty(head_old) &&
(head_old.value() == head_->load().value())) {
return false;
}
advance_head(head_old);
}
advance_tail(tail_old);
}
}
}
}
} // namespace scal
#endif // SCAL_DATASTRUCTURES_BOUNDEDSIZE_KFIFO_H_