-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeversion.hh
362 lines (326 loc) · 9.36 KB
/
nodeversion.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 Massachusetts Institute of Technology
*
* VLSC Laboratory
* Copyright (c) 2018-2019 Ecole Polytechnique Federale de Lausanne
*
* 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_NODEVERSION_HH
#define MASSTREE_NODEVERSION_HH
#include "compiler.hh"
#include "incll_configs.hh"
template <typename P>
class nodeversion {
public:
typedef P traits_type;
typedef typename P::value_type value_type;
nodeversion() {
}
explicit nodeversion(bool isleaf) {
v_ = isleaf ? (value_type) P::isleaf_bit : 0;
}
bool isleaf() const {
return v_ & P::isleaf_bit;
}
nodeversion<P> stable() const {
return stable(relax_fence_function());
}
template <typename SF>
nodeversion<P> stable(SF spin_function) const {
value_type x = v_;
while (x & P::dirty_mask) {
spin_function();
x = v_;
}
acquire_fence();
return x;
}
template <typename SF>
nodeversion<P> stable_annotated(SF spin_function) const {
value_type x = v_;
while (x & P::dirty_mask) {
spin_function(nodeversion<P>(x));
x = v_;
}
acquire_fence();
return x;
}
bool locked() const {
return v_ & P::lock_bit;
}
bool inserting() const {
return v_ & P::inserting_bit;
}
bool splitting() const {
return v_ & P::splitting_bit;
}
bool deleted() const {
return v_ & P::deleted_bit;
}
bool has_changed(nodeversion<P> x) const {
fence();
return (x.v_ ^ v_) > P::lock_bit;
}
bool is_root() const {
return v_ & P::root_bit;
}
bool has_split(nodeversion<P> x) const {
fence();
return (x.v_ ^ v_) >= P::vsplit_lowbit;
}
bool simple_has_split(nodeversion<P> x) const {
return (x.v_ ^ v_) >= P::vsplit_lowbit;
}
nodeversion<P> lock() {
return lock(*this);
}
nodeversion<P> lock(nodeversion<P> expected) {
return lock(expected, relax_fence_function());
}
template <typename SF>
nodeversion<P> lock(nodeversion<P> expected, SF spin_function) {
while (1) {
if (!(expected.v_ & P::lock_bit)
&& bool_cmpxchg(&v_, expected.v_,
expected.v_ | P::lock_bit))
break;
spin_function();
expected.v_ = v_;
}
masstree_invariant(!(expected.v_ & P::dirty_mask));
expected.v_ |= P::lock_bit;
acquire_fence();
masstree_invariant(expected.v_ == v_);
return expected;
}
void unlock() {
unlock(*this);
}
void unlock(nodeversion<P> x) {
masstree_invariant((fence(), x.v_ == v_));
masstree_invariant(x.v_ & P::lock_bit);
if (x.v_ & P::splitting_bit)
x.v_ = (x.v_ + P::vsplit_lowbit) & P::split_unlock_mask;
else
x.v_ = (x.v_ + ((x.v_ & P::inserting_bit) << 2)) & P::unlock_mask;
release_fence();
v_ = x.v_;
}
void mark_insert() {
masstree_invariant(locked());
v_ |= P::inserting_bit;
acquire_fence();
}
void clear_unlock(){
v_ &= ~P::lock_bit;
}
#ifdef INCLL
void clear_insert() {
v_ &= ~P::inserting_bit;
}
void clear_version_counter_bits(){
v_ &= P::version_counter_mask;
}
#endif //incll
nodeversion<P> mark_insert(nodeversion<P> current_version) {
masstree_invariant((fence(), v_ == current_version.v_));
masstree_invariant(current_version.v_ & P::lock_bit);
v_ = (current_version.v_ |= P::inserting_bit);
acquire_fence();
return current_version;
}
void mark_split() {
masstree_invariant(locked());
v_ |= P::splitting_bit;
acquire_fence();
}
void mark_change(bool is_split) {
masstree_invariant(locked());
v_ |= (is_split + 1) << P::inserting_shift;
acquire_fence();
}
nodeversion<P> mark_deleted() {
masstree_invariant(locked());
v_ |= P::deleted_bit | P::splitting_bit;
acquire_fence();
return *this;
}
void mark_deleted_tree() {
masstree_invariant(locked() && is_root());
v_ |= P::deleted_bit;
acquire_fence();
}
void mark_root() {
v_ |= P::root_bit;
acquire_fence();
}
void mark_nonroot() {
v_ &= ~P::root_bit;
acquire_fence();
}
void assign_version(nodeversion<P> x) {
v_ = x.v_;
}
value_type version_value() const {
return v_;
}
value_type unlocked_version_value() const {
return v_ & P::unlock_mask;
}
private:
value_type v_;
nodeversion(value_type v)
: v_(v) {
}
};
template <typename P>
class singlethreaded_nodeversion {
public:
typedef P traits_type;
typedef typename P::value_type value_type;
singlethreaded_nodeversion() {
}
explicit singlethreaded_nodeversion(bool isleaf) {
v_ = isleaf ? (value_type) P::isleaf_bit : 0;
}
bool isleaf() const {
return v_ & P::isleaf_bit;
}
singlethreaded_nodeversion<P> stable() const {
return *this;
}
template <typename SF>
singlethreaded_nodeversion<P> stable(SF) const {
return *this;
}
template <typename SF>
singlethreaded_nodeversion<P> stable_annotated(SF) const {
return *this;
}
bool locked() const {
return false;
}
bool inserting() const {
return false;
}
bool splitting() const {
return false;
}
bool deleted() const {
return false;
}
bool has_changed(singlethreaded_nodeversion<P>) const {
return false;
}
bool is_root() const {
return v_ & P::root_bit;
}
bool has_split(singlethreaded_nodeversion<P>) const {
return false;
}
bool simple_has_split(singlethreaded_nodeversion<P>) const {
return false;
}
singlethreaded_nodeversion<P> lock() {
return *this;
}
singlethreaded_nodeversion<P> lock(singlethreaded_nodeversion<P>) {
return *this;
}
template <typename SF>
singlethreaded_nodeversion<P> lock(singlethreaded_nodeversion<P>, SF) {
return *this;
}
void unlock() {
}
void unlock(singlethreaded_nodeversion<P>) {
}
void mark_insert() {
}
singlethreaded_nodeversion<P> mark_insert(singlethreaded_nodeversion<P>) {
return *this;
}
void mark_split() {
v_ &= ~P::root_bit;
}
void mark_change(bool is_split) {
if (is_split)
mark_split();
}
singlethreaded_nodeversion<P> mark_deleted() {
return *this;
}
void mark_deleted_tree() {
v_ |= P::deleted_bit;
}
void mark_root() {
v_ |= P::root_bit;
}
void mark_nonroot() {
v_ &= ~P::root_bit;
}
void assign_version(singlethreaded_nodeversion<P> x) {
v_ = x.v_;
}
value_type version_value() const {
return v_;
}
value_type unlocked_version_value() const {
return v_;
}
private:
value_type v_;
};
template <typename V> struct nodeversion_parameters {};
template <> struct nodeversion_parameters<uint32_t> {
enum {
lock_bit = (1U << 0),
inserting_shift = 1,
inserting_bit = (1U << 1),
splitting_bit = (1U << 2),
dirty_mask = inserting_bit | splitting_bit,
vinsert_lowbit = (1U << 3), // == inserting_bit << 2
vsplit_lowbit = (1U << 9),
unused1_bit = (1U << 28),
deleted_bit = (1U << 29),
root_bit = (1U << 30),
isleaf_bit = (1U << 31),
split_unlock_mask = ~(root_bit | unused1_bit | (vsplit_lowbit - 1)),
unlock_mask = ~(unused1_bit | (vinsert_lowbit - 1)),
version_counter_mask = ~(0x3FFFFFFF), //clear version after recovery
top_stable_bits = 4
};
typedef uint32_t value_type;
};
template <> struct nodeversion_parameters<uint64_t> {
enum {
lock_bit = (1ULL << 8),
inserting_shift = 9,
inserting_bit = (1ULL << 9),
splitting_bit = (1ULL << 10),
dirty_mask = inserting_bit | splitting_bit,
vinsert_lowbit = (1ULL << 11), // == inserting_bit << 2
vsplit_lowbit = (1ULL << 27),
unused1_bit = (1ULL << 60),
deleted_bit = (1ULL << 61),
root_bit = (1ULL << 62),
isleaf_bit = (1ULL << 63),
split_unlock_mask = ~(root_bit | unused1_bit | (vsplit_lowbit - 1)),
unlock_mask = ~(unused1_bit | (vinsert_lowbit - 1)),
top_stable_bits = 4
};
typedef uint64_t value_type;
};
typedef nodeversion<nodeversion_parameters<uint32_t> > nodeversion32;
#endif