-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLock_free_impr_mem.cpp
More file actions
349 lines (315 loc) · 10.2 KB
/
Copy pathLock_free_impr_mem.cpp
File metadata and controls
349 lines (315 loc) · 10.2 KB
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
/** @file Lock_free_impr_mem.cpp
* @author Daniel Zainzinger
* @date 2.6.2020
*
* @brief Lockfree list based set, with improvement for linkout nodes with memory management.
*/
#include <iostream>
using namespace std;
#include "Lock_free_impr_mem.hpp"
#include "benchmark.hpp"
#include "key.hpp"
#include "node.hpp"
#include <assert.h>
#include <cstring>
#include <omp.h>
#include <queue>
#include <stdint.h>
#define FLAG_POS 63
#define MASK 1ULL << FLAG_POS
// queue to store the candidats for delete
static thread_local queue<void *> deleteQueue;
/**
* @brief Constructor for the datastructure
*/
template <class T> LockFree_impr_mem<T>::LockFree_impr_mem(size_t Tmax) {
head = new nodeAtom<T>(0, INT32_MIN);
head->next = new nodeAtom<T>(0, INT32_MAX);
this->Tmax =Tmax;
snap = new std::atomic<uint32_t>[Tmax];
active = new std::atomic<uint16_t>[Tmax];
for (size_t i = 0; i < Tmax; i++) {
snap[i].store(0);
active[i].store(0);
}
}
/**
* @brief Destructor for the datastructure
*/
template <class T> LockFree_impr_mem<T>::~LockFree_impr_mem() {
while (head != NULL) {
nodeAtom<T> *oldHead = head;
head = getPointer(head->next);
delete oldHead;
}
delete[] snap;
}
/**
* @brief Function which adds one item from the datastructure
*
* @param[in] item item, which should be add to the datastructure
* @param[out] benchMark a struct, which stores information for benchmarking
* @return true, if it was succeccfully added, false otherwise
*/
template <class T> bool LockFree_impr_mem<T>::add(T item, sub_benchMark_t *benchMark) {
int tid = omp_get_thread_num();
active[tid].store(1);
Window_t<nodeAtom<T>> w;
std::chrono::_V2::system_clock::time_point resetTime;
bool reset = false; // is true, if there was a reset and we have to start again from the beginning
int32_t key = key_calc<T>(item);
try {
while (true) {
w = find(item, benchMark);
nodeAtom<T> *pred = w.pred;
nodeAtom<T> *curr = w.curr;
if (reset == true) { // messure the lost time because of a reset
auto finishTime = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = finishTime - resetTime;
uint32_t mus = chrono::duration_cast<chrono::microseconds>(elapsed).count();
benchMark->lostTime += mus;
reset = false;
}
// Item already in the set
if (curr->key == key) {
emptyQueue(false);
return false;
}
assert(pred->key < key && curr->key > key);
// Add item to the set
nodeAtom<T> *n = new nodeAtom<T>(item);
n->next.store(w.curr);
resetFlag(&n->next);
if (atomic_compare_exchange_strong(&pred->next, &curr, n) == false) { // include node
// delete the new node, if it was not possible to add
delete n;
benchMark->goToStart += 1;
resetTime = chrono::high_resolution_clock::now();
reset = true;
continue;
}
emptyQueue(false);
return true;
}
}
// Exception handling
catch (exception &e) {
cerr << "Error during add: " << item << std::endl;
cerr << "Standard exception: " << e.what() << endl;
return false;
} catch (...) {
cerr << "Error during add: " << item << std::endl;
return false;
}
}
/**
* @brief Function which removes one item from the datastructure
*
* @param[in] item item, which should be removed from the datastructure
* @param[out] benchMark a struct, which stores information for benchmarking
* @return true, if it was succeccfully removed, false otherwise
*/
template <class T> bool LockFree_impr_mem<T>::remove(T item, sub_benchMark_t *benchMark) {
int tid = omp_get_thread_num();
active[tid].store(1);
Window_t<nodeAtom<T>> w;
std::chrono::_V2::system_clock::time_point resetTime;
bool reset = false; // is true, if there was a reset and we have to start again from the beginning
int32_t key = key_calc<T>(item);
try {
while (true) {
w = find(item, benchMark);
if (reset == true) { // messure the lost time because of a reset
auto finishTime = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = finishTime - resetTime;
uint32_t mus = chrono::duration_cast<chrono::microseconds>(elapsed).count();
benchMark->lostTime += mus;
reset = false;
}
if (key == w.curr->key) {
nodeAtom<T> *succ = w.curr->next.load();
nodeAtom<T> *markedsucc = succ;
setFlag(&markedsucc);
resetFlag(&succ);
// mark as deleted
if (atomic_compare_exchange_strong(&w.curr->next, &succ, markedsucc) == false) {
benchMark->goToStart += 1;
resetTime = chrono::high_resolution_clock::now();
reset = true;
continue;
}
// try to unlink
if (atomic_compare_exchange_strong(&w.pred->next, &w.curr, succ) == true) {
deleteQueue.push(new node_del<nodeAtom<T>>(w.curr, this->snap, this->Tmax));
}
emptyQueue(false);
return true;
} else {
emptyQueue(false);
return false;
}
}
}
// Exception handling
catch (exception &e) {
cerr << "Error during remove: " << item << std::endl;
cerr << "Standard exception: " << e.what() << endl;
return false;
} catch (...) {
cerr << "Error during remove: " << item << std::endl;
return false;
}
}
/**
* @brief Function which checks if the item is in the datastructure
*
* @param[in] item item for check if it is included
* @param[out] benchMark a struct, which stores information for benchmarking
* @return true, if item is in the datastructure, false otherwise
*/
template <class T> bool LockFree_impr_mem<T>::contains(T item, sub_benchMark_t *benchMark) {
int tid = omp_get_thread_num();
active[tid].store(1);
nodeAtom<T> *n = head;
int32_t key = key_calc<T>(item);
while (n->key < key) {
n = getPointer(n->next.load());
}
bool result = n->key == key && !getFlag(n->next.load());
emptyQueue(false);
return result;
}
/**
* @brief Function which returns a window, where the key is of the first element is smaller than the key of the item,
* and the second key bigger or equal.
*
* @param[in] item item, from which the key is calculated to search the window
* @param[out] benchMark a struct, which stores information for benchmarking
*/
template <class T> Window_t<nodeAtom<T>> LockFree_impr_mem<T>::find(T item, sub_benchMark_t *benchMark) {
nodeAtom<T> *pred, *curr, *old;
;
int32_t key = key_calc<T>(item);
old = head;
retry:
while (true) {
pred = head;
curr = getPointer(pred->next.load());
auto resetTime = chrono::high_resolution_clock::now();
// get through the list
while (true) {
assert(pred->next != NULL);
nodeAtom<T> *succ = getPointer(curr->next.load());
// unlink marked items
while (getFlag(curr->next.load())) {
resetFlag(&curr);
resetFlag(&succ);
// link out curr, not possible when pred is flaged
if (atomic_compare_exchange_strong(&pred->next, &curr, succ) == false) {
if (getFlag(old->next) == false && pred != old) { // improvement
pred = old;
curr = getPointer(pred->next);
succ = getPointer(curr->next);
continue;
}
benchMark->goToStart += 1;
// messure the time thats lost because of the reset
auto finishTime = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = finishTime - resetTime;
uint32_t mus = chrono::duration_cast<chrono::microseconds>(elapsed).count();
benchMark->lostTime += mus;
goto retry;
}
resetTime = chrono::high_resolution_clock::now(); // set time, because we did something useful
deleteQueue.push(
new node_del<nodeAtom<T>>(curr, this->snap, this->Tmax)); // put in de que for delete candidats
curr = succ;
succ = getPointer(succ->next.load());
}
if (curr->key >= key) {
assert(pred->key < key && curr->key >= key);
Window_t<nodeAtom<T>> w{pred, curr};
return w;
}
old = pred;
pred = curr;
curr = succ;
}
}
}
/**
* @brief Function return pointer without a flag
*
* @param [in] pointer pointer to the node with flag included
* @return pointer without flag
*/
template <class T> nodeAtom<T> *LockFree_impr_mem<T>::getPointer(nodeAtom<T> *pointer) {
uint64_t u64_ptr = (uint64_t)pointer;
return (nodeAtom<T> *)(u64_ptr &= ~(MASK));
}
/**
* @brief Function set flag, which mark the node for delete (overloaded)
*
* @param [in,out] pointer pointer to the node
*/
template <class T> void LockFree_impr_mem<T>::setFlag(nodeAtom<T> **pointer) {
uint64_t u64_ptr = (uint64_t)*pointer;
*pointer = (nodeAtom<T> *)(u64_ptr |= MASK);
}
/**
* @brief Function reset flag, which mark the node for delete (overloaded)
*
* @param [in,out] pointer pointer to the node
*/
template <class T> void LockFree_impr_mem<T>::resetFlag(nodeAtom<T> **pointer) {
uint64_t u64_ptr = (uint64_t)*pointer;
*pointer = (nodeAtom<T> *)(u64_ptr &= ~(MASK));
}
/**
* @brief Function reset flag, which mark the node for delete (overloaded)
*
* @param [in,out] pointer pointer to the node
*/
template <class T> void LockFree_impr_mem<T>::resetFlag(atomic<nodeAtom<T> *> *pointer) {
uint64_t u64_ptr = (uint64_t)pointer->load();
pointer->store((nodeAtom<T> *)(u64_ptr &= ~(MASK)));
}
/**
* @brief Function return if node is marked for delete
*
* @param [in] pointer pointer to the node
* @return flag, if node is marded for delete
*/
template <class T> bool LockFree_impr_mem<T>::getFlag(nodeAtom<T> *pointer) {
return (nodeAtom<int> *)((((uint64_t)pointer) >> FLAG_POS) & 1U);
}
/**
* @brief Function to empty and delete que of candidats for delete
*
* @details emptyQueue is always called before return from this object. It checks, if
* each thread increased allready the timestamp, to be sure, that no one reads the
* node during delete
*
* @param[in] final if it is true, the function doesn't check if snap changed
*/
template <class T> void LockFree_impr_mem<T>::emptyQueue(bool final) {
int tid = omp_get_thread_num();
active[tid].store(0);
/* get the total number of threads available in this parallel region */
size_t NPR = omp_get_num_threads();
snap[tid]++;
while (deleteQueue.empty() == false) {
node_del<nodeAtom<T>> *curr = static_cast<node_del<nodeAtom<T>> *>(deleteQueue.front());
if (final == false) {
for (size_t i = 0; i < NPR; i++) {
if (curr->snap[i] == this->snap[i].load() && active[i] == true) {
return;
}
}
}
deleteQueue.pop();
delete (curr);
}
return;
}
template class LockFree_impr_mem<int>;