-
Notifications
You must be signed in to change notification settings - Fork 0
/
spsc.hpp
258 lines (226 loc) · 6.43 KB
/
spsc.hpp
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
/*
* This file is part of Waitfree.
*
* Waitfree is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Waitfree is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with Waitfree. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SPSC_HPP
#define SPSC_HPP
#include <utility>
#include <thread>
#include <atomic>
#include <cassert>
enum queuereturncode
{
QUEUESUCCESS = 0,
QUEUEISFULL,
QUEUEISEMPTY,
QUEUEFAILURE,
QUEUEAUTHORITYFAILURE,
QUEUEARGUMENTFAILURE,
};
struct safe_thread
{
template <typename Function, typename ... Args>
explicit safe_thread(Function f, Args... args)
: t(std::forward<Function>(f),std::forward<Args>(args)...) {}
~safe_thread() {t.join();}
private:
std::thread t;
};
namespace queue
{
typedef uint32_t ptr_size;
static inline constexpr std::size_t cacheline_size () { return 64;}
struct front;
struct back;
struct front
{
typedef back other;
};
struct back
{
typedef front other;
};
template <typename T>
struct ptr
{
ptr() {}
ptr(ptr_size value) : value(value) {}
ptr& operator++ ()
{
++value;
return *this;
}
ptr_size value = 0;
};
template <typename T>
struct atomic_ptr
{
std::atomic<ptr_size> value{0};
};
template <typename T>
struct caching_pointer
{
atomic_ptr<T> index;
ptr<typename T::other> cache;
};
template <typename T>
struct plain_pointer
{
atomic_ptr<T> index;
};
template <typename T,
std::size_t C = 16,
template <typename> class PP = caching_pointer
>
struct spsc
{
PP<back> push_to;
PP<front> pop_from;
T state[C];
};
template <typename T, std::size_t C, template <typename> class PP>
std::size_t capacity(const spsc<T,C,PP> &)
{
return C;
}
// Wrap the memory order requirements
template <typename T>
static inline ptr<T> load_primary_index(atomic_ptr<T> * location)
{
return ptr<T>(atomic_load_explicit(&(location->value),std::memory_order_relaxed));
}
template <typename T>
static inline ptr<T> load_secondary_index(atomic_ptr<T> * location)
{
return ptr<T>(atomic_load_explicit(&(location->value),std::memory_order_acquire));
}
template <typename T>
static inline void store_primary_index(atomic_ptr<T> * location, ptr<T> index)
{
atomic_store_explicit(&(location->value),index.value, std::memory_order_release);
}
// isfull and isempty implement the asymmetry in the calculation and are unit
// tested directly. They are wrapped in a uniform interface for use here.
template<std::size_t C>
bool isfull(ptr<back> local, ptr<front> remote)
{
return (local.value >= remote.value)
? ((remote.value + C - local.value) < 1)
: (isfull<C>(local.value-C-1,remote.value-C-1));
}
template<std::size_t C>
bool isempty(ptr<front> local, ptr<back> remote)
{
return (remote.value - local.value) < 1;
}
template<std::size_t C>
bool cannot_exchange_dispatch(ptr<front> local, ptr<back> remote)
{
return isempty<C>(local,remote);
}
template<std::size_t C>
bool cannot_exchange_dispatch(ptr<back> local, ptr<front> remote)
{
return isfull<C>(local, remote);
}
// These hold the difference in behaviour for the caching / noncaching versions
template <std::size_t C, typename L>
bool unsafe_to_exchange(ptr<L> local_index,
plain_pointer<L> * /*local*/,
plain_pointer<typename L::other> * remote)
{
auto remote_index = load_secondary_index(&(remote->index));
return (cannot_exchange_dispatch<C>(local_index,remote_index));
}
template <std::size_t C, typename L>
bool unsafe_to_exchange(ptr<L> local_index,
caching_pointer<L> * local,
caching_pointer<typename L::other> * remote)
{
if (cannot_exchange_dispatch<C>(local_index,local->cache))
{
local->cache = load_secondary_index(&(remote->index));
if (cannot_exchange_dispatch<C>(local_index,local->cache))
{
return true;
}
}
return false;
}
template <typename E, typename QueueType, typename T>
typename std::enable_if<std::is_same<E, back>::value, queuereturncode>::type
exchange_at_end(QueueType * Q, T * element)
{
return exchange(Q,element,&(Q->push_to),&(Q->pop_from));
}
template <typename E, typename QueueType, typename T>
typename std::enable_if<std::is_same<E, front>::value, queuereturncode>::type
exchange_at_end(QueueType * Q, T * element)
{
return exchange(Q,element,&(Q->pop_from),&(Q->push_to));
}
template <typename T, std::size_t C, template <typename> class PP, typename L>
queuereturncode exchange(spsc<T,C,PP> * Q,
T * element,
PP<L> * local,
PP<typename L::other> * remote)
{
using std::swap;
auto local_index = load_primary_index(&local->index);
if (unsafe_to_exchange<C>(local_index,local,remote))
{
return QUEUEFAILURE;
}
swap((Q->state)[(local_index.value)%C],*element);
store_primary_index(&(local->index),++local_index);
return QUEUESUCCESS;
}
// Public facing functions
template <typename T, typename QT>
queuereturncode push(QT * Q, T * t)
{
return (QUEUESUCCESS == exchange_at_end<back>(Q,t))
? QUEUESUCCESS
: QUEUEISFULL;
}
template <typename T, typename QT>
queuereturncode pop(QT * Q, T * t)
{
return (QUEUESUCCESS == exchange_at_end<front>(Q,t))
? QUEUESUCCESS
: QUEUEISEMPTY;
}
template <typename T, typename QT>
queuereturncode spinpush(QT * Q, T * t)
{
queuereturncode ret = QUEUEISFULL;
while (ret == QUEUEISFULL)
{
ret = push(Q,t);
}
return ret;
}
template <typename T, typename QT>
queuereturncode spinpop(QT * Q, T * t)
{
queuereturncode ret = QUEUEISEMPTY;
while (ret == QUEUEISEMPTY)
{
ret = pop(Q,t);
}
return ret;
}
}
#endif // SPSC_HPP