-
Notifications
You must be signed in to change notification settings - Fork 28
/
unboundedsize_kfifo.h
314 lines (276 loc) · 9.31 KB
/
unboundedsize_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
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
// 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 k-relaxed queue from:
//
// C.M. Kirsch, M. Lippautz, and H. Payer. Fast and scalable, lock-free k-fifo
// queues. In Proc. International Conference on Parallel Computing Technologies
// (PaCT), LNCS, pages 208-223. Springer, October 2013.
#ifndef SCAL_DATASTRUCTURES_UNBOUNDEDSIZE_KFIFO_H_
#define SCAL_DATASTRUCTURES_UNBOUNDEDSIZE_KFIFO_H_
#include <inttypes.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"
#include "util/threadlocals.h"
namespace scal {
namespace detail {
template<typename T>
class KSegment : public ThreadLocalMemory<64> {
public:
typedef TaggedValue<T> Item;
typedef AtomicTaggedValue<T, 0, 4 * 128> AtomicItem;
typedef TaggedValue<KSegment*> SegmentPtr;
typedef AtomicTaggedValue<KSegment*, 0, 4 * 128> AtomicSegmentPtr;
_always_inline explicit KSegment(uint64_t k)
: deleted_(0)
, k_(k)
, next_(SegmentPtr(NULL, 0))
, items_(static_cast<AtomicItem*>(
ThreadLocalAllocator::Get().CallocAligned(
k, sizeof(AtomicItem), 64))) {
}
_always_inline uint64_t k() { return k_; }
_always_inline uint8_t deleted() { return deleted_; }
_always_inline void set_deleted() { deleted_ = 1; }
_always_inline SegmentPtr next() { return next_.load(); }
_always_inline bool atomic_set_next(
const SegmentPtr& old_next, const SegmentPtr& new_next) {
return next_.swap(old_next, new_next);
}
_always_inline Item item(uint64_t index) {
__builtin_prefetch(&items_[index + 1], 0, 0);
return items_[index].load();
}
_always_inline bool atomic_set_item(
uint64_t index, const Item& old_item, const Item& new_item) {
return items_[index].swap(old_item, new_item);
}
private:
uint8_t deleted_;
uint8_t pad_[63];
uint64_t k_;
AtomicSegmentPtr next_;
AtomicItem* items_;
};
} // namespace detail
template<typename T>
class UnboundedSizeKFifo : public Queue<T> {
public:
explicit UnboundedSizeKFifo(uint64_t k);
bool enqueue(T item);
bool dequeue(T *item);
private:
typedef detail::KSegment<T> KSegment;
typedef typename KSegment::SegmentPtr SegmentPtr;
typedef typename KSegment::Item Item;
typedef typename KSegment::AtomicItem AtomicItem;
typedef AtomicTaggedValue<KSegment*, 4096, 4096> AtomicSegmentPtr;
_always_inline void advance_head(const SegmentPtr& head_old);
_always_inline void advance_tail(const SegmentPtr& tail_old);
_always_inline bool find_index(
KSegment* const start_index, bool empty, int64_t *item_index, Item* old);
_always_inline bool committed(
const SegmentPtr& tail_old, const Item& new_item, uint64_t item_index);
#ifdef LOCALLY_LINEARIZABLE
struct Marker {
SegmentPtr value;
uint8_t padding[64 - sizeof(SegmentPtr)];
};
_always_inline void SetLastSegment(SegmentPtr tail) {
markers[ThreadContext::get().thread_id()].value = tail;
}
_always_inline bool IsLastSegment(SegmentPtr tail) {
return markers[ThreadContext::get().thread_id()].value == tail;
}
Marker markers[kMaxThreads];
#endif // LOCALLY_LINEARIZABLE
AtomicSegmentPtr* head_;
AtomicSegmentPtr* tail_;
uint64_t k_;
};
template<typename T>
UnboundedSizeKFifo<T>::UnboundedSizeKFifo(uint64_t k)
: k_(k) {
const SegmentPtr new_segment(new KSegment(k_), 0);
head_ = new AtomicSegmentPtr(new_segment);
tail_ = new AtomicSegmentPtr(new_segment);
}
template<typename T>
void UnboundedSizeKFifo<T>::advance_head(const SegmentPtr& head_old) {
const SegmentPtr head_current = head_->load();
if (head_current == head_old) {
const SegmentPtr tail_current = tail_->load();
const SegmentPtr tail_next_ksegment = tail_current.value()->next();
const SegmentPtr head_next_ksegment = head_current.value()->next();
if (head_current == head_->load()) {
if (head_current.value() == tail_current.value()) {
if (tail_next_ksegment.value() == NULL) {
return;
}
if (tail_current == tail_->load()) {
tail_->swap(tail_current, SegmentPtr(tail_next_ksegment.value(),
tail_current.tag() + 1));
}
}
head_old.value()->set_deleted();
head_->swap(
head_old, SegmentPtr(head_next_ksegment.value(), head_old.tag() + 1));
}
}
}
template<typename T>
void UnboundedSizeKFifo<T>::advance_tail(const SegmentPtr& tail_old) {
const SegmentPtr tail_current = tail_->load();
SegmentPtr next_ksegment;
if (tail_current == tail_old) {
next_ksegment = tail_old.value()->next();
if (tail_old == tail_->load()) {
if (next_ksegment.value() != NULL) {
tail_->swap(tail_old, SegmentPtr(next_ksegment.value(),
next_ksegment.tag() + 1));
} else {
KSegment* segment = new KSegment(k_);
const SegmentPtr new_ksegment(segment, next_ksegment.tag() + 1);
if (tail_old.value()->atomic_set_next(next_ksegment, new_ksegment)) {
tail_->swap(
tail_old, SegmentPtr(new_ksegment.value(), tail_old.tag() + 1));
} else {
delete segment;
}
}
}
}
}
template<typename T>
bool UnboundedSizeKFifo<T>::find_index(
detail::KSegment<T>* const start_index, bool empty, int64_t *item_index,
Item* old) {
const uint64_t k = start_index->k();
const uint64_t random_index = hwrand() % k;
uint64_t index;
for (size_t i = 0; i < k; i++) {
index = ((random_index + i) % k);
*old = start_index->item(index);
if ((empty && old->value() == (T)NULL)
|| (!empty && old->value() != (T)NULL)) {
*item_index = index;
return true;
}
}
return false;
}
template<typename T>
bool UnboundedSizeKFifo<T>::committed(
const SegmentPtr& tail_old, const Item& new_item, uint64_t item_index) {
if (tail_old.value()->item(item_index) != new_item) {
return true;
}
const SegmentPtr head_current = head_->load();
const Item empty_item((T)NULL, 0);
if (tail_old.value()->deleted() == true) {
// Insert tail segment has been removed.
if (!tail_old.value()->atomic_set_item(item_index, new_item, empty_item)) {
// We are fine if element still has been removed.
return true;
}
} else if (tail_old.value() == head_current.value()) {
// Insert tail segment is now head.
if (head_->swap(head_current,
SegmentPtr(head_current.value(), head_current.tag() + 1))) {
// We are fine if we can update head and thus fail any concurrent
// advance_head attempts.
return true;
}
if (!tail_old.value()->atomic_set_item(item_index, new_item, empty_item)) {
// We are fine if element still has been removed.
return true;
}
} else if (tail_old.value()->deleted() == false) {
// Insert tail segment still not deleted.
return true;
} else {
// Head and tail moved beyond this segment. Try to remove the item.
if (!tail_old.value()->atomic_set_item(item_index, new_item, empty_item)) {
// We are fine if element still has been removed.
return true;
}
}
return false;
}
template<typename T>
bool UnboundedSizeKFifo<T>::dequeue(T *item) {
SegmentPtr tail_old;
SegmentPtr head_old;
int64_t item_index = 0;
Item old_item;
bool found_idx;
while (true) {
head_old = head_->load();
found_idx = find_index(head_old.value(), false, &item_index, &old_item);
tail_old = tail_->load();
if (head_old == head_->load()) {
if (found_idx) {
if (head_old.value() == tail_old.value()) {
advance_tail(tail_old);
}
const Item newcp((T)NULL, old_item.tag() + 1);
if (head_old.value()->atomic_set_item(item_index, old_item, newcp)) {
*item = old_item.value();
return true;
}
} else {
if ((head_old.value() == tail_old.value()) &&
(tail_old == tail_->load())) {
return false;
}
advance_head(head_old);
}
}
}
}
template<typename T>
bool UnboundedSizeKFifo<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 = 0;
Item old_item;
bool found_idx;
while (true) {
tail_old = tail_->load();
#ifdef LOCALLY_LINEARIZABLE
if (IsLastSegment(tail_old)) {
advance_tail(tail_old);
continue;
}
#endif // LOCALLY_LINEARIZABLE
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 newcp(item, old_item.tag() + 1);
if (tail_old.value()->atomic_set_item(item_index, old_item, newcp)) {
if (committed(tail_old, newcp, item_index)) {
#ifdef LOCALLY_LINEARIZABLE
SetLastSegment(tail_old);
#endif // LOCALLY_LINEARIZABLE
return true;
}
}
} else {
advance_tail(tail_old);
}
}
}
}
} // namespace scal
#endif // SCAL_DATASTRUCTURES_UNBOUNDEDSIZE_KFIFO_H_