-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskip_mcas.c
374 lines (301 loc) · 9.05 KB
/
skip_mcas.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/******************************************************************************
* skip_mcas.c
*
* Skip lists, allowing concurrent update by use of MCAS primitive.
*
* Copyright (c) 2001-2003, K A Fraser
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. Neither the name of the Keir Fraser
* nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific
* prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define __SET_IMPLEMENTATION__
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "portable_defns.h"
#include "ptst.h"
#include "set.h"
#define MCAS_MARK(_v) ((unsigned long)(_v) & 3)
#define PROCESS(_v, _pv) \
while ( MCAS_MARK(_v) ) { \
mcas_fixup((void **)(_pv), _v); \
(_v) = *(_pv); \
}
#define WALK_THRU(_v, _pv) \
if ( MCAS_MARK(_v) ) (_v) = read_barrier_lite((void **)(_pv));
/* Pull in the MCAS implementation. */
#include "mcas.c"
/*
* SKIP LIST
*/
typedef struct node_st node_t;
typedef struct set_st set_t;
typedef VOLATILE node_t *sh_node_pt;
struct node_st
{
int level;
setkey_t k;
setval_t v;
sh_node_pt next[1];
};
struct set_st
{
node_t head;
};
static int gc_id[NUM_LEVELS];
/*
* PRIVATE FUNCTIONS
*/
/*
* Random level generator. Drop-off rate is 0.5 per level.
* Returns value 1 <= level <= NUM_LEVELS.
*/
static int get_level(ptst_t *ptst)
{
unsigned long r = rand_next(ptst);
int l = 1;
r = (r >> 4) & ((1 << (NUM_LEVELS-1)) - 1);
while ( (r & 1) ) { l++; r >>= 1; }
return(l);
}
/*
* Allocate a new node, and initialise its @level field.
* NB. Initialisation will eventually be pushed into garbage collector,
* because of dependent read reordering.
*/
static node_t *alloc_node(ptst_t *ptst)
{
int l;
node_t *n;
l = get_level(ptst);
n = gc_alloc(ptst, gc_id[l - 1]);
n->level = l;
return(n);
}
/* Free a node to the garbage collector. */
static void free_node(ptst_t *ptst, sh_node_pt n)
{
gc_free(ptst, (void *)n, gc_id[n->level - 1]);
}
/*
* Search for first non-deleted node, N, with key >= @k at each level in @l.
* RETURN VALUES:
* Array @pa: @pa[i] is non-deleted predecessor of N at level i
* Array @na: @na[i] is N itself, which should be pointed at by @pa[i]
* MAIN RETURN VALUE: same as @na[0].
*/
static sh_node_pt search_predecessors(
set_t *l, setkey_t k, sh_node_pt *pa, sh_node_pt *na)
{
sh_node_pt x, x_next;
setkey_t x_next_k;
int i;
RMB();
x = &l->head;
for ( i = NUM_LEVELS - 1; i >= 0; i-- )
{
for ( ; ; )
{
READ_FIELD(x_next, x->next[i]);
WALK_THRU(x_next, &x->next[i]);
READ_FIELD(x_next_k, x_next->k);
if ( x_next_k >= k ) break;
x = x_next;
}
if ( pa ) pa[i] = x;
if ( na ) na[i] = x_next;
}
return(x_next);
}
static setval_t finish_delete(sh_node_pt x, sh_node_pt *preds)
{
per_thread_state_t *mcas_ptst = get_ptst();
CasDescriptor_t *cd;
int level, i, ret = FALSE;
sh_node_pt x_next;
setkey_t x_next_k;
setval_t v;
READ_FIELD(level, x->level);
cd = new_descriptor(mcas_ptst, (level << 1) + 1);
cd->status = STATUS_IN_PROGRESS;
cd->length = (level << 1) + 1;
/* First, the deleted node's value field. */
READ_FIELD(v, x->v);
PROCESS(v, &x->v);
if ( v == NULL ) goto fail;
cd->entries[0].ptr = (void **)&x->v;
cd->entries[0].old = v;
cd->entries[0].new = NULL;
for ( i = 0; i < level; i++ )
{
READ_FIELD(x_next, x->next[i]);
PROCESS(x_next, &x->next[i]);
READ_FIELD(x_next_k, x_next->k);
if ( x->k > x_next_k ) { v = NULL; goto fail; }
cd->entries[i +1].ptr = (void **)&x->next[i];
cd->entries[i +1].old = x_next;
cd->entries[i +1].new = preds[i];
cd->entries[i+level+1].ptr = (void **)&preds[i]->next[i];
cd->entries[i+level+1].old = x;
cd->entries[i+level+1].new = x_next;
}
ret = mcas0(mcas_ptst, cd);
if ( ret == 0 ) v = NULL;
fail:
rc_down_descriptor(cd);
return v;
}
/*
* PUBLIC FUNCTIONS
*/
set_t *set_alloc(void)
{
set_t *l;
node_t *n;
int i;
static int mcas_inited = 0;
if ( !CASIO(&mcas_inited, 0, 1) ) mcas_init();
n = malloc(sizeof(*n) + (NUM_LEVELS-1)*sizeof(node_t *));
memset(n, 0, sizeof(*n) + (NUM_LEVELS-1)*sizeof(node_t *));
n->k = SENTINEL_KEYMAX;
/*
* Set the forward pointers of final node to other than NULL,
* otherwise READ_FIELD() will continually execute costly barriers.
* Note use of 0xfc -- that doesn't look like a marked value!
*/
memset(n->next, 0xfc, NUM_LEVELS*sizeof(node_t *));
l = malloc(sizeof(*l) + (NUM_LEVELS-1)*sizeof(node_t *));
l->head.k = SENTINEL_KEYMIN;
l->head.level = NUM_LEVELS;
for ( i = 0; i < NUM_LEVELS; i++ )
{
l->head.next[i] = n;
}
return(l);
}
setval_t set_update(set_t *l, setkey_t k, setval_t v, int overwrite)
{
setval_t ov, new_ov;
ptst_t *ptst;
sh_node_pt preds[NUM_LEVELS], succs[NUM_LEVELS];
sh_node_pt succ, new = NULL;
int i, ret;
per_thread_state_t *mcas_ptst = NULL;
CasDescriptor_t *cd;
k = CALLER_TO_INTERNAL_KEY(k);
ptst = critical_enter();
do {
retry:
ov = NULL;
succ = search_predecessors(l, k, preds, succs);
if ( succ->k == k )
{
/* Already a @k node in the list: update its mapping. */
READ_FIELD(new_ov, succ->v);
do {
ov = new_ov;
PROCESS(ov, &succ->v);
if ( ov == NULL ) goto retry;
}
while ( overwrite && ((new_ov = CASPO(&succ->v, ov, v)) != ov) );
if ( new != NULL ) free_node(ptst, new);
goto out;
}
#ifdef WEAK_MEM_ORDER
/* Free node from previous attempt, if this is a retry. */
if ( new != NULL )
{
free_node(ptst, new);
new = NULL;
}
#endif
/* Not in the list, so initialise a new node for insertion. */
if ( new == NULL )
{
new = alloc_node(ptst);
new->k = k;
new->v = v;
}
for ( i = 0; i < new->level; i++ )
{
new->next[i] = succs[i];
}
if ( !mcas_ptst ) mcas_ptst = get_ptst();
cd = new_descriptor(mcas_ptst, new->level);
cd->status = STATUS_IN_PROGRESS;
cd->length = new->level;
for ( i = 0; i < new->level; i++ )
{
cd->entries[i].ptr = (void **)&preds[i]->next[i];
cd->entries[i].old = succs[i];
cd->entries[i].new = new;
}
ret = mcas0(mcas_ptst, cd);
rc_down_descriptor(cd);
}
while ( !ret );
out:
critical_exit(ptst);
return(ov);
}
setval_t set_remove(set_t *l, setkey_t k)
{
setval_t v = NULL;
ptst_t *ptst;
sh_node_pt preds[NUM_LEVELS], x;
k = CALLER_TO_INTERNAL_KEY(k);
ptst = critical_enter();
do {
x = search_predecessors(l, k, preds, NULL);
if ( x->k > k ) goto out;
} while ( (v = finish_delete(x, preds)) == NULL );
free_node(ptst, x);
out:
critical_exit(ptst);
return(v);
}
setval_t set_lookup(set_t *l, setkey_t k)
{
setval_t v = NULL;
ptst_t *ptst;
sh_node_pt x;
k = CALLER_TO_INTERNAL_KEY(k);
ptst = critical_enter();
x = search_predecessors(l, k, NULL, NULL);
if ( x->k == k )
{
READ_FIELD(v, x->v);
WALK_THRU(v, &x->v);
}
critical_exit(ptst);
return(v);
}
void _init_set_subsystem(void)
{
int i;
for ( i = 0; i < NUM_LEVELS; i++ )
{
gc_id[i] = gc_add_allocator(sizeof(node_t) + i*sizeof(node_t *));
}
}