-
Notifications
You must be signed in to change notification settings - Fork 1
/
skip_lock.c
435 lines (360 loc) · 10.2 KB
/
skip_lock.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/******************************************************************************
* skip_lock.c (Variable-granularity Mutexes)
*
* Mutex only taken for write operations (reads are unprotected). Write
* mutexes come in three flavours, selected by a compile-time flag.
*
* If FAT_MTX is defined:
* A skip list is protected by one mutex for the entire list. Note that this
* differs from skip_bm.c, which takes the mutex for read operations as well.
*
* If TINY_MTX is defined:
* Mutex per forward pointer in each node.
*
* If neither flag is defined:
* Mutex per node.
*
* 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 <stdlib.h>
#include <string.h>
#include <assert.h>
#include "portable_defns.h"
#include "ptst.h"
#include "set.h"
/*
* SKIP LIST
*/
typedef struct node_st node_t;
typedef struct set_st set_t;
typedef VOLATILE node_t *sh_node_pt;
typedef struct ptr_st ptr_t;
struct ptr_st
{
#ifdef TINY_MTX /* mutex per forward pointer */
mcs_lock_t m;
#endif
sh_node_pt p;
};
struct node_st
{
int level;
setkey_t k;
setval_t v;
#ifndef FAT_MTX
mcs_lock_t m;
#endif
ptr_t next[1];
};
struct set_st
{
#ifdef FAT_MTX
mcs_lock_t m;
#endif
node_t head;
};
static int gc_id[NUM_LEVELS];
/*
* LOCKING
*/
#ifdef FAT_MTX
#define LIST_LOCK(_l,_qn) ((void)mcs_lock((void*)&(_l)->m, (_qn)))
#define LIST_UNLOCK(_l,_qn) ((void)mcs_unlock((void*)&(_l)->m, (_qn)))
#define NODE_LOCK(_x,_qn) ((void)0)
#define NODE_UNLOCK(_x,_qn) ((void)0)
#define PTR_UPDATE_LOCK(_x,_i,_qn) ((void)0)
#define PTR_UPDATE_UNLOCK(_x,_i,_qn) ((void)0)
#define PTR_DELETE_LOCK(_x,_i,_qn) ((void)0)
#define PTR_DELETE_UNLOCK(_x,_i,_qn) ((void)0)
#else
#define LIST_LOCK(_l,_qn) ((void)0)
#define LIST_UNLOCK(_l,_qn) ((void)0)
/* We take the main node lock to get exclusive rights on insert/delete ops. */
#define NODE_LOCK(_x,_qn) ((void)mcs_lock((void*)&(_x)->m, (_qn)))
#define NODE_UNLOCK(_x,_qn) ((void)mcs_unlock((void*)&(_x)->m, (_qn)))
#ifdef TINY_MTX
/*
* Predecessor's pointer is locked before swinging (on delete), or
* replumbing (on insert).
*/
#define PTR_UPDATE_LOCK(_x, _i, _qn) \
((void)mcs_lock((void*)&(_x)->next[(_i)].m, (_qn)))
#define PTR_UPDATE_UNLOCK(_x, _i, _qn) \
((void)mcs_unlock((void*)&(_x)->next[(_i)].m, (_qn)))
/*
* When deleting a node, we take the lock on each of its pointers in turn,
* to prevent someone from inserting a new node directly after, or deleting
* immediate successor.
*/
#define PTR_DELETE_LOCK(_x, _i, _qn) PTR_UPDATE_LOCK(_x,_i,(_qn))
#define PTR_DELETE_UNLOCK(_x, _i, _qn) PTR_UPDATE_UNLOCK(_x,_i,(_qn))
#else /* LITTLE_MTX */
/*
* Predecessor must certainly be locked for insert/delete ops. So we take
* the only lock we can.
*/
#define PTR_UPDATE_LOCK(_x, _i, _qn) NODE_LOCK(_x,(_qn))
#define PTR_UPDATE_UNLOCK(_x, _i, _qn) NODE_UNLOCK(_x,(_qn))
/*
* We can't lock individual pointers. There's no need anyway, since we have
* the node's lock already (to allow us exclusive delete rights).
*/
#define PTR_DELETE_LOCK(_x, _i, _qn) ((void)0)
#define PTR_DELETE_UNLOCK(_x, _i, _qn) ((void)0)
#endif
#endif
/*
* 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;
#ifndef FAT_MTX
mcs_init(&n->m);
#endif
#ifdef TINY_MTX
for ( l = 0; l < n->level; l++ )
{
mcs_init(&n->next[l].m);
}
#endif
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]);
}
/*
* Find and lock predecessor at level @i of node with key @k. This
* predecessor must have key >= @x->k.
*/
#ifndef FAT_MTX
static sh_node_pt get_lock(sh_node_pt x, setkey_t k, int i, qnode_t *qn)
{
sh_node_pt y;
setkey_t y_k;
for ( ; ; )
{
READ_FIELD(y, x->next[i].p);
READ_FIELD(y_k, y->k);
if ( y_k >= k ) break;
retry:
x = y;
}
PTR_UPDATE_LOCK(x, i, qn); /* MB => no need for READ_FIELD on x or y. */
y = x->next[i].p;
if ( y->k < k )
{
PTR_UPDATE_UNLOCK(x, i, qn);
goto retry;
}
return(x);
}
#else
#define get_lock(_x,_k,_i,_qn) (_x)
#endif
/*
* 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
* MAIN RETURN VALUE: N at level 0.
*/
static sh_node_pt search_predecessors(set_t *l, setkey_t k, sh_node_pt *pa)
{
sh_node_pt x, y;
setkey_t y_k;
int i;
x = &l->head;
for ( i = NUM_LEVELS - 1; i >= 0; i-- )
{
for ( ; ; )
{
READ_FIELD(y, x->next[i].p);
READ_FIELD(y_k, y->k);
if ( y_k >= k ) break;
x = y; /* remember largest predecessor so far */
}
if ( pa ) pa[i] = x;
}
return(y);
}
/*
* PUBLIC FUNCTIONS
*/
set_t *set_alloc(void)
{
set_t *l;
node_t *n;
int i;
n = malloc(sizeof(*n) + (NUM_LEVELS-1)*sizeof(ptr_t));
memset(n, 0, sizeof(*n) + (NUM_LEVELS-1)*sizeof(ptr_t));
n->k = SENTINEL_KEYMAX;
l = malloc(sizeof(*l) + (NUM_LEVELS-1)*sizeof(ptr_t));
l->head.k = SENTINEL_KEYMIN;
l->head.level = NUM_LEVELS;
#ifdef FAT_MTX
mcs_init(&l->m);
#else
mcs_init(&l->head.m);
#endif
for ( i = 0; i < NUM_LEVELS; i++ )
{
l->head.next[i].p = n;
#ifdef TINY_MTX
mcs_init(&l->head.next[i].m);
#endif
}
return(l);
}
setval_t set_update(set_t *l, setkey_t k, setval_t v, int overwrite)
{
setval_t ov = NULL;
ptst_t *ptst;
sh_node_pt update[NUM_LEVELS];
sh_node_pt x, y;
int i;
qnode_t l_qn, x_qn, y_qn;
k = CALLER_TO_INTERNAL_KEY(k);
ptst = critical_enter();
LIST_LOCK(l, &l_qn);
(void)search_predecessors(l, k, update);
x = get_lock(update[0], k, 0, &x_qn);
y = x->next[0].p;
if ( y->k == k )
{
ov = y->v;
if ( overwrite ) y->v = v;
PTR_UPDATE_UNLOCK(x, 0, &x_qn);
goto out;
}
/* Not in the list, so do the insertion. */
y = alloc_node(ptst);
y->k = k;
y->v = v;
NODE_LOCK(y, &y_qn);
for ( i = 0; i < y->level; i++ )
{
if ( i != 0 ) x = get_lock(update[i], k, i, &x_qn);
y->next[i].p = x->next[i].p;
WMB();
x->next[i].p = y;
PTR_UPDATE_UNLOCK(x, i, &x_qn);
}
NODE_UNLOCK(y, &y_qn);
out:
LIST_UNLOCK(l, &l_qn);
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 update[NUM_LEVELS];
sh_node_pt x, y;
int i;
qnode_t l_qn, x_qn, y_qn, yd_qn;
k = CALLER_TO_INTERNAL_KEY(k);
ptst = critical_enter();
LIST_LOCK(l, &l_qn);
y = search_predecessors(l, k, update);
#ifdef FAT_MTX
if ( y->k != k ) goto out;
#else
y = update[0];
for ( ; ; )
{
setkey_t y_k;
y = y->next[0].p; /* no need for READ_FIELD() */
READ_FIELD(y_k, y->k);
if ( y_k > k ) goto out;
NODE_LOCK(y, &y_qn);
if ( (y_k == k) && (y_k <= y->next[0].p->k) ) break;
NODE_UNLOCK(y, &y_qn);
}
#endif
/* @y is the correct node, and we have it locked, so now delete it. */
for ( i = y->level - 1; i >= 0; i-- )
{
x = get_lock(update[i], k, i, &x_qn);
PTR_DELETE_LOCK(y, i, &yd_qn);
x->next[i].p = y->next[i].p;
WMB();
y->next[i].p = x;
PTR_DELETE_UNLOCK(y, i, &yd_qn);
PTR_UPDATE_UNLOCK(x, i, &x_qn);
}
v = y->v;
free_node(ptst, y);
NODE_UNLOCK(y, &y_qn);
out:
LIST_UNLOCK(l, &l_qn);
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);
if ( x->k == k ) READ_FIELD(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(ptr_t));
}
}