forked from LPD-EPFL/ASCYLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pugh.c
219 lines (194 loc) · 4.63 KB
/
pugh.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
/*
* File: pugh.c
* Author: Vasileios Trigonakis <[email protected]>
* Description: William Pugh.
* Concurrent Maintenance of Skip Lists. Technical report, 1990.
* pugh.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 "optimistic.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 */
sval_t
optimistic_find(sl_intset_t *set, skey_t key)
{
PARSE_TRY();
PARSE_START_TS(0);
sval_t val = 0;
sl_node_t* succ = NULL;
sl_node_t* pred = set->head;
int lvl;
for (lvl = levelmax - 1; lvl >= 0; lvl--)
{
succ = pred->next[lvl];
while (succ->key < key)
{
pred = succ;
succ = succ->next[lvl];
}
if (succ->key == key) /* at any search level */
{
val = succ->val;
break;
}
}
PARSE_END_TS(0, lat_parsing_get++);
return val;
}
sl_node_t*
get_lock(sl_node_t* pred, skey_t key, int lvl)
{
sl_node_t* succ = pred->next[lvl];
while (succ->key < key)
{
pred = succ;
succ = succ->next[lvl];
}
LOCK(ND_GET_LOCK(pred));
succ = pred->next[lvl];
while (succ->key < key)
{
UNLOCK(ND_GET_LOCK(pred));
pred = succ;
LOCK(ND_GET_LOCK(pred));
succ = pred->next[lvl];
}
return pred;
}
int
optimistic_insert(sl_intset_t *set, skey_t key, sval_t val)
{
PARSE_TRY();
UPDATE_TRY();
PARSE_START_TS(1);
sl_node_t* update[HERLIHY_MAX_MAX_LEVEL];
sl_node_t* succ;
sl_node_t* pred = set->head;
int lvl;
for (lvl = levelmax - 1; lvl >= 0; lvl--)
{
succ = pred->next[lvl];
while (succ->key < key)
{
pred = succ;
succ = succ->next[lvl];
}
if (unlikely(succ->key == key)) /* at any search level */
{
return false;
}
update[lvl] = pred;
}
PARSE_END_TS(1, lat_parsing_put++);
int rand_lvl = get_rand_level(); /* do the rand_lvl outside the CS */
GL_LOCK(set->lock);
pred = get_lock(pred, key, 0);
if (unlikely(pred->next[0]->key == key))
{
UNLOCK(ND_GET_LOCK(pred));
GL_UNLOCK(set->lock);
return false;
}
sl_node_t* n = sl_new_simple_node(key, val, rand_lvl, 0);
LOCK(ND_GET_LOCK(n));
n->next[0] = pred->next[0]; /* we already hold the lock for lvl 0 */
#ifdef __tile__
MEM_BARRIER;
#endif
pred->next[0] = n;
UNLOCK(ND_GET_LOCK(pred));
for (lvl = 1; lvl < n->toplevel; lvl++)
{
pred = get_lock(update[lvl], key, lvl);
n->next[lvl] = pred->next[lvl];
#ifdef __tile__
MEM_BARRIER;
#endif
pred->next[lvl] = n;
UNLOCK(ND_GET_LOCK(pred));
}
UNLOCK(ND_GET_LOCK(n));
GL_UNLOCK(set->lock);
return 1;
}
sval_t
optimistic_delete(sl_intset_t *set, skey_t key)
{
PARSE_TRY();
UPDATE_TRY();
PARSE_START_TS(2);
sl_node_t* update[HERLIHY_MAX_MAX_LEVEL];
sl_node_t* succ = NULL;
sl_node_t* pred = set->head;
int lvl;
for (lvl = levelmax - 1; lvl >= 0; lvl--)
{
succ = pred->next[lvl];
while (succ->key < key)
{
pred = succ;
succ = succ->next[lvl];
}
update[lvl] = pred;
}
PARSE_END_TS(2, lat_parsing_rem++);
GL_LOCK(set->lock);
succ = pred;
int is_garbage;
do
{
succ = succ->next[0];
if (succ->key > key)
{
GL_UNLOCK(set->lock);
return false;
}
LOCK(ND_GET_LOCK(succ));
is_garbage = (succ->key > succ->next[0]->key);
if (is_garbage || succ->key != key)
{
UNLOCK(ND_GET_LOCK(succ));
}
else
{
break;
}
}
while(true);
for (lvl = succ->toplevel - 1; lvl >= 0; lvl--)
{
pred = get_lock(update[lvl], key, lvl);
pred->next[lvl] = succ->next[lvl];
succ->next[lvl] = pred; /* pointer reversal! :-) */
UNLOCK(ND_GET_LOCK(pred));
}
UNLOCK(ND_GET_LOCK(succ));
GL_UNLOCK(set->lock);
#if GC == 1
ssmem_free(alloc, (void*) succ);
#endif
return succ->val;
}