forked from akors/atomic_queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomic_queue.hpp
313 lines (263 loc) · 9.36 KB
/
atomic_queue.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (c) 2012 Alexander Korsunsky <fat.lobyte9@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ATOMIC_QUEUE_HPP_INCLUDED
#define ATOMIC_QUEUE_HPP_INCLUDED
#include <cstddef>
#include <memory>
#include <atomic>
#include <thread>
// At the time of writing, MSVC didn't know noexcept
#if defined(_MSC_VER) && _MSC_VER <= 1700
# define noexcept throw()
#endif
namespace aq {
namespace detail {
/** A node in the linked list. */
template <typename T>
struct node
{
T t /**< Value */;
std::atomic<node<T>*> next /**< Next node in list */;
};
/** A thread-safe and lock-free queue container.
*
* Use this class as a queue where multiple threads can read from and write
* to at the same time.
* To add elements to the queue, use the push_back() function.
* To remove and retrieve elements, use the pop_front() function, but remember to
* deallocate the value with deallocate() after using it.
*
* @tparam T Type of the objects this queue will hold.
* @tparam Allocator Allocator type
*/
template <typename T, typename Allocator = std::allocator<T> >
class atomic_queue_base
{
public:
/** Construct an empty qeue.
*
* @param alc Allocator object that is to be used for memory
* allocation/deallocation.
*
* @note The queue is thread-safe after this function has returned.
*/
atomic_queue_base(const Allocator& alc = Allocator()) noexcept
: size_(0u), front_(nullptr), back_(nullptr),
alc_(alc)
{ }
/** Destructor.
*
* @note The queue is thread-safe before this function is invoked.
*/
~atomic_queue_base() noexcept
{
node<T>* fr = front_;
while(fr)
{
node<T>* next = fr->next;
NodeAllocatorTraits::destroy(alc_, fr);
NodeAllocatorTraits::deallocate(alc_, fr, 1);
fr = next;
}
}
/** Push object into the queue by copying it.
*
* @param t Object you want to push into the queue. Requires T to be
* CopyConstructible.
* @throws Any exceptions thrown by the copy constructor of the object.
*
* @note This function is Thread-safe, lock-free and wait-free.
*/
void push_back(const T& t)
{
auto new_node = NodeAllocatorTraits::allocate(alc_, 1);
try {
ValueAllocator alc(alc_);
ValueAllocatorTraits::construct(
alc,
&new_node->t, t
);
} catch(...)
{
NodeAllocatorTraits::deallocate(alc_, new_node, 1);
throw;
}
new_node->next = nullptr;
push_node(new_node);
}
/** Push object into the queue by moving it.
*
* @param t Object you want to push into the queue. Requires T to be
* MoveConstructible.
* @throws Any exceptions thrown by the move constructor of the object.
*
* @note This function is Thread-safe, lock-free and wait-free.
*/
void push_back(T&& t)
{
auto new_node = NodeAllocatorTraits::allocate(alc_, 1);
try {
ValueAllocator alc(alc_);
ValueAllocatorTraits::construct(
alc, &new_node->t, std::move(t)
);
} catch(...)
{
NodeAllocatorTraits::deallocate(alc_, new_node, 1);
throw;
}
new_node->next = nullptr;
push_node(new_node);
}
// I WANT C++11!!! NOW!!!
#if !(defined(_MSC_VER) && _MSC_VER <= 1700)
/** Create and push object into the queue.
*
* @param args... Arguments to the objects constructor
* @throws Any exceptions thrown by the constructor of the object.
*
* @note This function is Thread-safe, lock-free and wait-free.
*/
template<typename... Args>
void emplace_back(Args&&... args)
{
auto new_node = NodeAllocatorTraits::allocate(alc_, 1);
try {
ValueAllocator alc(alc_);
ValueAllocatorTraits::construct(
alc, &new_node->t, std::forward<Args>(args)...
);
} catch(...)
{
NodeAllocatorTraits::deallocate(alc_, new_node, 1);
throw;
}
new_node->next = nullptr;
push_node(new_node);
}
#endif
/** Pop object from the queue.
*
* This function removes an object from the front of the queue and returns
* a pointer to it. Do not delete the object manually!
*
* To delete an object returned by this function, use the deallocate()
* member function.
*
* @return A pointer to the object that was removed from the queue.
*
* @note This function is Thread-safe, lock-free but not wait-free.
*/
T* pop_front() noexcept
{
node<T>* old_front = front_;
node<T>* new_front;
do {
if (!old_front) return nullptr; // nothing to pop
new_front = old_front->next;
} while(!front_.compare_exchange_weak(old_front, new_front));
--size_;
// if the old front was also the back, the queue is now empty.
new_front = old_front;
if(back_.compare_exchange_strong(new_front, nullptr))
old_front->next = old_front;
return reinterpret_cast<T*>(old_front);
}
/** Deallocate an object returned by pop_front().
*
* This function must be used to destroy objects that are returned by
* pop_front().
* Do not use this function with an object that was not returned by
* pop_front(), even if the type matches.
*
* @param obj A pointer to an object returned by pop_front().
*
* @note This function is Thread-safe, lock-free but not wait-free.
*/
void deallocate(T* obj) noexcept
{
if (!obj) return;
// call destructor
NodeAllocatorTraits::destroy(alc_, reinterpret_cast<node<T>*>(obj));
// nodes with next == 0 are still referenced by an executing
// push_back() function and the next ptr will be modified.
// Since we don't want push_back() to write to deallocated memory, we hang
// in a loop until the node has a non-zero next ptr.
while(!reinterpret_cast<node<T>*>(obj)->next.load())
std::this_thread::yield();
NodeAllocatorTraits::deallocate(
alc_, reinterpret_cast<node<T>*>(obj), 1
);
}
/** Get size of the queue.
*
* This function returns an approximation of the current queue size.
* In a multithreaded environment, this value might change immediately
* before or after the invocation. Especially, it is *not* guaranteed that
* if(size() != 0) assert(pop_front() != nullptr) !
*
* In multithreaded environments, please consider to not use this function.
*
* @return Current size of the queue.
*
* @note This function is Thread-safe, lock-free and wait-free.
*/
std::size_t size() const noexcept
{ return size_; }
protected:
void push_node(node<T>* new_node) noexcept
{
node<T>* old_back = back_.exchange(new_node);
node<T>* null_node = nullptr;
// if front_ was set to null (no node yet), we have to update the front_
// pointer.
if(!front_.compare_exchange_strong(null_node, new_node))
// if front_ is not null, then there was a previous node.
// We have to update this nodes next pointer.
old_back->next = new_node;
++size_;
}
typedef Allocator ValueAllocator;
typedef std::allocator_traits<ValueAllocator> ValueAllocatorTraits;
// Rebind allocator traits for ValueAllocator to our own NodeAllocator
typedef
typename ValueAllocatorTraits::template rebind_traits<node<T> >
#if defined(_MSC_VER) && _MSC_VER <= 1700
::other
#endif
NodeAllocatorTraits;
// Get actual allocator type from traits
typedef typename NodeAllocatorTraits::allocator_type NodeAllocator;
std::atomic_size_t size_ /**< Current size of queue. Not reliable. */;
std::atomic<node<T>*> front_ /**< Front of the queue. */;
std::atomic<node<T>*> back_ /**< Back of the queue. */;
NodeAllocator alc_ /**< Allocator for node<T> objects. */;
};
} // namespace detail
using detail::atomic_queue_base;
} // namespace aq
#endif // ifndef ATOMIC_QUEUE_HPP_INCLUDED