-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathpugh.c
178 lines (160 loc) · 3.81 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
/*
* 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 "pugh.h"
RETRY_STATS_VARS;
/*********************************************************************************
* help search functions
*********************************************************************************/
static inline node_l_t*
search_weak_left(intset_l_t* set, skey_t key)
{
node_l_t* pred = set->head;
node_l_t* succ = pred->next;
while (succ->key < key)
{
pred = succ;
succ = succ->next;
}
return pred;
}
static inline node_l_t*
search_weak_right(intset_l_t* set, skey_t key)
{
node_l_t* succ = set->head->next;
while (succ->key < key)
{
succ = succ->next;
}
return succ;
}
static inline node_l_t*
search_strong(intset_l_t* set, skey_t key, node_l_t** right)
{
node_l_t* pred = search_weak_left(set, key);
GL_LOCK(set->lock);
LOCK(ND_GET_LOCK(pred));
node_l_t* succ = pred->next;
while (unlikely(succ->key < key))
{
UNLOCK(ND_GET_LOCK(pred));
pred = succ;
LOCK(ND_GET_LOCK(pred));
succ = pred->next;
}
*right = succ;
return pred;
}
static inline node_l_t*
search_strong_cond(intset_l_t* set, skey_t key, node_l_t** right, int equal)
{
node_l_t* pred = search_weak_left(set, key);
node_l_t* succ = pred->next;
if ((succ->key == key) == equal)
{
return 0;
}
GL_LOCK(set->lock);
LOCK(ND_GET_LOCK(pred));
succ = pred->next;
while (unlikely(succ->key < key))
{
UNLOCK(ND_GET_LOCK(pred));
pred = succ;
LOCK(ND_GET_LOCK(pred));
succ = pred->next;
}
*right = succ;
return pred;
}
sval_t
list_search(intset_l_t* set, skey_t key)
{
PARSE_TRY();
node_l_t* right = search_weak_right(set, key);
if (right->key == key)
{
return right->val;
}
return false;
}
int
list_insert(intset_l_t* set, skey_t key, sval_t val)
{
PARSE_TRY();
UPDATE_TRY();
int result = 1;
node_l_t* right;
/* optimize for step-wise strong search:: if found, return before locking! */
#if PUGH_RO_FAIL == 1
node_l_t* left = search_strong_cond(set, key, &right, 1);
if (left == NULL)
{
return 0;
}
#else
node_l_t* left = search_strong(set, key, &right);
#endif
if (right->key == key)
{
result = 0;
}
else
{
node_l_t* n = new_node_l(key, val, left->next, 0);
left->next = n;
}
UNLOCK(ND_GET_LOCK(left));
GL_UNLOCK(set->lock);
return result;
}
sval_t
list_delete(intset_l_t* set, skey_t key)
{
PARSE_TRY();
UPDATE_TRY();
sval_t result = 0;
node_l_t* right;
#if PUGH_RO_FAIL == 1
node_l_t* left = search_strong_cond(set, key, &right, 0);
if (left == NULL)
{
return 0;
}
#else
node_l_t* left = search_strong(set, key, &right);
#endif
if (right->key == key)
{
LOCK(ND_GET_LOCK(right));
result = right->val;
left->next = right->next;
right->next = left;
UNLOCK(ND_GET_LOCK(right));
#if GC == 1
ssmem_free(alloc, (void*) right);
#endif
}
UNLOCK(ND_GET_LOCK(left));
GL_UNLOCK(set->lock);
return result;
}