-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlock_free_impl.hpp
349 lines (308 loc) · 7.14 KB
/
lock_free_impl.hpp
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#pragma once
#include <cassert>
#include <memory>
#include <iterator>
#include "atomic_reference.hpp"
#include "macros.hpp"
namespace private_ {
struct nop_scoper {
template <typename T>
inline void release(T *) const {}
};
}
/**
* Lock-free singly-linked list implemention, with configurable ref counting
* and garbage collection policies
*
* References returned by this implementation are guaranteed to be valid until
* the element is removed from the list
*/
template <typename T,
typename RefPtrLockImpl = spinlock,
typename RefCountImpl = atomic_ref_counted,
typename ScopedImpl = private_::nop_scoper>
class lock_free_impl {
private:
struct node;
typedef atomic_ref_ptr<node, RefPtrLockImpl> node_ptr;
struct node : public RefCountImpl {
// non-copyable
node(const node &) = delete;
node(node &&) = delete;
node &operator=(const node &) = delete;
node() : value_(), next_() {}
node(const T &value, const node_ptr &next)
: value_(value), next_(next) {}
~node()
{
// sanity check
assert(next_.get_mark());
}
T value_;
node_ptr next_;
inline bool
is_marked() const
{
return next_.get_mark();
}
};
node_ptr head_; // head_ points to a sentinel beginning node
mutable node_ptr tail_; // tail_ is maintained loosely
struct iterator_ : public std::iterator<std::forward_iterator_tag, T> {
iterator_() : node_(), scoper_() {}
iterator_(const node_ptr &node)
: node_(node), scoper_() {}
typedef T value_type;
T &
operator*() const
{
// could return deleted value
return node_->value_;
}
T *
operator->() const
{
// could return deleted value
return &node_->value_;
}
bool
operator==(const iterator_ &o) const
{
return node_ == o.node_;
}
bool
operator!=(const iterator_ &o) const
{
return !operator==(o);
}
iterator_ &
operator++()
{
do {
node_ = node_->next_;
} while (node_ && node_->is_marked());
return *this;
}
iterator_
operator++(int)
{
iterator_ cur = *this;
++(*this);
return cur;
}
node_ptr node_;
ScopedImpl scoper_;
};
public:
typedef iterator_ iterator;
lock_free_impl() : head_(new node), tail_(head_) {}
~lock_free_impl()
{
ScopedImpl scoper;
// can do this non-thread safe, since we know there are
// no other mutators
node_ptr cur = head_;
while (cur) {
if (cur->next_.mark())
scoper.release(cur.get());
cur = cur->next_;
}
}
size_t
size() const
{
ScopedImpl scoper UNUSED;
assert(!head_->is_marked());
size_t ret = 0;
node_ptr cur = head_->next_;
while (cur) {
if (!cur->is_marked()) {
ret++;
} else {
// XXX: reap cur for garbage collection
}
if (!cur->next_ && !cur->is_marked() && tail_ != cur)
tail_ = cur;
cur = cur->next_;
}
return ret;
}
T &
front()
{
retry:
ScopedImpl scoper UNUSED;
assert(!head_->is_marked());
node_ptr p = head_->next_;
assert(p);
if (p->is_marked())
// XXX: reap p for garbage collection
goto retry;
T &ref = p->value_;
if (p->is_marked())
// XXX: reap p for garbage collection
goto retry;
// we have stability on a reference
if (!p->next_ && tail_ != p)
tail_ = p;
return ref;
}
inline const T &
front() const
{
return const_cast<lock_free_impl *>(this)->front();
}
T &
back()
{
retry:
ScopedImpl scoper UNUSED;
assert(!head_->is_marked());
node_ptr tail = tail_;
assert(tail);
while (tail->next_) {
tail = tail->next_;
if (!tail)
goto retry;
}
if (tail->is_marked()) { // hopefully rare
// XXX: reap p for garbage collection
fix_tail_pointer_from_head();
goto retry;
}
tail_ = tail;
T &ref = tail->value_;
if (tail->is_marked()) { // see above
// XXX: reap p for garbage collection
fix_tail_pointer_from_head();
goto retry;
}
// we have stability on a reference
return ref;
}
inline const T &
back() const
{
return const_cast<lock_free_impl *>(this)->back();
}
void
pop_front()
{
retry:
ScopedImpl scoper;
assert(!head_->is_marked());
node_ptr cur = head_->next_;
assert(cur);
if (!cur->next_.mark())
// was concurrently deleted
goto retry;
// we don't need to CAS the prev ptr here, because we know that the
// sentinel node will never be deleted (that is, the first node of a list
// will *always* be the first node)
head_->next_ = cur->next_; // semantics of assign() do not copy marked bits
if (!cur->next_ && tail_ != head_)
tail_ = head_;
assert(cur->is_marked());
scoper.release(cur.get());
}
void
push_back(const T &val)
{
retry:
ScopedImpl scoper;
assert(!head_->is_marked());
node_ptr tail = tail_;
assert(tail);
while (tail->next_) {
tail = tail->next_;
if (!tail)
goto retry;
}
if (tail->is_marked()) { // hopefully rare
fix_tail_pointer_from_head();
goto retry;
}
node_ptr n(new node(val, node_ptr()));
if (!tail->next_.compare_exchange_strong(node_ptr(), n)) {
bool ret = n->next_.mark(); // be pedantic
if (!ret) assert(false);
scoper.release(n.get());
goto retry;
}
tail_ = n;
}
inline void
remove(const T &val)
{
ScopedImpl scoper;
node_ptr prev = head_;
node_ptr p = head_->next_, *pp = &head_->next_;
while (p) {
if (p->value_ == val) {
// mark removed
if (p->next_.mark()) {
// try to unlink- ignore success value
if (pp->compare_exchange_strong(p, p->next_)) {
// successful unlink, report
assert(p->is_marked());
scoper.release(p.get());
}
if (!p->next_)
tail_ = prev;
}
// in any case, advance the current ptr, but keep the
// prev ptr the same
p = p->next_;
} else {
prev = p;
pp = &p->next_;
p = p->next_;
}
}
}
std::pair<bool, T>
try_pop_front()
{
retry:
ScopedImpl scoper;
assert(!head_->is_marked());
node_ptr cur = head_->next_;
if (unlikely(!cur))
return std::make_pair(false, T());
if (!cur->next_.mark())
// was concurrently deleted
goto retry;
T t = cur->value_;
head_->next_ = cur->next_; // semantics of assign() do not copy marked bits
if (!cur->next_ && tail_ != head_)
tail_ = head_;
assert(cur->is_marked());
scoper.release(cur.get());
return std::make_pair(true, t);
}
iterator
begin()
{
ScopedImpl scoper UNUSED;
return iterator_(head_->next_);
}
iterator
end()
{
return iterator_(node_ptr());
}
private:
// relatively expensive, should be avoided
void
fix_tail_pointer_from_head() const
{
node_ptr cur = head_->next_;
node_ptr prev = head_;
while (cur) {
prev = cur;
cur = cur->next_;
}
assert(prev);
tail_ = prev;
}
};