-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraser.c
249 lines (215 loc) · 5.8 KB
/
fraser.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
/*
* File:
* fraser.c
* Author(s):
* Vincent Gramoli <[email protected]>
* Description:
* Lock-based skip list implementation of the Fraser algorithm
* "Practical Lock Freedom", K. Fraser,
* PhD dissertation, September 2003
* Cambridge University Technical Report UCAM-CL-TR-579
*
* Copyright (c) 2009-2010.
*
* fraser.c is part of Synchrobench
*
* Synchrobench 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 "fraser.h"
extern ALIGNED(64) uint8_t levelmax[64];
int is_marked(uintptr_t i)
{
return (int)(i & (uintptr_t)0x01);
}
uintptr_t unset_mark(uintptr_t i)
{
return (i & ~(uintptr_t)0x01);
}
uintptr_t set_mark(uintptr_t i)
{
return (i | (uintptr_t)0x01);
}
void fraser_search(sl_intset_t *set, slkey_t key, sl_node_t **left_list, sl_node_t **right_list)
{
int i;
sl_node_t *left, *left_next, *right, *right_next;
retry:
left = set->head;
for (i = *levelmax - 1; i >= 0; i--)
{
left_next = left->next[i];
if (is_marked((uintptr_t)left_next))
goto retry;
/* Find unmarked node pair at this level */
for (right = left_next; ; right = right_next)
{
/* Skip a sequence of marked nodes */
while(1)
{
right_next = right->next[i];
if (!is_marked((uintptr_t)right_next))
break;
right = (sl_node_t*)unset_mark((uintptr_t)right_next);
}
if (right->key >= key)
break;
left = right;
left_next = right_next;
}
/* Ensure left and right nodes are adjacent */
if ((left_next != right) &&
(!ATOMIC_CAS_MB(&left->next[i], left_next, right)))
goto retry;
if (left_list != NULL)
left_list[i] = left;
if (right_list != NULL)
right_list[i] = right;
}
}
int
fraser_find(sl_intset_t *set, slkey_t key, val_t *val)
{
sl_node_t **succs;
int result;
/* succs = (sl_node_t **)malloc(*levelmax * sizeof(sl_node_t *)); */
succs = (sl_node_t **)ssalloc(*levelmax * sizeof(sl_node_t *));
fraser_search(set, key, NULL, succs);
result = (succs[0]->key == key && !succs[0]->deleted);
*val = succs[0]->val; // garbage if result = 0
/* free(succs); */
ssfree(succs);
return result;
}
void mark_node_ptrs(sl_node_t *n)
{
int i;
sl_node_t *n_next;
for (i=n->toplevel-1; i>=0; i--)
{
do
{
n_next = n->next[i];
if (is_marked((uintptr_t)n_next))
{
break;
}
} while (!ATOMIC_CAS_MB(&n->next[i], n_next, set_mark((uintptr_t)n_next)));
}
}
int
fraser_remove(sl_intset_t *set, slkey_t key, val_t *val, int remove_succ)
{
sl_node_t **succs;
int result;
/* succs = (sl_node_t **)malloc(*levelmax * sizeof(sl_node_t *)); */
succs = (sl_node_t **)ssalloc(*levelmax * sizeof(sl_node_t *));
fraser_search(set, key, NULL, succs);
if (remove_succ) {
result = (succs[0]->next[0] != NULL); // Don't remove tail
key = succs[0]->key;
*val = succs[0]->val;
} else {
result = (succs[0]->key == key);
*val = succs[0]->val;
}
if (result == 0)
goto end;
/* 1. Node is logically deleted when the deleted field is not 0 */
if (succs[0]->deleted)
{
result = 0;
goto end;
}
if (ATOMIC_FETCH_AND_INC_FULL(&succs[0]->deleted) == 0)
{
/* 2. Mark forward pointers, then search will remove the node */
mark_node_ptrs(succs[0]);
/* MEM_BARRIER; */
fraser_search(set, key, NULL, NULL);
}
else
{
result = 0;
}
end:
/* free(succs); */
ssfree(succs);
return result;
}
int
fraser_insert(sl_intset_t *set, slkey_t key, val_t val)
{
sl_node_t *new, *new_next, *pred, *succ, **succs, **preds;
int i;
int result = 0;
new = sl_new_simple_node_val(key, val, get_rand_level(), 0);
/* preds = (sl_node_t **)malloc(*levelmax * sizeof(sl_node_t *)); */
/* succs = (sl_node_t **)malloc(*levelmax * sizeof(sl_node_t *)); */
preds = (sl_node_t **)ssalloc(*levelmax * sizeof(sl_node_t *));
succs = (sl_node_t **)ssalloc(*levelmax * sizeof(sl_node_t *));
retry:
fraser_search(set, key, preds, succs);
/* Update the value field of an existing node */
if (succs[0]->key == key && succs[0]->val == val)
{ /* Value already in list */
if (succs[0]->deleted)
{ /* Value is deleted: remove it and retry */
mark_node_ptrs(succs[0]);
goto retry;
}
result = 0;
sl_delete_node(new);
goto end;
}
for (i = 0; i < new->toplevel; i++)
{
new->next[i] = succs[i];
}
MEM_BARRIER;
/* Node is visible once inserted at lowest level */
if (!ATOMIC_CAS_MB(&preds[0]->next[0], succs[0], new))
{
goto retry;
}
for (i = 1; i < new->toplevel; i++)
{
while (1)
{
pred = preds[i];
succ = succs[i];
/* Update the forward pointer if it is stale */
new_next = new->next[i];
if (is_marked((uintptr_t) new_next))
{
goto success;
}
if ((new_next != succ) &&
(!ATOMIC_CAS_MB(&new->next[i], unset_mark((uintptr_t)new_next), succ)))
break; /* Give up if pointer is marked */
/* Check for old reference to a k node */
if (succ->key == key && succ->val == val)
succ = (sl_node_t *)unset_mark((uintptr_t)succ->next);
/* We retry the search if the CAS fails */
if (ATOMIC_CAS_MB(&pred->next[i], succ, new))
break;
/* MEM_BARRIER; */
fraser_search(set, key, preds, succs);
}
}
success:
result = 1;
end:
/* free(preds); */
/* free(succs); */
ssfree(preds);
ssfree(succs);
return result;
}