forked from LPD-EPFL/ASCYLIB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harris.c
272 lines (248 loc) · 5.98 KB
/
harris.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
/*
* File: harris.c
* Author: Vincent Gramoli <[email protected]>,
* Vasileios Trigonakis <[email protected]>
* Description: Timothy L Harris. A Pragmatic Implementation
* of Non-blocking Linked Lists. DISC 2001.
* harris.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 "harris.h"
RETRY_STATS_VARS;
/*
* The five following functions handle the low-order mark bit that indicates
* whether a node is logically deleted (1) or not (0).
* - is_marked_ref returns whether it is marked,
* - (un)set_marked changes the mark,
* - get_(un)marked_ref sets the mark before returning the node.
*/
inline int
is_marked_ref(long i)
{
/* return (int) (i & (LONG_MIN+1)); */
return (int) (i & 0x1L);
}
inline long
unset_mark(long i)
{
/* i &= LONG_MAX-1; */
i &= ~0x1L;
return i;
}
inline long
set_mark(long i)
{
/* i = unset_mark(i); */
/* i += 1; */
i |= 0x1L;
return i;
}
inline long
get_unmarked_ref(long w)
{
/* return unset_mark(w); */
return w & ~0x1L;
}
inline long
get_marked_ref(long w)
{
/* return set_mark(w); */
return w | 0x1L;
}
/*
* harris_search looks for value val, it
* - returns right_node owning val (if present) or its immediately higher
* value present in the list (otherwise) and
* - sets the left_node to the node owning the value immediately lower than val.
* Encountered nodes that are marked as logically deleted are physically removed
* from the list, yet not garbage collected.
*/
node_t*
harris_search(intset_t *set, skey_t key, node_t **left_node)
{
node_t *left_node_next, *right_node;
left_node_next = set->head;
do
{
PARSE_TRY();
node_t *t = set->head;
node_t *t_next = set->head->next;
/* Find left_node and right_node */
do
{
if (!is_marked_ref((long) t_next))
{
(*left_node) = t;
left_node_next = t_next;
}
t = (node_t *) get_unmarked_ref((long) t_next);
if (!t->next)
{
break;
}
t_next = t->next;
}
while (is_marked_ref((long) t_next) || (t->key < key));
right_node = t;
/* Check that nodes are adjacent */
if (left_node_next == right_node)
{
if (right_node->next && is_marked_ref((long) right_node->next))
{
continue;
}
else
{
return right_node;
}
}
CLEANUP_TRY();
/* Remove one or more marked nodes */
if (ATOMIC_CAS_MB(&(*left_node)->next, left_node_next, right_node))
{
#if GC == 1
node_t* cur = left_node_next;
do
{
node_t* free = cur;
cur = (node_t*) get_unmarked_ref((long) cur->next);
ssmem_free(alloc, (void*) free);
}
while (cur != right_node);
#endif
if (!(right_node->next && is_marked_ref((long) right_node->next)))
{
return right_node;
}
}
}
while (1);
}
/*
* harris_find returns whether there is a node in the list owning value val.
*/
sval_t
harris_find(intset_t *set, skey_t key)
{
node_t *right_node, *left_node;
left_node = set->head;
right_node = harris_search(set, key, &left_node);
if ((right_node->next == NULL) || right_node->key != key)
{
return 0;
}
else
{
return right_node->val;
}
}
/*
* harris_find inserts a new node with the given value val in the list
* (if the value was absent) or does nothing (if the value is already present).
*/
int
harris_insert(intset_t *set, skey_t key, sval_t val)
{
node_t *newnode = NULL, *right_node, *left_node;
left_node = set->head;
do
{
UPDATE_TRY();
right_node = harris_search(set, key, &left_node);
if (right_node->key == key)
{
#if GC == 1
if (unlikely(newnode != NULL))
{
ssmem_free(alloc, (void*) newnode);
}
#endif
return 0;
}
if (likely(newnode == NULL))
{
newnode = new_node(key, val, right_node, 0);
}
else
{
newnode->next = right_node;
}
#ifdef __tile__
MEM_BARRIER;
#endif
if (ATOMIC_CAS_MB(&left_node->next, right_node, newnode))
return 1;
}
while(1);
}
/*
* harris_find deletes a node with the given value val (if the value is present)
* or does nothing (if the value is already present).
* The deletion is logical and consists of setting the node mark bit to 1.
*/
sval_t
harris_delete(intset_t *set, skey_t key)
{
node_t *right_node, *right_node_next, *left_node;
left_node = set->head;
sval_t ret = 0;
do
{
UPDATE_TRY();
right_node = harris_search(set, key, &left_node);
if (right_node->key != key)
{
return 0;
}
right_node_next = right_node->next;
if (!is_marked_ref((long) right_node_next))
{
if (ATOMIC_CAS_MB(&right_node->next, right_node_next, get_marked_ref((long) right_node_next)))
{
ret = right_node->val;
break;
}
}
}
while(1);
if (likely(ATOMIC_CAS_MB(&left_node->next, right_node, right_node_next)))
{
#if GC == 1
ssmem_free(alloc, (void*) get_unmarked_ref((long) right_node));
#endif
;
}
else
{
harris_search(set, key, &left_node);
}
return ret;
}
int
set_size(intset_t *set)
{
int size = 0;
node_t* node;
/* We have at least 2 elements */
node = (node_t*) get_unmarked_ref((long) set->head->next);
while ((node_t*) get_unmarked_ref((long) node->next) != NULL)
{
if (!is_marked_ref((long) node->next)) size++;
node = (node_t*) get_unmarked_ref((long) node->next);
}
return size;
}