-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathskiplist-optik.c
345 lines (295 loc) · 7.22 KB
/
skiplist-optik.c
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
/*
* File: skiplist-optik.c
* Author: Vasileios Trigonakis <[email protected]>
* Vincent Gramoli <[email protected]>,
* Description: Fine-grained locking skip list.
* Optimized with OPTIK.
* C implementation of the Herlihy et al. algorithm originally
* designed for managed programming language.
* "A Simple Optimistic Skiplist Algorithm"
* M. Herlihy, Y. Lev, V. Luchangco, N. Shavit
* p.124-138, SIROCCO 2007
* skiplist-optik.c is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <[email protected]>,
* Tudor David <[email protected]>
* Distributed Programming Lab (LPD), EPFL
*
* ASCYLIB is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "skiplist-optik.h"
#include "utils.h"
RETRY_STATS_VARS;
#include "latency.h"
#if LATENCY_PARSING == 1
__thread size_t lat_parsing_get = 0;
__thread size_t lat_parsing_put = 0;
__thread size_t lat_parsing_rem = 0;
#endif /* LATENCY_PARSING == 1 */
extern ALIGNED(CACHE_LINE_SIZE) unsigned int levelmax;
#define MAX_BACKOFF 131071
#define HERLIHY_MAX_MAX_LEVEL 64 /* covers up to 2^64 elements */
inline int
ok_to_delete(sl_node_t *node, int found)
{
return (node->fullylinked && ((node->toplevel-1) == found) && !node->marked);
}
inline int
sl_optik_search(sl_intset_t *set, skey_t key, sl_node_t **preds, sl_node_t **succs, optik_t* predsv)
{
restart:
PARSE_TRY();
int found, i;
sl_node_t *pred, *curr;
found = -1;
pred = set->head;
optik_t predv = set->head->lock;
for (i = (pred->toplevel - 1); i >= 0; i--)
{
curr = pred->next[i];
optik_t currv = curr->lock;
while (key > curr->key)
{
predv = currv;
pred = curr;
curr = pred->next[i];
currv = curr->lock;
}
if (unlikely(pred->marked))
{
goto restart;
}
preds[i] = pred;
predsv[i] = predv;
succs[i] = curr;
if (found == -1 && key == curr->key)
{
found = i;
}
}
return found;
}
inline sl_node_t*
sl_optik_left_search(sl_intset_t *set, skey_t key)
{
PARSE_TRY();
int i;
sl_node_t *pred, *curr, *nd = NULL;
pred = set->head;
for (i = (pred->toplevel - 1); i >= 0; i--)
{
curr = pred->next[i];
while (key > curr->key)
{
pred = curr;
curr = pred->next[i];
}
if (key == curr->key)
{
nd = curr;
break;
}
}
return nd;
}
sval_t
sl_optik_find(sl_intset_t *set, skey_t key)
{
sval_t result = 0;
PARSE_START_TS(0);
sl_node_t* nd = sl_optik_left_search(set, key);
PARSE_END_TS(0, lat_parsing_get++);
if (nd != NULL && !nd->marked && nd->fullylinked)
{
result = nd->val;
}
return result;
}
inline void
unlock_levels(sl_intset_t* set, sl_node_t **nodes, int highestlevel)
{
int i;
sl_node_t *old = NULL;
for (i = 0; i <= highestlevel; i++)
{
if (old != nodes[i])
{
optik_unlock(&nodes[i]->lock);
}
old = nodes[i];
}
}
int
sl_optik_insert(sl_intset_t *set, skey_t key, sval_t val)
{
sl_node_t *succs[HERLIHY_MAX_MAX_LEVEL], *preds[HERLIHY_MAX_MAX_LEVEL];
optik_t predsv[HERLIHY_MAX_MAX_LEVEL];
sl_node_t *node_found, *prev_pred, *new_node;
sl_node_t *pred, *succ;
int toplevel, highest_locked, i, valid, found;
unsigned int backoff;
toplevel = get_rand_level();
backoff = 1;
PARSE_START_TS(1);
while (1)
{
UPDATE_TRY();
found = sl_optik_search(set, key, preds, succs, predsv);
PARSE_END_TS(1, lat_parsing_put);
if (found != -1)
{
node_found = succs[found];
if (!node_found->marked)
{
while (!node_found->fullylinked)
{
PAUSE;
}
PARSE_END_INC(lat_parsing_put);
return 0;
}
continue;
}
highest_locked = -1;
prev_pred = NULL;
valid = 1;
for (i = 0; valid && (i < toplevel); i++)
{
pred = preds[i];
succ = succs[i];
if (pred != prev_pred)
{
// optik_lock(&pred->lock);
if (!optik_lock_version(&pred->lock, predsv[i]))
{
valid = !pred->marked;
}
highest_locked = i;
prev_pred = pred;
}
valid = valid && !succ->marked;
valid = valid && (pred->next[i] == succ);
}
if (!valid)
{ /* Unlock the predecessors before leaving */
unlock_levels(set, preds, highest_locked); /* unlocks the global-lock in the GL case */
if (backoff > 5000)
{
nop_rep(backoff & MAX_BACKOFF);
}
backoff <<= 1;
continue;
}
new_node = sl_new_simple_node(key, val, toplevel, 0);
for (i = 0; i < toplevel; i++)
{
new_node->next[i] = succs[i];
}
#if defined(__tile__)
MEM_BARRIER;
#endif
for (i = 0; i < toplevel; i++)
{
preds[i]->next[i] = new_node;
}
new_node->fullylinked = 1;
unlock_levels(set, preds, highest_locked);
PARSE_END_INC(lat_parsing_put);
return 1;
}
}
sval_t
sl_optik_delete(sl_intset_t *set, skey_t key)
{
sl_node_t *succs[HERLIHY_MAX_MAX_LEVEL], *preds[HERLIHY_MAX_MAX_LEVEL];
optik_t predsv[HERLIHY_MAX_MAX_LEVEL];
sl_node_t *node_todel, *prev_pred;
sl_node_t *pred, *succ;
int is_marked, toplevel, highest_locked, i, valid, found;
unsigned int backoff;
node_todel = NULL;
is_marked = 0;
toplevel = -1;
backoff = 1;
PARSE_START_TS(2);
while(1)
{
UPDATE_TRY();
found = sl_optik_search(set, key, preds, succs, predsv);
PARSE_END_TS(2, lat_parsing_rem);
/* If not marked and ok to delete, then mark it */
if (is_marked || (found != -1 && ok_to_delete(succs[found], found)))
{
if (!is_marked)
{
node_todel = succs[found];
optik_lock(&node_todel->lock);
toplevel = node_todel->toplevel;
/* Unless it has been marked meanwhile */
if (node_todel->marked)
{
optik_unlock(&node_todel->lock);
PARSE_END_INC(lat_parsing_rem);
return 0;
}
node_todel->marked = 1;
is_marked = 1;
}
/* Physical deletion */
highest_locked = -1;
prev_pred = NULL;
valid = 1;
for (i = 0; valid && (i < toplevel); i++)
{
pred = preds[i];
succ = succs[i];
if (pred != prev_pred)
{
if (!optik_lock_version(&pred->lock, predsv[i]))
{
valid = !pred->marked;
valid = valid && (pred->next[i] == succ);
}
highest_locked = i;
prev_pred = pred;
}
}
if (!valid)
{
unlock_levels(set, preds, highest_locked);
if (backoff > 5000)
{
nop_rep(backoff & MAX_BACKOFF);
}
backoff <<= 1;
continue;
}
for (i = (toplevel-1); i >= 0; i--)
{
preds[i]->next[i] = node_todel->next[i];
}
sval_t val = node_todel->val;
optik_unlock(&node_todel->lock);
unlock_levels(set, preds, highest_locked);
#if GC == 1
ssmem_free(alloc, (void*) node_todel);
#endif
PARSE_END_INC(lat_parsing_rem);
return val;
}
else
{
PARSE_END_INC(lat_parsing_rem);
return 0;
}
}
}