-
Notifications
You must be signed in to change notification settings - Fork 28
/
elimination_backoff_stack.h
262 lines (222 loc) · 6.92 KB
/
elimination_backoff_stack.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
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
// Copyright (c) 2012-2013, the Scal Project Authors. All rights reserved.
// Please see the AUTHORS file for details. Use of this source code is governed
// by a BSD license that can be found in the LICENSE file.
// Implementing the non-blocking lock-free stack from:
//
// D. Hendler, N. Shavit, and L. Yerushalmi. A scalable lock-free stack algorithm.
// In Proc. Symposium on Parallelism in Algorithms and Architectures (SPAA),
// pages 206–215. ACM, 2004.
#ifndef SCAL_DATASTRUCTURES_ELIMINATION_BACKOFF_STACK_H_
#define SCAL_DATASTRUCTURES_ELIMINATION_BACKOFF_STACK_H_
#include <inttypes.h>
#include <assert.h>
#include <atomic>
#include "datastructures/stack.h"
#include "util/allocation.h"
#include "util/atomic_value_new.h"
#include "util/platform.h"
#include "util/threadlocals.h"
#include "util/random.h"
#include "util/scal-time.h"
#define EMPTY 0
namespace scal {
namespace detail {
enum Opcode {
Push = 1,
Pop = 2
};
template<typename T>
struct Operation {
Opcode opcode;
T data;
};
template<typename T>
struct Node : ThreadLocalMemory<kCachePrefetch * 4> {
explicit Node(T item) : next(NULL), data(item) { }
Node<T>* next;
T data;
};
} // namespace detail
template<typename T>
class EliminationBackoffStack : public Stack<T> {
public:
EliminationBackoffStack(uint64_t num_threads, uint64_t size_collision,
uint64_t delay);
bool push(T item);
bool pop(T *item);
char* ds_get_stats(void) {
char buffer[255] = { 0 };
uint32_t n = snprintf(buffer,
sizeof(buffer),
" ,\"collision\": %ld ,\"delay\": %ld",
size_collision_, delay_);
if (n != strlen(buffer)) {
fprintf(stderr, "%s: error creating stats string\n", __func__);
abort();
}
char *newbuf = static_cast<char*>(calloc(
strlen(buffer) + 1, sizeof(*newbuf)));
return strncpy(newbuf, buffer, strlen(buffer));
}
private:
typedef detail::Node<T> Node;
typedef detail::Opcode Opcode;
typedef detail::Operation<T> Operation;
typedef TaggedValue<Node*> NodePtr;
typedef AtomicTaggedValue<Node*, 64, 64> AtomicNodePtr;
AtomicNodePtr* top_;
Operation* *operations_;
std::atomic<uint64_t>* *location_;
std::atomic<uint64_t>* *collision_;
const uint64_t num_threads_;
const uint64_t size_collision_;
const uint64_t delay_;
bool try_collision(uint64_t thread_id, uint64_t other, T *item);
bool backoff(Opcode opcode, T *item);
};
template<typename T>
EliminationBackoffStack<T>::EliminationBackoffStack(
uint64_t num_threads, uint64_t size_collision, uint64_t delay)
: num_threads_(num_threads), size_collision_(size_collision), delay_(delay) {
top_ = new AtomicNodePtr();
operations_ = static_cast<Operation**>(
ThreadLocalAllocator::Get().CallocAligned(num_threads,
sizeof(Operation*), kCachePrefetch * 4));
location_ = static_cast<std::atomic<uint64_t>**>(
ThreadLocalAllocator::Get().CallocAligned(num_threads,
sizeof(std::atomic<uint64_t>*), kCachePrefetch * 4));
collision_ = static_cast<std::atomic<uint64_t>**>(
ThreadLocalAllocator::Get().CallocAligned(size_collision_,
sizeof(TaggedValue<uint64_t>*), kCachePrefetch * 4));
void* mem;
for (uint64_t i = 0; i < num_threads; i++) {
mem = MallocAligned(sizeof(Operation), kCachePrefetch * 4);
operations_[i] = new (mem) Operation();
}
for (uint64_t i = 0; i < num_threads; i++) {
mem = MallocAligned(sizeof(std::atomic<uint64_t>), kCachePrefetch * 4);
location_[i] = new (mem) std::atomic<uint64_t>();
}
for (uint64_t i = 0; i < size_collision_; i++) {
mem = MallocAligned(sizeof(std::atomic<uint64_t>), kCachePrefetch * 4);
collision_[i] = new (mem) std::atomic<uint64_t>();
}
}
inline uint64_t get_pos(uint64_t size) {
return hwrand() % size;
}
template<typename T>
bool EliminationBackoffStack<T>::try_collision(
uint64_t thread_id, uint64_t other, T *item) {
TaggedValue<T> old_value(other, 0);
if (operations_[thread_id]->opcode == Opcode::Push) {
TaggedValue<T> new_value(thread_id, 0);
if (location_[other]->compare_exchange_weak(
other, thread_id)) {
return true;
} else {
return false;
}
} else {
TaggedValue<T> new_value(EMPTY, 0);
if (location_[other]->compare_exchange_weak(
other, EMPTY)) {
*item = operations_[other]->data;
return true;
} else {
return false;
}
}
}
template<typename T>
bool EliminationBackoffStack<T>::backoff(Opcode opcode, T *item) {
uint64_t thread_id = ThreadContext::get().thread_id();
operations_[thread_id]->opcode = opcode;
operations_[thread_id]->data = *item;
location_[thread_id]->store(thread_id);
uint64_t position = get_pos(size_collision_);
uint64_t him = collision_[position]->load();
while (!collision_[position]->compare_exchange_weak(him, thread_id)) {
}
if (him != EMPTY) {
uint64_t other = location_[him]->load();
if (other == him && operations_[other]->opcode != opcode) {
uint64_t expected = thread_id;
if (location_[thread_id]->compare_exchange_weak(expected, EMPTY)) {
if (try_collision(thread_id, other, item)) {
return true;
} else {
return false;
}
} else {
if (opcode == Opcode::Pop) {
*item = operations_[location_[thread_id]->load()]->data;
location_[thread_id]->store(0);
}
return true;
}
}
}
// Wait some time for collisions.
uint64_t wait = get_hwtime() + delay_;
while (get_hwtime() < wait) {
__asm__("PAUSE");
}
uint64_t expected = thread_id;
if (!location_[thread_id]->compare_exchange_strong(expected, EMPTY)) {
if (opcode == Opcode::Pop) {
*item = operations_[location_[thread_id]->load()]->data;
location_[thread_id]->store(EMPTY);
}
return true;
}
return false;
}
template<typename T>
bool EliminationBackoffStack<T>::push(T item) {
if (backoff(Opcode::Push, &item)) {
return true;
}
Node *n = new Node(item);
NodePtr top_old;
NodePtr top_new;
while (true) {
top_old = top_->load();
n->next = top_old.value();
top_new = NodePtr(n, top_old.tag() + 1);
if (!top_->swap(top_old, top_new)) {
if (backoff(Opcode::Push, &item)) {
return true;
}
} else {
break;
}
}
return true;
}
template<typename T>
bool EliminationBackoffStack<T>::pop(T *item) {
if (backoff(Opcode::Pop, item)) {
return true;
}
NodePtr top_old;
NodePtr top_new;
while (true) {
top_old = top_->load();
if (top_old.value() == NULL) {
return false;
}
top_new = NodePtr(top_old.value()->next, top_old.tag() + 1);
if (!top_->swap(top_old, top_new)) {
if (backoff(Opcode::Pop, item)) {
return true;
}
} else {
break;
}
}
*item = top_old.value()->data;
return true;
}
} // namespace scal
#endif // SCAL_DATASTRUCTURES_ELIMINATION_BACKOFF_STACK_H_