forked from stephentu/silo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pxqueue.h
354 lines (315 loc) · 8.13 KB
/
pxqueue.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#pragma once
#include <iterator>
#include <string>
#include "counter.h"
#include "macros.h"
#include "util.h"
// abstract queue for RCU-like queues
// forward decl
template <typename T, size_t N> struct basic_px_queue;
template <typename T, size_t N>
struct basic_px_group {
basic_px_group()
: next_(nullptr), rcu_tick_(0)
{
static event_counter evt_px_group_creates(
util::cxx_typename<T>::value() + std::string("_px_group_creates"));
++evt_px_group_creates;
}
~basic_px_group()
{
static event_counter evt_px_group_deletes(
util::cxx_typename<T>::value() + std::string("_px_group_deletes"));
++evt_px_group_deletes;
}
// no copying/moving
basic_px_group(const basic_px_group &) = delete;
basic_px_group &operator=(const basic_px_group &) = delete;
basic_px_group(basic_px_group &&) = delete;
static const size_t GroupSize = N;
friend class basic_px_queue<T, N>;
private:
basic_px_group *next_;
typename util::vec<T, GroupSize>::type pxs_;
uint64_t rcu_tick_; // all elements in pxs_ are from this tick,
// this number is only meaningful if pxs_ is
// not empty
};
// not thread safe- should guard with lock for concurrent manipulation
template <typename T, size_t N>
struct basic_px_queue {
basic_px_queue()
: head_(nullptr), tail_(nullptr),
freelist_head_(nullptr), freelist_tail_(nullptr),
ngroups_(0) {}
typedef basic_px_group<T, N> px_group;
basic_px_queue(basic_px_queue &&) = default;
basic_px_queue(const basic_px_queue &) = delete;
basic_px_queue &operator=(const basic_px_queue &) = delete;
~basic_px_queue()
{
reap_chain(head_);
reap_chain(freelist_head_);
}
void
swap(basic_px_queue &other)
{
std::swap(head_, other.head_);
std::swap(tail_, other.tail_);
std::swap(freelist_head_, other.freelist_head_);
std::swap(freelist_tail_, other.freelist_tail_);
std::swap(ngroups_, other.ngroups_);
}
template <typename PtrType, typename ObjType>
class iterator_ : public std::iterator<std::forward_iterator_tag, ObjType> {
public:
inline iterator_() : px_(nullptr), i_() {}
inline iterator_(PtrType *px) : px_(px), i_() {}
// allow iterator to assign to const_iterator
template <typename P, typename O>
inline iterator_(const iterator_<P, O> &o) : px_(o.px_), i_(o.i_) {}
inline ObjType &
operator*() const
{
return px_->pxs_[i_];
}
inline ObjType *
operator->() const
{
return &px_->pxs_[i_];
}
inline uint64_t
tick() const
{
return px_->rcu_tick_;
}
inline bool
operator==(const iterator_ &o) const
{
return px_ == o.px_ && i_ == o.i_;
}
inline bool
operator!=(const iterator_ &o) const
{
return !operator==(o);
}
inline iterator_ &
operator++()
{
++i_;
if (i_ == px_->pxs_.size()) {
px_ = px_->next_;
i_ = 0;
}
return *this;
}
inline iterator_
operator++(int)
{
iterator_ cur = *this;
++(*this);
return cur;
}
private:
PtrType *px_;
size_t i_;
};
typedef iterator_<px_group, T> iterator;
typedef iterator_<const px_group, const T> const_iterator;
inline iterator begin() { return iterator(head_); }
inline const_iterator begin() const { return iterator(head_); }
inline iterator end() { return iterator(nullptr); }
inline const_iterator end() const { return iterator(nullptr); }
// enqueue t in epoch rcu_tick
// assumption: rcu_ticks can only go up!
void
enqueue(const T &t, uint64_t rcu_tick)
{
INVARIANT(bool(head_) == bool(tail_));
INVARIANT(bool(head_) == bool(ngroups_));
INVARIANT(!tail_ || tail_->pxs_.size() <= px_group::GroupSize);
INVARIANT(!tail_ || tail_->rcu_tick_ <= rcu_tick);
px_group *g;
if (unlikely(!tail_ ||
tail_->pxs_.size() == px_group::GroupSize ||
tail_->rcu_tick_ != rcu_tick)) {
ensure_freelist();
// pop off freelist
g = freelist_head_;
freelist_head_ = g->next_;
if (g == freelist_tail_)
freelist_tail_ = nullptr;
g->next_ = nullptr;
g->pxs_.clear();
g->rcu_tick_ = rcu_tick;
ngroups_++;
// adjust ptrs
if (!head_) {
head_ = tail_ = g;
} else {
tail_->next_ = g;
tail_ = g;
}
} else {
g = tail_;
}
INVARIANT(g->pxs_.size() < px_group::GroupSize);
INVARIANT(g->rcu_tick_ == rcu_tick);
INVARIANT(!g->next_);
INVARIANT(tail_ == g);
g->pxs_.emplace_back(t);
sanity_check();
}
void
ensure_freelist()
{
INVARIANT(bool(freelist_head_) == bool(freelist_tail_));
if (likely(freelist_head_))
return;
const size_t nalloc = 16;
alloc_freelist(nalloc);
}
inline bool
empty() const
{
return !head_;
}
#ifdef CHECK_INVARIANTS
void
sanity_check() const
{
INVARIANT(bool(head_) == bool(tail_));
INVARIANT(!tail_ || ngroups_);
INVARIANT(!tail_ || !tail_->next_);
INVARIANT(bool(freelist_head_) == bool(freelist_tail_));
INVARIANT(!freelist_tail_ || !freelist_tail_->next_);
px_group *p = head_, *pprev = nullptr;
size_t n = 0;
uint64_t prev_tick = 0;
while (p) {
INVARIANT(p->pxs_.size());
INVARIANT(p->pxs_.size() <= px_group::GroupSize);
INVARIANT(prev_tick <= p->rcu_tick_);
prev_tick = p->rcu_tick_;
pprev = p;
p = p->next_;
n++;
}
INVARIANT(n == ngroups_);
INVARIANT(!pprev || tail_ == pprev);
p = freelist_head_;
pprev = nullptr;
while (p) {
pprev = p;
p = p->next_;
}
INVARIANT(!pprev || freelist_tail_ == pprev);
}
#else
inline ALWAYS_INLINE void sanity_check() const {}
#endif
// assumes this instance is EMPTY, accept from source
// all entries <= e
inline void
empty_accept_from(basic_px_queue &source, uint64_t rcu_tick)
{
ALWAYS_ASSERT(empty());
INVARIANT(this != &source);
INVARIANT(!tail_);
px_group *p = source.head_, *pnext;
while (p && p->rcu_tick_ <= rcu_tick) {
pnext = p->next_;
p->next_ = nullptr;
if (!head_) {
head_ = tail_ = p;
} else {
tail_->next_ = p;
tail_ = p;
}
ngroups_++;
source.ngroups_--;
source.head_ = p = pnext;
if (!source.head_)
source.tail_ = nullptr;
}
sanity_check();
source.sanity_check();
}
// transfer *this* elements freelist to dest
void
transfer_freelist(basic_px_queue &dest, ssize_t n = -1)
{
if (!freelist_head_)
return;
if (n < 0) {
freelist_tail_->next_ = dest.freelist_head_;
if (!dest.freelist_tail_)
dest.freelist_tail_ = freelist_tail_;
dest.freelist_head_ = freelist_head_;
freelist_head_ = freelist_tail_ = nullptr;
} else {
px_group *p = freelist_head_;
size_t c = 0;
while (p && c++ < static_cast<size_t>(n)) {
px_group *tmp = p->next_;
p->next_ = dest.freelist_head_;
dest.freelist_head_ = p;
if (!dest.freelist_tail_)
dest.freelist_tail_ = p;
if (p == freelist_tail_)
freelist_tail_ = nullptr;
p = tmp;
freelist_head_ = p;
}
}
sanity_check();
}
void
clear()
{
if (!head_)
return;
tail_->next_ = freelist_head_;
if (!freelist_tail_)
freelist_tail_ = tail_;
freelist_head_ = head_;
head_ = tail_ = nullptr;
ngroups_ = 0;
}
// adds n new groups to the freelist
void
alloc_freelist(size_t n)
{
for (size_t i = 0; i < n; i++) {
px_group *p = new px_group;
if (!freelist_tail_)
freelist_tail_ = p;
p->next_ = freelist_head_;
freelist_head_ = p;
}
}
inline size_t get_ngroups() const { return ngroups_; }
inline bool
get_latest_epoch(uint64_t &e) const
{
if (!tail_)
return false;
e = tail_->rcu_tick_;
return true;
}
private:
void
reap_chain(px_group *px)
{
while (px) {
px_group *tmp = px->next_;
delete px;
px = tmp;
}
}
px_group *head_;
px_group *tail_;
px_group *freelist_head_;
px_group *freelist_tail_;
size_t ngroups_;
};