-
Notifications
You must be signed in to change notification settings - Fork 28
/
treiber_stack.h
187 lines (149 loc) · 4.26 KB
/
treiber_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
// Copyright (c) 2012-2015, 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:
//
// R. K. Treiber. Systems Programming: Coping with Parallelism. RJ 5118, IBM
// Almaden Research Center, April 1986.
#ifndef SCAL_DATASTRUCTURES_TREIBER_STACK_H_
#define SCAL_DATASTRUCTURES_TREIBER_STACK_H_
#include "datastructures/distributed_data_structure_interface.h"
#include "datastructures/stack.h"
#include "util/allocation.h"
#include "util/atomic_value_new.h"
#include "util/platform.h"
namespace scal {
namespace detail {
template<typename T>
struct Node : ThreadLocalMemory<64> {
explicit Node(T item) : next(NULL), data(item) { }
Node<T>* next;
T data;
};
} // namespace detail
template<typename T>
class TreiberStack : public Stack<T> {
public:
TreiberStack();
bool push(T item);
bool pop(T *item);
bool try_push(T item, uint64_t top_old_tag);
uint8_t try_pop(T *item, uint64_t top_old_tag, State* put_state);
// Satisfy the DistributedQueueInterface
inline bool put(T item) {
return push(item);
}
inline State put_state() {
return top_->load().tag();
}
inline bool empty() {
return top_->load().value() == NULL;
}
inline bool get_return_put_state(T *item, State* put_state);
private:
typedef detail::Node<T> Node;
typedef TaggedValue<Node*> NodePtr;
typedef AtomicTaggedValue<Node*, 64, 64> AtomicNodePtr;
AtomicNodePtr* top_;
};
template<typename T>
TreiberStack<T>::TreiberStack() : top_(new AtomicNodePtr()) {
}
template<typename T>
bool TreiberStack<T>::push(T item) {
Node* n = new Node(item);
NodePtr top_old;
NodePtr top_new;
do {
top_old = top_->load();
n->next = top_old.value();
top_new = NodePtr(n, top_old.tag() + 1);
} while (!top_->swap(top_old, top_new));
return true;
}
template<typename T>
bool TreiberStack<T>::pop(T *item) {
NodePtr top_old;
NodePtr top_new;
do {
top_old = top_->load();
if (top_old.value() == NULL) {
return false;
}
top_new = NodePtr(top_old.value()->next, top_old.tag() + 1);
} while (!top_->swap(top_old, top_new));
*item = top_old.value()->data;
return true;
}
// Tailored for the LRU distributed stack implementation.
// Using only 8 bit of the top pointers tag as ABA count
// for put operations.
template<typename T>
bool TreiberStack<T>::try_push(T item, uint64_t top_old_tag) {
Node* n = new Node(item);
NodePtr top_old;
NodePtr top_new;
top_old = top_->load();
uint16_t tag_old = top_old.tag();
uint16_t put_tag = tag_old >> 8;
uint16_t get_tag = tag_old & 0x00FF;
if (top_old_tag == put_tag) {
n->next = top_old.value();
put_tag += 1;
if (put_tag == 256) {
put_tag = 0;
}
top_new = NodePtr(n, (put_tag << 8) ^ get_tag);
if (top_->swap(top_old, top_new)) {
return true;
}
}
return false;
}
// Tailored for the LRU distributed stack implementation.
// Using only 8 bit of the top pointers tag as ABA count
// for get operations.
template<typename T>
uint8_t TreiberStack<T>::try_pop(
T *item, uint64_t top_old_tag, State* put_state) {
NodePtr top_old;
NodePtr top_new;
top_old = top_->load();
uint16_t tag_old = top_old.tag();
uint16_t put_tag = tag_old >> 8;
uint16_t get_tag = tag_old & 0x00FF;
if (top_old_tag == get_tag) {
if (top_old.value() == NULL) {
*put_state = top_old.tag();
return 1;
}
get_tag += 1;
if (get_tag == 256) {
get_tag = 0;
}
top_new = NodePtr(top_old.value()->next, (put_tag << 8) ^ get_tag);
if (top_->swap(top_old, top_new)) {
*item = top_old.value()->data;
return 0;
}
}
return 2;
}
template<typename T>
bool TreiberStack<T>::get_return_put_state(T* item, State* put_state) {
NodePtr top_old;
NodePtr top_new;
do {
top_old = top_->load();
if (top_old.value() == NULL) {
*put_state = top_old.tag();
return false;
}
top_new = NodePtr(top_old.value()->next, top_old.tag() + 1);
} while (!top_->swap(top_old, top_new));
*item = top_old.value()->data;
*put_state = top_old.tag();
return true;
}
} // namespace scal
#endif // SCAL_DATASTRUCTURES_TREIBER_STACK_H_