forked from bhhbazinga/LockFreeQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreclaimer.h
185 lines (155 loc) · 4.55 KB
/
reclaimer.h
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
#ifndef RECLAIMER_H
#define RECLAIMER_H
#include <atomic>
#include <cassert>
#include <functional>
#include <thread>
// An estimate count that must be greater or equal than the max number of
// threads, you need to specify this number
#ifdef MAX_THREADS
const int kEstimateHazardPointerCount = MAX_THREADS;
#else
// defalut max number of threads
const int kEstimateHazardPointerCount = 64;
#endif
struct HazardPointer {
HazardPointer() : ptr(nullptr) {}
~HazardPointer() {}
HazardPointer(const HazardPointer& other) = delete;
HazardPointer(HazardPointer&& other) = delete;
HazardPointer& operator=(const HazardPointer& other) = delete;
HazardPointer& operator=(HazardPointer&& other) = delete;
std::atomic_flag flag;
// We must use atomic pointer to ensure that the modification of pointer
// is visible to other threads
std::atomic<void*> ptr;
};
static HazardPointer g_hazard_pointers[kEstimateHazardPointerCount];
class Reclaimer {
public:
static Reclaimer& GetInstance() {
// Each thread has its own reclaimer
thread_local static Reclaimer reclaimer;
return reclaimer;
}
Reclaimer(const Reclaimer&) = delete;
Reclaimer(Reclaimer&&) = delete;
Reclaimer& operator=(const Reclaimer&) = delete;
Reclaimer& operator=(Reclaimer&&) = delete;
// Mark ptr as an hazard pointer
// If ptr == nullptr then mask last ptr(that is hazard) as no hazard
void MarkHazard(void* const ptr) {
// TODO:Try to optimize memory order
hazard_pointer_->ptr.store(ptr, std::memory_order_seq_cst);
}
// Check if the ptr is hazard
bool Hazard(void* const ptr) {
for (int i = 0; i < kEstimateHazardPointerCount; ++i) {
// TODO:Try to optimize memory order
if (g_hazard_pointers[i].ptr.load(std::memory_order_seq_cst) == ptr) {
return true;
}
}
return false;
}
// If ptr is hazard then reclaim it later
void ReclaimLater(void* const ptr, std::function<void(void*)>&& func) {
ReclaimNode*& old_head = reclaim_list_.head;
old_head->ptr = ptr;
old_head->delete_func = std::move(func);
ReclaimNode* new_head = reclaim_pool_.Pop();
new_head->next = old_head;
old_head = new_head;
}
// Try to reclaim all no hazard pointers
void ReclaimNoHazardPointer() {
ReclaimNode* pre = reclaim_list_.head;
ReclaimNode* p = pre->next;
while (p) {
if (!Hazard(p->ptr)) {
ReclaimNode* temp = p;
p = pre->next = p->next;
temp->delete_func(temp->ptr);
reclaim_pool_.Push(temp);
} else {
pre = p;
p = p->next;
}
}
}
private:
Reclaimer() : hazard_pointer_(nullptr) {
for (int i = 0; i < kEstimateHazardPointerCount; ++i) {
if (!g_hazard_pointers[i].flag.test_and_set()) {
hazard_pointer_ = &g_hazard_pointers[i];
assert(nullptr == hazard_pointer_->ptr);
break;
}
}
// If assertion satisfies, you should increase kEstimateHazardPointerCount
assert(nullptr != hazard_pointer_);
}
~Reclaimer() {
// The Reclaimer destruct when the thread exit
assert(nullptr == hazard_pointer_->ptr);
// 1.Hand over the hazard pointer
hazard_pointer_->flag.clear();
// 2.reclaim all no hazard pointers
ReclaimNode* p = reclaim_list_.head->next;
while (p) {
// Wait until p->ptr is no hazard
// Maybe less efficient?
while (Hazard(p->ptr)) {
std::this_thread::yield();
}
ReclaimNode* temp = p;
p = p->next;
temp->delete_func(temp->ptr);
delete temp;
}
}
struct ReclaimNode {
ReclaimNode() : ptr(nullptr), next(nullptr), delete_func(nullptr) {}
~ReclaimNode() {}
void* ptr;
ReclaimNode* next;
std::function<void(void*)> delete_func;
};
struct ReclaimList {
ReclaimList() : head(new ReclaimNode()) {}
~ReclaimList() { delete head; }
ReclaimNode* head;
};
// Reuse ReclaimNode
struct ReclaimPool {
ReclaimPool() : head(new ReclaimNode()) {}
~ReclaimPool() {
ReclaimNode* temp;
while (head) {
temp = head;
head = head->next;
delete temp;
}
}
void Push(ReclaimNode* node) {
node->next = head;
head = node;
// delete node;
}
ReclaimNode* Pop() {
if (nullptr == head->next) {
return new ReclaimNode();
}
ReclaimNode* temp = head;
head = head->next;
temp->next = nullptr;
return temp;
// return new ReclaimNode();
}
ReclaimNode* head;
};
HazardPointer* hazard_pointer_;
ReclaimList reclaim_list_;
ReclaimPool reclaim_pool_;
};
#endif // RECLAIMER_H