-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmichael.c
243 lines (217 loc) · 5.54 KB
/
michael.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
/*
* File: michael.c
* Author: Vasileios Trigonakis <[email protected]>
* Description: similar to: Michael, M. M. (2002). High performance dynamic
* lock-free hash tables and list-based sets. Proceedings of the Fourteenth Annual
* ACM Symposium on Parallel Algorithms and Architectures
* - SPAA ’02
* michael.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 "linkedlist.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 & 0x1L);
}
inline long
unset_mark(long i)
{
i &= ~0x1L;
return i;
}
inline long
set_mark(long i)
{
i |= 0x1L;
return i;
}
inline long
get_unmarked_ref(long w)
{
return w & ~0x1L;
}
inline long
get_marked_ref(long w)
{
return w | 0x1L;
}
static inline int
physical_delete_right(node_t* left_node, node_t* right_node)
{
node_t* new_next = (node_t*)get_unmarked_ref((long)right_node->next);
node_t* res = CAS_PTR(&left_node->next, right_node, new_next);
int removed = (res == right_node);
#if GC == 1
if (removed)
{
ssmem_free(alloc, (void*) res);
}
#endif
return removed;
}
/*
* list_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.
*/
static inline node_t*
list_search(intset_t* set, skey_t key, node_t** left_node_ptr)
{
node_t* left_node;
node_t* right_node;
retry:
PARSE_TRY();
left_node = set->head;
right_node = set->head->next;
while(1)
{
if (unlikely(left_node->next != right_node))
{
goto retry;
}
if (is_marked_ref((long)right_node->next))
{
CLEANUP_TRY();
if (!physical_delete_right(left_node, right_node))
{
goto retry;
}
}
else
{
if(right_node->key >= key)
{
break;
}
left_node = right_node;
}
right_node = (node_t*)get_unmarked_ref((long)right_node->next);
}
*left_node_ptr = left_node;
return right_node;
}
/*
* returns a value different from 0 if there is a node in the list owning value val.
*/
sval_t
michael_find(intset_t* set, skey_t key)
{
node_t* left;
node_t* right = list_search(set, key, &left);
if (right->key == key && !is_marked_ref((uintptr_t) right->next))
{
return right->val;
}
return 0;
}
/*
* 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
michael_insert(intset_t *the_list, skey_t key, sval_t val)
{
node_t* cas_result;
node_t* right_node;
node_t* node_to_add = NULL;
do
{
UPDATE_TRY();
node_t* left_node;
right_node = list_search(the_list, key, &left_node);
if (right_node->key == key)
{
#if GC == 1
if (unlikely(node_to_add != NULL))
{
ssmem_free(alloc, (void*) node_to_add);
}
#endif
return 0;
}
if (likely(node_to_add == NULL))
{
node_to_add = new_node(key, val, right_node, 0);
}
else
{
node_to_add->next = right_node;
}
#ifdef __tile__
MEM_BARRIER;
#endif
// Try to swing left_node's unmarked next pointer to a new node
cas_result = CAS_PTR(&left_node->next, right_node, node_to_add);
} while(cas_result != right_node);
return 1;
}
/*
* 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
michael_delete(intset_t *the_list, skey_t key)
{
node_t* cas_result;
long unmarked_ref;
node_t* left_node;
node_t* right_node;
do
{
UPDATE_TRY();
right_node = list_search(the_list, key, &left_node);
if (right_node->key != key)
{
return 0;
}
// Try to mark right_node as logically deleted
unmarked_ref = get_unmarked_ref((long)right_node->next);
long marked_ref = get_marked_ref(unmarked_ref);
cas_result = CAS_PTR(&right_node->next, (node_t*)unmarked_ref, (node_t*)marked_ref);
}
while(cas_result != (node_t*)unmarked_ref);
sval_t ret = right_node->val;
physical_delete_right(left_node, right_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;
}