-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlockfree_queue.h
165 lines (142 loc) · 4.81 KB
/
lockfree_queue.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
#ifndef LOCKFREE_QUEUE_H
#define LOCKFREE_QUEUE_H
#include <atomic>
#include "HazardPointer/reclaimer.h"
template <typename T>
class QueueReclaimer;
template <typename T>
class LockFreeQueue {
static_assert(std::is_copy_constructible_v<T>, "T requires copy constructor");
friend QueueReclaimer<T>;
public:
LockFreeQueue()
: head_(new Node),
tail_(head_.load(std::memory_order_relaxed)),
size_(0) {}
~LockFreeQueue() {
Node* p = head_.load(std::memory_order_consume);
while (p != nullptr) {
Node* tmp = p;
p = p->next.load(std::memory_order_consume);
delete tmp;
}
}
LockFreeQueue(const LockFreeQueue&) = delete;
LockFreeQueue(LockFreeQueue&&) = delete;
LockFreeQueue& operator=(const LockFreeQueue& other) = delete;
LockFreeQueue& operator=(LockFreeQueue&& other) = delete;
template <typename... Args>
void Emplace(Args&&... args);
void Enqueue(const T& data) { Emplace(data); };
void Enqueue(T&& data) { Emplace(std::forward<T>(data)); }
bool Dequeue(T& data);
size_t size() const { return size_.load(std::memory_order_relaxed); }
private:
struct Node;
bool TryInsertNewTail(Node* old_tail, Node* new_tail) {
Node* null_ptr = nullptr;
if (old_tail->next.compare_exchange_strong(null_ptr, new_tail,
std::memory_order_release,
std::memory_order_relaxed)) {
tail_.store(new_tail, std::memory_order_release);
size_.fetch_add(1, std::memory_order_relaxed);
return true;
} else {
return false;
}
}
// Acquire head or tail and mark it as hazard
Node* AcquireSafeNode(std::atomic<Node*>& atomic_node, HazardPointer& hp);
// Invoke this function when the node can be reclaimed
static void OnDeleteNode(void* ptr) { delete static_cast<Node*>(ptr); }
struct Node {
Node() : data(nullptr), next(nullptr) {}
~Node() {
T* ptr = data.load(std::memory_order_relaxed);
if (ptr != nullptr) delete ptr;
}
std::atomic<T*> data;
std::atomic<Node*> next;
};
std::atomic<Node*> head_;
std::atomic<Node*> tail_;
std::atomic<size_t> size_;
static Reclaimer::HazardPointerList global_hp_list_;
};
template <typename T>
Reclaimer::HazardPointerList LockFreeQueue<T>::global_hp_list_;
template <typename T>
class QueueReclaimer : public Reclaimer {
friend LockFreeQueue<T>;
private:
QueueReclaimer(HazardPointerList& hp_list) : Reclaimer(hp_list) {}
~QueueReclaimer() override = default;
static QueueReclaimer<T>& GetInstance() {
thread_local static QueueReclaimer reclaimer(
LockFreeQueue<T>::global_hp_list_);
return reclaimer;
}
};
template <typename T>
typename LockFreeQueue<T>::Node* LockFreeQueue<T>::AcquireSafeNode(
std::atomic<Node*>& atomic_node, HazardPointer& hp) {
QueueReclaimer<T>& reclaimer = QueueReclaimer<T>::GetInstance();
Node* node = atomic_node.load(std::memory_order_consume);
Node* temp;
do {
hp.UnMark();
temp = node;
hp = HazardPointer(&reclaimer, node);
node = atomic_node.load(std::memory_order_consume);
} while (temp != node);
return node;
}
template <typename T>
template <typename... Args>
void LockFreeQueue<T>::Emplace(Args&&... args) {
Node* new_tail = new Node();
T* data_ptr = new T(std::forward<Args>(args)...);
for (;;) {
HazardPointer hp;
Node* old_tail = AcquireSafeNode(tail_, hp);
T* null_ptr = nullptr;
if (old_tail->data.compare_exchange_strong(null_ptr, data_ptr,
std::memory_order_release,
std::memory_order_relaxed)) {
if (!TryInsertNewTail(old_tail, new_tail)) {
// Other help thread already insert tail
delete new_tail;
}
return;
} else {
// If CAS failed,
// help another thread to finish enqueueing
if (TryInsertNewTail(old_tail, new_tail)) {
// Help success, so allocate new one
new_tail = new Node();
}
}
}
}
template <typename T>
bool LockFreeQueue<T>::Dequeue(T& data) {
HazardPointer hp;
Node* old_head;
do {
old_head = AcquireSafeNode(head_, hp);
if (tail_.load(std::memory_order_consume) == old_head) {
// Because old_head is dummy node, the queue is empty
return false;
}
} while (!head_.compare_exchange_weak(
old_head, old_head->next.load(std::memory_order_consume),
std::memory_order_release, std::memory_order_relaxed));
size_.fetch_sub(1, std::memory_order_relaxed);
T* data_ptr = old_head->data.load(std::memory_order_consume);
data = std::move(*data_ptr);
QueueReclaimer<T>& reclaimer = QueueReclaimer<T>::GetInstance();
reclaimer.ReclaimLater(old_head, LockFreeQueue<T>::OnDeleteNode);
reclaimer.ReclaimNoHazardPointer();
return true;
}
#endif // LOCKFREE_QUEUE_H