forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
masstree_remove.hh
343 lines (305 loc) · 10.8 KB
/
masstree_remove.hh
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2016 President and Fellows of Harvard College
* Copyright (c) 2012-2016 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef MASSTREE_REMOVE_HH
#define MASSTREE_REMOVE_HH
#include "masstree_get.hh"
#include "btree_leaflink.hh"
#include "circular_int.hh"
namespace Masstree {
template <typename P>
bool tcursor<P>::gc_layer(threadinfo& ti)
{
find_locked(ti);
masstree_precondition(!n_->deleted() && !n_->deleted_layer());
// find_locked might return early if another gc_layer attempt has
// succeeded at removing multiple tree layers. So check that the whole
// key has been consumed
if (ka_.has_suffix())
return false;
// find the slot for the child tree
// ka_ is a multiple of ikey_size bytes long. We are looking for the entry
// for the next tree layer, which has keylenx_ corresponding to ikey_size+1.
// So if has_value(), then we found an entry for the same ikey, but with
// length ikey_size; we need to adjust ki_.
kx_.i += has_value();
if (kx_.i >= n_->size())
return false;
permuter_type perm(n_->permutation_);
kx_.p = perm[kx_.i];
if (n_->ikey0_[kx_.p] != ka_.ikey() || !n_->is_layer(kx_.p))
return false;
// remove redundant internode layers
node_type *layer;
while (1) {
layer = n_->lv_[kx_.p].layer();
if (!layer->is_root()) {
n_->lv_[kx_.p] = layer->maybe_parent();
continue;
}
if (layer->isleaf())
break;
internode_type *in = static_cast<internode_type *>(layer);
if (in->size() > 0)
return false;
in->lock(*layer, ti.lock_fence(tc_internode_lock));
if (!in->is_root() || in->size() > 0)
goto unlock_layer;
node_type *child = in->child_[0];
child->make_layer_root();
n_->lv_[kx_.p] = child;
in->mark_split();
in->set_parent(child); // ensure concurrent reader finds true root
// NB: now in->parent() might weirdly be a LEAF!
in->unlock();
in->deallocate_rcu(ti);
}
{
leaf_type* lf = static_cast<leaf_type*>(layer);
if (lf->size() > 0)
return false;
lf->lock(*lf, ti.lock_fence(tc_leaf_lock));
if (!lf->is_root() || lf->size() > 0)
goto unlock_layer;
// child is an empty leaf: kill it
masstree_invariant(!lf->prev_ && !lf->next_.ptr);
masstree_invariant(!lf->deleted());
masstree_invariant(!lf->deleted_layer());
if (P::need_phantom_epoch
&& circular_int<typename P::phantom_epoch_type>::less(n_->phantom_epoch_[0], lf->phantom_epoch_[0]))
n_->phantom_epoch_[0] = lf->phantom_epoch_[0];
lf->mark_deleted_layer(); // NB DO NOT mark as deleted (see above)
lf->unlock();
lf->deallocate_rcu(ti);
return true;
}
unlock_layer:
layer->unlock();
return false;
}
template <typename P>
struct gc_layer_rcu_callback : public P::threadinfo_type::mrcu_callback {
typedef typename P::threadinfo_type threadinfo;
node_base<P>* root_;
int len_;
char s_[0];
gc_layer_rcu_callback(node_base<P>* root, Str prefix)
: root_(root), len_(prefix.length()) {
memcpy(s_, prefix.data(), len_);
}
void operator()(threadinfo& ti);
size_t size() const {
return len_ + sizeof(*this);
}
static void make(node_base<P>* root, Str prefix, threadinfo& ti);
};
template <typename P>
void gc_layer_rcu_callback<P>::operator()(threadinfo& ti)
{
while (!root_->is_root())
root_ = root_->maybe_parent();
if (!root_->deleted()) { // if not destroying tree...
tcursor<P> lp(root_, s_, len_);
bool do_remove = lp.gc_layer(ti);
if (!do_remove || !lp.finish_remove(ti))
lp.n_->unlock();
ti.deallocate(this, size(), memtag_masstree_gc);
}
}
template <typename P>
void gc_layer_rcu_callback<P>::make(node_base<P>* root, Str prefix,
threadinfo& ti)
{
size_t sz = prefix.len + sizeof(gc_layer_rcu_callback<P>);
void *data = ti.allocate(sz, memtag_masstree_gc);
gc_layer_rcu_callback<P> *cb =
new(data) gc_layer_rcu_callback<P>(root, prefix);
ti.rcu_register(cb);
}
template <typename P>
bool tcursor<P>::finish_remove(threadinfo& ti) {
if (n_->modstate_ == leaf<P>::modstate_insert) {
n_->mark_insert();
n_->modstate_ = leaf<P>::modstate_remove;
}
permuter_type perm(n_->permutation_);
perm.remove(kx_.i);
n_->permutation_ = perm.value();
if (perm.size())
return false;
else
return remove_leaf(n_, root_, ka_.prefix_string(), ti);
}
template <typename P>
bool tcursor<P>::remove_leaf(leaf_type* leaf, node_type* root,
Str prefix, threadinfo& ti)
{
if (!leaf->prev_) {
if (!leaf->next_.ptr && !prefix.empty())
gc_layer_rcu_callback<P>::make(root, prefix, ti);
return false;
}
// mark leaf deleted, RCU-free
leaf->mark_deleted();
leaf->deallocate_rcu(ti);
// Ensure node that becomes responsible for our keys has its phantom epoch
// kept up to date
while (P::need_phantom_epoch) {
leaf_type *prev = leaf->prev_;
typename P::phantom_epoch_type prev_ts = prev->phantom_epoch();
while (circular_int<typename P::phantom_epoch_type>::less(prev_ts, leaf->phantom_epoch())
&& !bool_cmpxchg(&prev->phantom_epoch_[0], prev_ts, leaf->phantom_epoch()))
prev_ts = prev->phantom_epoch();
fence();
if (prev == leaf->prev_)
break;
}
// Unlink leaf from doubly-linked leaf list
btree_leaflink<leaf_type>::unlink(leaf);
// Remove leaf from tree, collapse trivial chains, and rewrite
// ikey bounds.
ikey_type ikey = leaf->ikey_bound();
node_type* n = leaf;
node_type* replacement = nullptr;
while (1) {
internode_type *p = n->locked_parent(ti);
p->mark_insert();
masstree_invariant(!p->deleted());
int kp = internode_type::bound_type::upper(ikey, *p);
masstree_invariant(kp == 0 || p->ikey0_[kp - 1] <= ikey); // NB ikey might not equal!
masstree_invariant(p->child_[kp] == n);
p->child_[kp] = replacement;
if (replacement) {
replacement->set_parent(p);
} else if (kp > 0) {
p->shift_down(kp - 1, kp, p->nkeys_ - kp);
--p->nkeys_;
}
if (kp <= 1 && p->nkeys_ > 0 && !p->child_[0]) {
redirect(p, ikey, p->ikey0_[0], ti);
ikey = p->ikey0_[0];
}
n->unlock();
if (p->nkeys_ > (p->child_[0] == nullptr)
|| p->is_root()) {
p->unlock();
return true;
}
p->mark_deleted();
p->deallocate_rcu(ti);
n = p;
replacement = p->child_[p->nkeys_];
p->child_[p->nkeys_] = nullptr;
}
}
template <typename P>
void tcursor<P>::redirect(internode_type* n, ikey_type ikey,
ikey_type replacement_ikey, threadinfo& ti)
{
int kp = -1;
do {
internode_type* p = n->locked_parent(ti);
if (kp >= 0)
n->unlock();
n = p;
kp = internode_type::bound_type::upper(ikey, *n);
if (kp > 0) {
// NB n->ikey0_[kp - 1] might not equal ikey
n->ikey0_[kp - 1] = replacement_ikey;
}
} while (kp == 0 || (kp == 1 && !n->child_[0]));
n->unlock();
}
template <typename P>
struct destroy_rcu_callback : public P::threadinfo_type::mrcu_callback {
typedef typename P::threadinfo_type threadinfo;
typedef typename node_base<P>::leaf_type leaf_type;
typedef typename node_base<P>::internode_type internode_type;
node_base<P>* root_;
int count_;
destroy_rcu_callback(node_base<P>* root)
: root_(root), count_(0) {
}
void operator()(threadinfo& ti);
static void make(node_base<P>* root, Str prefix, threadinfo& ti);
private:
static inline node_base<P>** link_ptr(node_base<P>* n);
static inline void enqueue(node_base<P>* n, node_base<P>**& tailp);
};
template <typename P>
inline node_base<P>** destroy_rcu_callback<P>::link_ptr(node_base<P>* n) {
if (n->isleaf())
return &static_cast<leaf_type*>(n)->parent_;
else
return &static_cast<internode_type*>(n)->parent_;
}
template <typename P>
inline void destroy_rcu_callback<P>::enqueue(node_base<P>* n,
node_base<P>**& tailp) {
*tailp = n;
tailp = link_ptr(n);
}
template <typename P>
void destroy_rcu_callback<P>::operator()(threadinfo& ti) {
if (++count_ == 1) {
while (!root_->is_root())
root_ = root_->maybe_parent();
root_->lock();
root_->mark_deleted_tree(); // i.e., deleted but not splitting
root_->unlock();
ti.rcu_register(this);
return;
}
node_base<P>* workq;
node_base<P>** tailp = &workq;
enqueue(root_, tailp);
while (node_base<P>* n = workq) {
node_base<P>** linkp = link_ptr(n);
if (linkp != tailp)
workq = *linkp;
else {
workq = 0;
tailp = &workq;
}
if (n->isleaf()) {
leaf_type* l = static_cast<leaf_type*>(n);
typename leaf_type::permuter_type perm = l->permutation();
for (int i = 0; i != l->size(); ++i) {
int p = perm[i];
if (l->is_layer(p))
enqueue(l->lv_[p].layer(), tailp);
}
l->deallocate(ti);
} else {
internode_type* in = static_cast<internode_type*>(n);
for (int i = 0; i != in->size() + 1; ++i)
if (in->child_[i])
enqueue(in->child_[i], tailp);
in->deallocate(ti);
}
}
ti.deallocate(this, sizeof(this), memtag_masstree_gc);
}
template <typename P>
void basic_table<P>::destroy(threadinfo& ti) {
if (root_) {
void* data = ti.allocate(sizeof(destroy_rcu_callback<P>), memtag_masstree_gc);
destroy_rcu_callback<P>* cb = new(data) destroy_rcu_callback<P>(root_);
ti.rcu_register(cb);
root_ = 0;
}
}
} // namespace Masstree
#endif