This repository has been archived by the owner on Jan 18, 2019. It is now read-only.
forked from cksystemsgroup/scal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_queue.h
278 lines (240 loc) · 6.82 KB
/
segment_queue.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
// Copyright (c) 2012, the Scal project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Implementing the segment queue from:
//
// Y. Afek, G. Korland, and E. Yanovsky. Quasi-linearizability: Relaxed
// consistency for improved concurrency. In Proc. Conference on Principles of
// Distributed Systems (OPODIS), pages 395–410. Springer, 2010.
#ifndef SCAL_DATASTRUCTURES_SEGMENT_QUEUE_H_
#define SCAL_DATASTRUCTURES_SEGMENT_QUEUE_H_
#include <inttypes.h>
#include "datastructures/queue.h"
#include "util/allocation.h"
#include "util/atomic_value_new.h"
#include "util/random.h"
namespace scal {
namespace detail {
template<typename T>
class Pair : public ThreadLocalMemory<64> {
public:
_always_inline explicit Pair(T value)
: deleted(false) {
this->value = value;
}
T value;
bool deleted;
};
template<typename T>
class Node : public ThreadLocalMemory<64> {
public:
typedef TaggedValue<Node*> NodePtr;
typedef AtomicTaggedValue<Node*, 0, 64> AtomicNodePtr;
_always_inline Node(uint64_t s)
: segment(static_cast<Pair<T>**>(
ThreadLocalAllocator::Get().CallocAligned(
s, sizeof(Pair<T>*), 64)))
, next_(NodePtr(NULL, 0)) {
for (uint64_t i = 0; i < s; i++) {
segment[i] = new Pair<T>((T)NULL);
}
}
_always_inline NodePtr next() { return next_.load(); }
_always_inline bool atomic_set_next(
const NodePtr& old_next, const NodePtr& new_next) {
return next_.swap(old_next, new_next);
}
_always_inline T item(uint64_t index) {
return segment[index]->value;
}
_always_inline bool atomic_set_item(
uint64_t index, T old_item, T new_item) {
return __sync_bool_compare_and_swap(
&segment[index]->value, old_item, new_item);
}
_always_inline bool mark_deleted(uint64_t index) {
return __sync_bool_compare_and_swap(
&segment[index]->deleted, false, true);
}
private:
Pair<T>** segment;
AtomicNodePtr next_;
};
} // namespace detail
template<typename T>
class SegmentQueue : public Queue<T> {
public:
explicit SegmentQueue(uint64_t s);
bool enqueue(T item);
bool dequeue(T* item);
private:
typedef detail::Node<T> Node;
typedef typename detail::Node<T>::NodePtr NodePtr;
typedef AtomicTaggedValue<Node*, 4*128, 4*128> AtomicNodePtr;
NodePtr get_tail();
NodePtr get_head();
void tail_segment_create(const typename detail::Node<T>::NodePtr& my_tail);
void head_segment_remove(const typename detail::Node<T>::NodePtr& my_head);
AtomicNodePtr* head_;
AtomicNodePtr* tail_;
uint64_t s_;
};
template<typename T>
SegmentQueue<T>::SegmentQueue(uint64_t s)
: s_(s) {
const NodePtr new_node(new Node(s), 0);
head_ = new AtomicNodePtr(new_node);
tail_ = new AtomicNodePtr(new_node);
}
template<typename T>
bool SegmentQueue<T>::enqueue(T item) {
NodePtr tail = get_tail();
while (tail.value() == NULL) {
tail_segment_create(tail);
tail = get_tail();
}
uint64_t item_index;
uint64_t rand;
while (true) {
rand = hwrand() % s_;
for (uint64_t i = 0; i <= s_; i++) {
item_index = (i + rand) % s_;
if (tail.value()->item(item_index) != (T)NULL) { // optimization
continue;
}
if (tail.value()->atomic_set_item(item_index, (T)NULL, item)) {
return true;
}
}
do {
tail_segment_create(tail);
tail = get_tail();
} while (tail.value() == NULL);
}
}
template<typename T>
bool SegmentQueue<T>::dequeue(T* item) {
NodePtr head;
uint64_t rand;
uint64_t item_index;
bool found_null = false;
while (true) {
head = get_head();
if (head.value() == NULL) {
return false;
}
rand = hwrand() % s_;
for (uint64_t i = 0; i < s_; i++) {
item_index = (i + rand) % s_;
if (head.value()->item(item_index) == (T)NULL) {
found_null = true;
continue;
}
if (head.value()->mark_deleted(item_index)) {
*item = head.value()->item(item_index);
return true;
}
}
if (found_null) {
return false;
}
head_segment_remove(head);
}
}
template<typename T>
typename detail::Node<T>::NodePtr SegmentQueue<T>::get_tail() {
NodePtr next;
NodePtr head_old;
NodePtr tail_old;
while (true) {
head_old = head_->load();
tail_old = tail_->load();
next = tail_old.value()->next();
if ((tail_old == tail_->load()) &&
(head_old == head_->load())) {
if (next.value() == NULL) {
if (head_old == tail_old) {
return NodePtr(NULL, tail_old.tag()); // only sentinel left
}
return tail_old;
}
NodePtr tmp(next.value(), tail_old.tag());
tail_->swap(tail_old, tmp);
}
}
}
template<typename T>
typename detail::Node<T>::NodePtr SegmentQueue<T>::get_head() {
NodePtr next;
NodePtr head_old;
NodePtr tail_old;
while (true) {
head_old = head_->load();
tail_old = tail_->load();
next = head_old.value()->next();
if (head_old == head_->load()) {
if (head_old == tail_old) {
if (next.value() == NULL) {
return NodePtr(NULL, 0);
}
}
return next;
}
}
}
template<typename T>
void SegmentQueue<T>::tail_segment_create(
const typename detail::Node<T>::NodePtr& my_tail) {
NodePtr tail_old;
NodePtr next;
while (true) {
tail_old = tail_->load();
next = tail_old.value()->next();
if (tail_old == tail_->load()) {
if (next.value() == NULL) {
if ((my_tail.value() == NULL && my_tail.tag() == tail_old.tag()) ||
tail_old == my_tail) {
const NodePtr new_next(new Node(s_), 0);
if (tail_old.value()->atomic_set_next(next, new_next)) {
break;
}
}
return; // somebody else made it
} else {
const NodePtr tmp(next.value(), tail_old.tag() + 1);
tail_->swap(tail_old, tmp);
return;
}
}
}
}
template<typename T>
void SegmentQueue<T>::head_segment_remove(
const typename detail::Node<T>::NodePtr& my_head) {
NodePtr head_old;
NodePtr tail_old;
NodePtr next;
while (true) {
head_old = head_->load();
tail_old = tail_->load();
next = head_old.value()->next();
if (head_old == head_->load()) {
if (head_old == tail_old) {
if (next.value() == NULL) {
return; // We don't remove sentinel node.
}
NodePtr new_tail(next.value(), tail_old.tag() + 1);
tail_->swap(tail_old, new_tail);
} else {
if (next == my_head) {
// Still the same, let's push the sentinel by one.
NodePtr new_head(next.value(), head_old.tag() + 1);
head_->swap(head_old, new_head);
}
return; // someone else made it
}
}
}
}
} // namespace scal
#endif // SCAL_DATASTRUCTURES_SEGMENT_QUEUE_H_