-
Notifications
You must be signed in to change notification settings - Fork 20
/
MagedHarrisLinkedListSetHP.hpp
274 lines (243 loc) · 9.34 KB
/
MagedHarrisLinkedListSetHP.hpp
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
/******************************************************************************
* Copyright (c) 2014-2016, Pedro Ramalhete, Andreia Correia
* All rights reserved.
*
* 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 Concurrency Freaks 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 <COPYRIGHT HOLDER> 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.
******************************************************************************
*/
#ifndef _MAGED_MICHAEL_TIM_HARRIS_LINKED_LIST_HP_H_
#define _MAGED_MICHAEL_TIM_HARRIS_LINKED_LIST_HP_H_
#include <atomic>
#include <thread>
#include <forward_list>
#include <set>
#include <iostream>
#include <string>
#include "common/HazardPointers.hpp"
/**
* This is the linked list by Maged M. Michael that uses Hazard Pointers in
* a correct way because Harris original algorithm with HPs doesn't.
* Lock-Free Linked List as described in Maged M. Michael paper (Figure 4):
* http://www.cs.tau.ac.il/~afek/p73-Lock-Free-HashTbls-michael.pdf
*
*
* <p>
* This set has three operations:
* <ul>
* <li>add(x) - Lock-Free
* <li>remove(x) - Lock-Free
* <li>contains(x) - Lock-Free
* </ul><p>
* <p>
* @author Pedro Ramalhete
* @author Andreia Correia
*/
template<typename T>
class MagedHarrisLinkedListSetHP {
private:
struct Node {
T key;
std::atomic<Node*> next;
Node(T key) : key{key}, next{nullptr} { }
bool casNext(Node *cmp, Node *val) {
return next.compare_exchange_strong(cmp, val);
}
};
// Pointers to head and tail sentinel nodes of the list
std::atomic<Node*> head;
std::atomic<Node*> tail;
const int maxThreads;
// We need 3 hazard pointers
HazardPointers<Node> hp {3, maxThreads};
const int kHp0 = 0; // Protects next
const int kHp1 = 1; // Protects curr
const int kHp2 = 2; // Protects prev
public:
MagedHarrisLinkedListSetHP(const int maxThreads) : maxThreads{maxThreads} {
head.store(new Node({}));
tail.store(new Node({}));
head.load()->next.store(tail.load());
}
// We don't expect the destructor to be called if this instance can still be in use
~MagedHarrisLinkedListSetHP() {
Node *prev = head.load();
Node *node = prev->next.load();
while (node != nullptr) {
delete prev;
prev = node;
node = prev->next.load();
}
delete prev;
}
static std::string className() { return "MagedHarris-LinkedListSetHP"; }
/*
* This function is single threaded to be called at the start of the test.
* It is assumed keys are ordered
*/
void addAll(T** keys, const int size, const int tid) {
Node* node = head;
for(int i=0;i<size;i++){
T* key = keys[i];
Node* newNode = new Node(*key);
node->next.store(newNode, std::memory_order_relaxed);
node = newNode;
}
node->next.store(tail.load(std::memory_order_relaxed), std::memory_order_relaxed);
}
/**
* This method is named 'Insert()' in the original paper.
* Taken from Figure 7 of the paper:
* "High Performance Dynamic Lock-Free Hash Tables and List-Based Sets"
* <p>
* Progress Condition: Lock-Free
*
*/
bool add(T key, const int tid)
{
Node *curr, *next;
std::atomic<Node*> *prev;
Node* newNode = new Node(key);
while (true) {
if (find(&key, &prev, &curr, &next, tid)) {
delete newNode; // There is already a matching key
hp.clear(tid);
return false;
}
newNode->next.store(curr, std::memory_order_relaxed);
Node *tmp = getUnmarked(curr);
if (prev->compare_exchange_strong(tmp, newNode)) { // seq-cst
hp.clear(tid);
return true;
}
}
}
/**
* This method is named 'Delete()' in the original paper.
* Taken from Figure 7 of the paper:
* "High Performance Dynamic Lock-Free Hash Tables and List-Based Sets"
*/
bool remove(T key, const int tid)
{
Node *curr, *next;
std::atomic<Node*> *prev;
while (true) {
/* Try to find the key in the list. */
if (!find(&key, &prev, &curr, &next, tid)) {
hp.clear(tid);
return false;
}
/* Mark if needed. */
Node *tmp = getUnmarked(next);
if (!curr->next.compare_exchange_strong(tmp, getMarked(next))) {
continue; /* Another thread interfered. */
}
tmp = getUnmarked(curr);
if (prev->compare_exchange_strong(tmp, getUnmarked(next))) { /* Unlink */
hp.clear(tid);
hp.retire(getUnmarked(curr), tid); /* Reclaim */
} else {
hp.clear(tid);
}
/*
* If we want to prevent the possibility of there being an
* unbounded number of unmarked nodes, add "else _find(head,key)."
* This is not necessary for correctness.
*/
return true;
}
}
/**
* This is named 'Search()' on the original paper
* Taken from Figure 7 of the paper:
* "High Performance Dynamic Lock-Free Hash Tables and List-Based Sets"
* <p>
* Progress Condition: Lock-Free
*/
bool contains(T key, const int tid)
{
Node *curr, *next;
std::atomic<Node*> *prev;
bool isContains = find(&key, &prev, &curr, &next, tid);
hp.clear(tid);
return isContains;
}
private:
/**
* TODO: This needs to be code reviewed... it's not production-ready
* <p>
* Progress Condition: Lock-Free
*/
bool find (T* key, std::atomic<Node*> **par_prev, Node **par_curr, Node **par_next, const int tid)
{
std::atomic<Node*> *prev;
Node *curr, *next;
try_again:
prev = &head;
curr = prev->load();
// Protect curr with a hazard pointer.
hp.protectPtr(kHp1, curr, tid);
if (prev->load() != getUnmarked(curr)) goto try_again;
while (true) {
if (getUnmarked(curr) == nullptr) break;
// Protect next with a hazard pointer.
next = curr->next.load();
hp.protectPtr(kHp0, getUnmarked(next), tid);
if (getUnmarked(curr)->next.load() != next) goto try_again;
if (getUnmarked(next) == tail.load()) break;
if (prev->load() != getUnmarked(curr)) goto try_again;
if (getUnmarked(next) == next) { // !cmark in the paper
if (!(getUnmarked(curr)->key < *key)) { // Check for null to handle head and tail
*par_curr = curr;
*par_prev = prev;
*par_next = next;
return (getUnmarked(curr)->key == *key);
}
prev = &getUnmarked(curr)->next;
hp.protectRelease(kHp2, getUnmarked(curr), tid);
} else {
// Update the link and retire the node.
Node *tmp = getUnmarked(curr);
if (!prev->compare_exchange_strong(tmp, getUnmarked(next))) {
goto try_again;
}
hp.retire(getUnmarked(curr), tid);
}
curr = next;
hp.protectRelease(kHp1, getUnmarked(next), tid);
}
*par_curr = curr;
*par_prev = prev;
*par_next = next;
return false;
}
bool isMarked(Node * node) {
return ((size_t) node & 0x1);
}
Node * getMarked(Node * node) {
return (Node*)((size_t) node | 0x1);
}
Node * getUnmarked(Node * node) {
return (Node*)((size_t) node & (~0x1));
}
};
#endif /* _MAGED_MICHAEL_TIM_HARRIS_LINKED_LIST_HP_H_ */