-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFine_Grained.cpp
More file actions
189 lines (177 loc) · 4.25 KB
/
Fine_Grained.cpp
File metadata and controls
189 lines (177 loc) · 4.25 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
/** @file Fine_Grained.cpp
* @author Daniel Zainzinger
* @date 2.6.2020
*
* @brief list based set, which locks node, when it is reading or writing on it
*/
#include <iostream>
using namespace std;
#include "Fine_Grained.hpp"
#include "benchmark.hpp"
#include "key.hpp"
#include "node.hpp"
#include <assert.h>
#include <omp.h>
#include <stdint.h>
/**
* @brief Constructor for the datastructure
*/
template <class T> FineList<T>::FineList() {
head = new nodeFine<T>(0, INT32_MIN);
head->next = new nodeFine<T>(0, INT32_MAX);
}
/**
* @brief Destructor for the datastructure
*/
template <class T> FineList<T>::~FineList() {
while (head != NULL) {
nodeFine<T> *oldHead = head;
head = head->next;
delete oldHead;
}
}
/**
* @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 FineList<T>::add(T item, sub_benchMark_t *benchMark) {
nodeFine<T> *pred = NULL, *curr = NULL;
try {
int32_t key = key_calc<T>(item);
head->lock();
pred = head;
curr = pred->next;
curr->lock();
while (curr->key < key) {
assert(curr->next != NULL);
pred->unlock();
pred = curr;
curr = curr->next;
curr->lock();
}
// Item already in the set
if (key == curr->key) {
pred->unlock();
curr->unlock();
return false;
}
// Add item to the set
nodeFine<T> *n = new nodeFine<T>(item);
n->next = curr;
pred->next = n;
pred->unlock();
curr->unlock();
return true;
}
// Exception handling
catch (exception &e) {
pred->unlock();
curr->unlock();
cerr << "Error during add: " << item << std::endl;
cerr << "Standard exception: " << e.what() << endl;
return false;
} catch (...) {
pred->unlock();
curr->unlock();
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 FineList<T>::remove(T item, sub_benchMark_t *benchMark) {
nodeFine<T> *pred, *curr;
int32_t key = key_calc<T>(item);
head->lock();
try {
pred = head;
curr = pred->next;
curr->lock();
while (curr->key < key) {
assert(curr->next != NULL);
pred->unlock();
pred = curr;
curr = curr->next;
curr->lock();
}
if (key == curr->key) {
pred->next = curr->next;
pred->unlock();
curr->unlock();
delete curr;
return true;
} else {
pred->unlock();
curr->unlock();
return false;
}
}
// Exception handling
catch (exception &e) {
pred->unlock();
curr->unlock();
cerr << "Error during remove: " << item << std::endl;
cerr << "Standard exception: " << e.what() << endl;
return false;
} catch (...) {
pred->unlock();
curr->unlock();
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 FineList<T>::contains(T item, sub_benchMark_t *benchMark) {
nodeFine<T> *pred, *curr;
int32_t key = key_calc<T>(item);
head->lock();
try {
pred = head;
curr = pred->next;
curr->lock();
while (curr->key < key) {
assert(curr->next != NULL);
pred->unlock();
pred = curr;
curr = curr->next;
curr->lock();
}
if (key == curr->key) {
pred->unlock();
curr->unlock();
return true;
} else {
pred->unlock();
curr->unlock();
return false;
}
}
// Exception handling
catch (exception &e) {
pred->unlock();
curr->unlock();
cerr << "Error during contains: " << item << std::endl;
cerr << "Standard exception: " << e.what() << endl;
return false;
} catch (...) {
pred->unlock();
curr->unlock();
cerr << "Error during contains: " << item << std::endl;
return false;
}
}
template class FineList<int>;